-
Notifications
You must be signed in to change notification settings - Fork 16
/
gulpfile.js
416 lines (369 loc) · 12.5 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
'use strict';
var gulp = require('gulp');
var concat = require('gulp-concat');
var filter = require('gulp-filter');
var inject = require('gulp-inject');
var markdown = require('gulp-markdown');
var minifyCSS = require('gulp-minify-css');
var minifyHTML = require('gulp-minify-html');
var protractor = require('gulp-protractor').protractor;
var sourcemaps = require('gulp-sourcemaps');
var tsc = require('gulp-typescript');
var uglify = require('gulp-uglify');
var watch = require('gulp-watch');
var webdriver_update = require('gulp-protractor').webdriver_update;
var Builder = require('systemjs-builder');
var del = require('del');
var escape = require('escape-html');
var exec = require('child_process').exec;
var fs = require('fs');
var http = require('http');
var karma = require('karma').Server;
var path = require('path');
var join = path.join;
var runSequence = require('run-sequence');
var through2 = require('through2');
var express = require('express');
var openResource = require('open');
// --------------
// Configuration.
var PATH = {
dest: {
all: 'dist',
dev: {
all: 'dist/dev',
lib: 'dist/dev/lib'
},
prod: {
all: 'dist/prod',
lib: 'dist/prod/lib',
ng2: 'angular2-and-all-libs.js'
}
},
src: {
html: ['demo/**/*.html', 'src/**/*.html'],
assets: ['demo/**/*.css'],
sourceToDisplay: ['demo/**/demo-*', '!demo/demo-app.ts'],
md: ['demo/**/*.md'],
ts: ['demo/**/*.ts', 'src/**/*.ts'],
test: ['test/**/*.ts'],
// Order is quite important here for the HTML tag injection.
lib: {
dev: [
'node_modules/es6-shim/es6-shim.js',
'demo/shims_for_old_browsers.js',
'node_modules/systemjs/dist/system.js',
'demo/system.conf.js',
'node_modules/angular2/bundles/angular2.dev.js',
'node_modules/angular2/bundles/router.dev.js'
],
prod: [
'node_modules/es6-shim/es6-shim.js',
'demo/shims_for_old_browsers.js',
'node_modules/systemjs/dist/system.js',
'demo/system.conf.js',
'node_modules/angular2/bundles/angular2.min.js',
'node_modules/angular2/bundles/router.dev.js'
]
}
}
};
var tsProject = tsc.createProject('tsconfig.json', {
typescript: require('typescript')
});
//Automate the generation
gulp.task('generateDeclaration', ['clean.tmp'], function () {
var result = gulp.src(['src/**/*.ts'])
.pipe(tsc(tsProject));
return result.dts
.pipe(gulp.dest('tmp'));
});
var SERVER_PORT = 5555;
// --------------
// Clean.
gulp.task('clean', function (done) {
del(PATH.dest.all, done);
});
gulp.task('clean.dev', function (done) {
del(PATH.dest.dev.all, done);
});
gulp.task('clean.app.dev', function (done) {
// TODO: rework this part.
del([join(PATH.dest.dev.all, '**/*'), '!' +
PATH.dest.dev.lib, '!' + join(PATH.dest.dev.lib, '*')], done);
});
gulp.task('clean.prod', function (done) {
del(PATH.dest.prod.all, done);
});
gulp.task('clean.app.prod', function (done) {
// TODO: rework this part.
del([join(PATH.dest.prod.all, '**/*'), '!' +
PATH.dest.prod.lib, '!' + join(PATH.dest.prod.lib, '*')], done);
});
gulp.task('clean.tmp', function(done) {
del('tmp', done);
});
// --------------
// Build dev.
gulp.task('build.lib.dev', function () {
return gulp.src(PATH.src.lib.dev)
.pipe(gulp.dest(PATH.dest.dev.lib));
});
gulp.task('build.js.dev', function () {
var result = gulp.src(PATH.src.ts.concat(PATH.src.test))
.pipe(sourcemaps.init())
.pipe(tsc(tsProject));
return result.js
.pipe(sourcemaps.write())
.pipe(gulp.dest(PATH.dest.dev.all));
});
gulp.task('build.sources.dev', ['build.js.dev'], function () {
var filterSourceCode = filter('**/samples/**/demo-*', {restore: true});
return gulp.src(PATH.src.assets.concat(PATH.src.sourceToDisplay))
.pipe(filterSourceCode)
.pipe(convertSourceCode())
.pipe(filterSourceCode.restore)
.pipe(gulp.dest(PATH.dest.dev.all));
});
gulp.task('build.assets.dev', ['build.sources.dev'], function () {
var filterMD = filter('**/*.md', {restore: true});
return gulp.src(PATH.src.html.concat(PATH.src.md))
.pipe(filterMD)
.pipe(markdown())
.pipe(convertTables())
.pipe(filterMD.restore)
.pipe(gulp.dest(PATH.dest.dev.all));
});
gulp.task('build.index.dev', function() {
var lib = gulp.src(injectableDevAssetsRef(), { read: false });
return gulp.src('./demo/index.html')
.pipe(inject(lib, { transform: transformPath('dev') }))
.pipe(gulp.dest(PATH.dest.dev.all));
});
gulp.task('build.app.dev', function (done) {
runSequence('clean.app.dev', 'build.assets.dev', 'build.index.dev', done);
});
gulp.task('build.dev', function (done) {
runSequence('clean.dev', 'build.lib.dev', 'build.app.dev', done);
});
// --------------
// Build prod.
var demoProdBuilder = new Builder('tmp');
demoProdBuilder.config({
meta: {
'angular2-bootstrap': { build: false },
'angular2/angular2': { build: false },
'angular2/router': { build: false }
},
defaultJSExtensions: true
});
var appProdBuilder = new Builder('tmp');
appProdBuilder.config({
meta: {
'angular2/angular2': { build: false },
'angular2/router': { build: false }
},
defaultJSExtensions: true
});
gulp.task('build.lib.prod', function () {
return gulp.src(PATH.src.lib.prod)
.pipe(concat(PATH.dest.prod.ng2))
.pipe(uglify())
.pipe(gulp.dest(PATH.dest.prod.lib));
});
gulp.task('build.sources.tmp', function () {
var filterSourceCode = filter('**/samples/**/demo-*', {restore: true});
return gulp.src(PATH.src.assets.concat(PATH.src.sourceToDisplay))
.pipe(filterSourceCode)
.pipe(convertSourceCode())
.pipe(filterSourceCode.restore)
.pipe(gulp.dest('tmp'));
});
gulp.task('build.assets.tmp', ['build.sources.tmp'], function () {
var filterMD = filter('**/*.md', {restore: true});
var filterHTML = filter('**/*.html', {restore: true});
return gulp.src(PATH.src.html.concat(PATH.src.md))
.pipe(filterMD)
.pipe(markdown())
.pipe(convertTables())
.pipe(filterMD.restore)
.pipe(filterHTML)
.pipe(minifyHTML({ conditionals: true }))
.pipe(filterHTML.restore)
.pipe(gulp.dest('tmp'));
});
gulp.task('build.js.tmp', ['build.assets.tmp'], function () {
var result = gulp.src(PATH.src.ts)
.pipe(tsc(tsProject));
return result.js
.pipe(inlineTemplates('tmp'))
.pipe(gulp.dest('tmp'));
});
// TODO: add inline source maps (System only generate separate source maps file).
gulp.task('build.demo.prod', ['build.js.tmp'], function() {
return demoProdBuilder.bundle('demo-app', join(PATH.dest.prod.all, 'demo-app.js'),
{ minify: true }).catch(function (e) { console.log(e); });
});
gulp.task('build.js.prod', ['build.demo.prod'], function() {
return appProdBuilder.bundle('angular2-bootstrap', join(PATH.dest.prod.lib, 'angular2-bootstrap.js'),
{ minify: true }).catch(function (e) { console.log(e); });
});
gulp.task('build.assets.prod', ['build.js.prod'], function () {
var filterCSS = filter('**/*.css', {restore: true});
return gulp.src(PATH.src.assets)
.pipe(filterCSS)
.pipe(minifyCSS())
.pipe(filterCSS.restore)
.pipe(gulp.dest(PATH.dest.prod.all));
});
gulp.task('build.index.prod', function() {
var lib = gulp.src([join(PATH.dest.prod.lib, '**/*.js'),
join(PATH.dest.prod.all, '**/*.css')], { read: false });
return gulp.src('./demo/index.html')
.pipe(inject(lib, { transform: transformPath('prod') }))
.pipe(gulp.dest(PATH.dest.prod.all));
});
gulp.task('build.app.prod', function (done) {
runSequence('clean.app.prod', 'build.assets.prod', 'build.index.prod', 'clean.tmp', done);
});
gulp.task('build.prod', function (done) {
runSequence('clean.prod', 'build.lib.prod', 'clean.tmp', 'build.app.prod', done);
});
// --------------
// Unit tests.
gulp.task('karma-launch', function() {
var server = new karma({
configFile: join(__dirname, 'karma.conf.js')
});
server.start();
});
gulp.task('karma-run', function (done) {
runKarma('karma.conf.js', done);
});
gulp.task('build.app.test.minify', function () {
return gulp.src([PATH.dest.dev.all + '/**/*.html'])
.pipe(minifyHTML({ conditionals: true }))
.pipe(gulp.dest(PATH.dest.dev.all));
});
gulp.task('build.app.test.inline', function () {
return gulp.src([PATH.dest.dev.all + '/**/*.js', '!' + PATH.dest.dev.all + '/lib/*'])
.pipe(inlineTemplates(PATH.dest.dev.all))
.pipe(gulp.dest(PATH.dest.dev.all));
});
gulp.task('test', ['build.dev'], function (neverDone) {
runSequence(
'build.app.test.minify',
'build.app.test.inline',
'karma-launch',
function() {
watch(['./src/**', './test/**'], function() {
runSequence('build.app.dev', 'build.app.test.minify', 'build.app.test.inline', 'karma-run');
});
}
);
});
// --------------
// E2E tests.
var e2eServer = null;
gulp.task('e2eServer', ['build.dev'], function (done) {
e2eServer = http.createServer(_startSPA('dev'));
e2eServer.listen(SERVER_PORT, done);
});
gulp.task('webdriver_update', webdriver_update);
gulp.task('e2e', ['webdriver_update', 'e2eServer'], function (done) {
gulp.src(['test-e2e/**/*spec.js']).pipe(protractor({
args: ['--baseUrl', 'http://localhost:' + SERVER_PORT]
})).on('error', function(e) {
e2eServer.close();
done();
}).on('end', function() {
e2eServer.close();
done();
});
});
// --------------
// Serve dev.
gulp.task('serve.dev', ['build.dev'], function () {
watch(['./src/**', './demo/**'], function () {
gulp.start('build.app.dev');
});
serveSPA('dev');
});
// --------------
// Serve prod.
gulp.task('serve.prod', ['build.prod'], function () {
watch(['./src/**', './demo/**'], function () {
gulp.start('build.app.prod');
});
serveSPA('prod');
});
// --------------
// Utils.
function transformPath(env) {
return function (filepath) {
arguments[0] = filepath.replace('/' + PATH.dest[env].all + '/', '');
return inject.transform.apply(inject.transform, arguments);
};
}
function injectableDevAssetsRef() {
var src = PATH.src.lib.dev.map(function(path) {
return join(PATH.dest.dev.lib, path.split('/').pop());
});
src.push(join(PATH.dest.dev.all, '**/*.css'));
return src;
}
function serveSPA(env) {
var app = _startSPA(env);
app.listen(SERVER_PORT, function () {
openResource('http://localhost:' + SERVER_PORT);
});
}
function _startSPA(env) {
var app = express().use(express.static(join(__dirname, PATH.dest[env].all)));
return app;
}
function convertTables() {
return through2.obj(function (file, encoding, done) {
var content = String(file.contents).replace(/<table>/g, '<table class="table table-bordered">');
file.contents = new Buffer(content);
this.push(file);
done();
});
}
function convertSourceCode() {
return through2.obj(function (file, encoding, done) {
var ext = path.extname(file.path);
var language = ext == '.ts' ? 'typescript' : 'html';
var content = '<pre><code class="language-' + language + '" ng-non-bindable>' + escape(String(file.contents)) + '</code></pre>';
file.contents = new Buffer(content);
file.path += '.source.html';
this.push(file);
done();
});
}
function inlineTemplates(folder) {
return through2.obj(function(file, encoding, done) {
var content = String(file.contents);
var pathPrefix = __dirname + path.sep + folder;
var re = new RegExp("templateUrl\\s*:\\s*(\'|\"|\`)([^\'\"\`]*)(\'|\"|\`)", "g");
var res = re.exec(content);
while (res) {
var tpl = fs.readFileSync(join(pathPrefix, res[2]), {encoding: 'utf8'}).replace(/\'/g, '\\\'').replace(/(\r\n|\n|\r)/gm, ' \'+\r\n\'');
content = content.replace(res[0], 'template: \'' + tpl + '\'');
res = re.exec(content);
}
file.contents = new Buffer(content);
this.push(file);
done();
});
}
function runKarma(configFile, done) {
var cmd = process.platform === 'win32' ? 'node_modules\\.bin\\karma run ' :
'node node_modules/.bin/karma run ';
cmd += configFile;
exec(cmd, function(e, stdout) {
// ignore errors, we don't want to fail the build in the interactive (non-ci) mode
// karma server will print all test failures
done();
});
}