-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
581 lines (529 loc) · 15.6 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
var gulp = require('gulp');
var watch = require('gulp-watch');
var plumber = require('gulp-plumber');
var babel = require('gulp-babel');
var changed = require('gulp-changed');
var filter = require('gulp-filter');
var nodemon = require('gulp-nodemon');
var browserSync = require('browser-sync');
var historyApiFallback = require('connect-history-api-fallback');
var jshint = require('gulp-jshint');
var runSequence = require('run-sequence');
var vinylPaths = require('vinyl-paths');
var del = require('del');
var stylish = require('jshint-stylish');
var assign = Object.assign || require('object.assign');
var sourcemaps = require('gulp-sourcemaps');
var ngHtml2Js = require('gulp-ng-html2js');
var htmlMin = require('gulp-minify-html');
var builder = require('systemjs-builder');
var RSVP = require('rsvp');
var less = require('gulp-less');
var svgSprite = require('gulp-svg-sprite');
var autoprefixer = require('gulp-autoprefixer');
var karma = require('karma').server;
var insert = require('gulp-insert');
var ngAnnotate = require('gulp-ng-annotate');
var fs = require('fs-extra');
var replace = require('gulp-replace-task');
var yaml = require('gulp-yaml');
var replace = require('gulp-replace');
var lessPluginCleanCSS = require('less-plugin-clean-css');
var cleancss = new lessPluginCleanCSS({advanced: true});
var cache = require('gulp-cached');
var uglify = require('gulp-uglify');
var adjustUrls = require('gulp-css-url-adjuster');
var routeBundler = require('systemjs-route-bundler');
var argv = require('yargs').argv;
var gulpif = require('gulp-if');
var rev = require('gulp-rev');
var revCollector = require('gulp-rev-collector');
var _ = require('lodash');
var gulpUtil = require('gulp-util');
var pandoc = require('gulp-pandoc');
var compilerOptions = {
// filename: '',
filenameRelative: '',
blacklist: [],
whitelist: [],
modules: 'system',
//sourceMap: true,
//sourceMapName: '',
//sourceFileName: '',
sourceRoot: '',
moduleRoot: '',
moduleIds: false,
experimental: false,
format: {
comments: false,
compact: false,
indent: {
parentheses: true,
adjustMultilineComment: true,
style: ' ',
base: 0
}
}
};
var path = {
srcDir:'client/src/',
jsSrc: [
'client/src/**/*.js',
'!client/src/system.config.js'
],
svgSrc:'client/src/assets/svg',
html:'client/src/**/*.html',
indexHtml:'client/src/index.html',
templates:[
'client/src/{app,common}/**/**.tpl.html',
'!client/src/index.html',
],
less: 'client/src/**/*.less',
ymlSrc: 'core-rules/**/*.yml',
output:'client/dist/',
distBase: 'dist/',
distClient: 'dist/client/',
outputCss: 'dist/client/**/*.css',
systemJsConfig: 'client/src/system.config.js'
};
var apiPath = {
source:'server/**/*.js'
};
browserSyncConfig = {
open: false,
port: 8000,
server: {
baseDir: ['dist'],
ui: false,
notify: false,
ghostMode: false,
routes: {
'/jspm_packages': './client/jspm_packages'
},
middleware: [
historyApiFallback,
function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
}
]
}
};
// prevent code syncing and live reload in production
if (argv.production) {
browserSyncConfig = _.merge(browserSyncConfig, {
codeSync: false,
reloadOnRestart: false,
server: {
snippetOptions: {
rule: {
match: /qqqqqqqqqqq/
}
}
}
});
};
var spriteConfig = {
shape: {
id: { // SVG shape ID related options
separator: '-', // Separator for directory name traversal
generator: function(name) {
return name.replace('.svg','');
}
}
},
svg: {
namespaceIDs: true
},
mode: {
stack: { // Activate the «css» mode
example: {
// for some reason the gulp wrapper for this doesn't pick up pathing
// relative to the parent, so we use a Node constant
// 'client/src/assets/svg/active/**/*.svg'
template: __dirname+'/'+path.svgSrc+'/sprite-template.html',
// dest: '../../',
},
dest: '',
bust: false,
render: {
css: false // Activate CSS output (with default options)
}
}
}
};
// pandoc -s -S --toc --template=template.html 00-introduction/01-introduction.md -o 00-introduction.tpl.html &&
// gulp.task('odt-to-md', function() {
// gulp.src('openlegend-corerules.odt')
// .pipe(pandoc({
// from: 'opendocument',
// to: 'markdown',
// ext: '.md',
// args: ['-s', '-S']
// }))
// .pipe(gulp.dest(path.distClient+'/app'));
// });
gulp.task('rules-docs', function() {
gulp.src('core-rules/core/src/**/**.md')
.pipe(pandoc({
from: 'markdown',
to: 'html5',
ext: '.tpl.html',
args: ['-s', '-S', '--toc', '--template=core-rules/core/src/template.html']
}))
.pipe(gulp.dest(path.srcDir+'/app/core'));
});
gulp.task('test', ['compile-all'], function (done) {
karma.start({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, function(err){
// `err` is (0 || 1), but we can't pass `err` to done() so we
// kill the process and return `1` so bash get's an echo-able exit code
if ( err === 1 ) process.exit(1);
done();
});
});
gulp.task('clean', function() {
return gulp.src([path.distBase+'/*'])
.pipe(vinylPaths(del));
});
gulp.task('html', function () {
return gulp.src(path.templates)
.pipe(cache('html'))
.pipe(plumber())
.pipe(changed(path.distClient, { extension: '.html' }))
.pipe(htmlMin({
empty: true,
spare: true,
quotes: true
}))
.pipe(ngHtml2Js())
// not entirely sure this is needed....
.pipe(insert.prepend("import angular from 'angular';\n"))
.pipe(babel(compilerOptions))
.pipe(gulp.dest(path.distClient))
.pipe(browserSync.reload({ stream: true }));
});
gulp.task('less', function () {
return gulp.src(path.less)
.pipe(cache('less'))
.pipe(plumber())
.pipe(changed(path.distClient, {extension: '.less'}))
.pipe(sourcemaps.init())
.pipe(less({
plugins: [ cleancss ]
}))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(path.distClient))
.pipe(filter('**/*.css')) // prevents reloading due to .map files
.pipe(browserSync.reload({ stream: true }));
});
gulp.task('rebase-css-paths', function(callback) {
return gulp.src(path.distClientCss)
.pipe(adjustUrls({
replace: [/(\.\.\/)+/,'dist/']
}))
.pipe(gulp.dest(path.distClient));
});
gulp.task('compile-sprites', function () {
// don't output docs file in production mode
if ( argv.production ) {
spriteConfig = _.merge(spriteConfig, {
mode: {
stack: {
example: false
}
}
});
}
return gulp.src(path.svgSrc+'/active/**/*.svg', {cwd:''})
.pipe(svgSprite( spriteConfig ))
.pipe(gulpif(argv.production, rev())) // revision the assets
.pipe(gulp.dest(path.distClient+'/assets')) // output the assets
.pipe(gulpif(argv.production, rev.manifest( // generate the rev manifest
path.distClient+'/rev-manifest.json',
{ base: path.distClient, merge: true}
))) // only run in production build
.pipe(gulpif(argv.production, gulp.dest(path.distClient))) // save the rev manifest
.pipe(browserSync.reload({ stream: true }));
});
gulp.task('json', function () {
return gulp.src('./client/src/**/*.json')
.pipe(changed(path.distClient, { extension: '.json' }))
.pipe(gulp.dest(path.distClient))
.pipe(browserSync.reload({ stream: true }));
});
gulp.task('index-html', function () {
return gulp.src(path.indexHtml)
.pipe(gulp.dest(path.distClient))
.pipe(browserSync.reload({ stream: true }));
});
gulp.task('move', ['index-html','json','compile-sprites'], function () {
return gulp.src([
'client/src/system.config.js',
'client/src/**/*.{svg,woff,ttf,png,gif,ico,jpg,eot,pdf}',
'!'+path.svgSrc+'/**/*'
])
.pipe(cache('move'))
.pipe(gulpif(argv.production, rev())) // revision the assets
.pipe(gulp.dest(path.distClient)) // output the assets
.pipe(gulpif(argv.production, rev.manifest( // generate the rev manifest
path.distClient+'/rev-manifest.json',
{ base: path.distClient, merge: true}
))) // only run in production build
.pipe(gulpif(argv.production, gulp.dest(path.distClient))) // save the rev manifest
.pipe(browserSync.reload({ stream: true }));
});
gulp.task('copy-client', function () {
return gulp.src([
'./client/jspm_packages/**/*',
], { base: './' })
.pipe(cache('copy-client'))
.pipe(gulp.dest(path.distClient)) // output the assets
.pipe(browserSync.reload({ stream: true }))
.on('end', function () {
fs.removeSync('dist/client/jspm_packages');
fs.rename('dist/client/client/jspm_packages', 'dist/client/jspm_packages', function (err) {
if (err) return console.error(err);
console.log('moved jspm_packages dir to dist/client')
});
});
});
// gulp.task('copy-server', function () {
// return gulp.src([
// '.openshift',
// './package.json',
// './app.yaml',
// './server/**/*',
// ], { base: './' })
// .pipe(cache('copy-server'))
// .pipe(gulp.dest(path.distBase)) // output the assets
// .pipe(browserSync.reload({ stream: true }));
// });
gulp.task('cache-bust', function(){
return gulp.src( path.distClient + 'index.html')
.pipe(replace({
usePrefix: false,
patterns: [
{
match: '<!--PROD',
replacement: ''
},
{
match: 'END-->',
replacement: ''
},
{
match: '{{hash}}',
replacement: Math.round(new Date() / 1000)
}
]
}))
.pipe(gulp.dest('./'));
});
gulp.task('rev', function () {
return gulp.src([
path.distClient+'**/*.{css,map,js,html}**',
])
.pipe( revCollector({
replaceReved: true,
dirReplacements: {
// 'css': '/dist/css',
// '/js/': '/dist/js/',
}
}) )
.pipe( gulp.dest(path.distClient) );
});
gulp.task('es6', function () {
return gulp.src(path.jsSrc)
.pipe(cache('es6'))
.pipe(plumber())
.pipe(changed(path.distClient, { extension: '.js' }))
.pipe(sourcemaps.init())
.pipe(babel(compilerOptions))
.pipe(ngAnnotate({
sourceMap: true,
gulpWarnings: false
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(path.distClient))
.pipe(browserSync.reload({ stream: true }));
});
// compile rules from yaml to json and output to
gulp.task('yaml', function(){
gulp.src(path.ymlSrc)
.pipe(yaml({
space: 2
}))
// replaces newlines from YAML "foo : |" linebreaks
.pipe(replace('\\n', '<br />'))
.pipe(gulp.dest(path.distClient+'/app'))
});
gulp.task('timestamp', function(){
gulp.src(path.srcDir+'config/config.js')
.pipe(babel(compilerOptions))
.pipe(replace('DATE_TO_REPLACE', Date.now()))
.pipe(gulp.dest(path.distClient+'config'))
});
gulp.task('compile-all', function (callback) {
return runSequence(
// ['less', 'html', 'es6', 'yaml', 'move', 'copy-client', 'copy-server'],
['less', 'html', 'es6', 'yaml', 'move', 'copy-client'],
'timestamp',
callback
);
});
gulp.task('compile-other', function (callback) {
return runSequence(
['html', 'es6', 'move'],
'timestamp',
callback
);
});
gulp.task('compile-less', function (callback) {
return runSequence(
['less','timestamp'],
callback
);
});
gulp.task('recompile', function (callback) {
return runSequence(
'clean',
['compile-all'],
callback
);
});
gulp.task('compile-production', function (callback) {
return runSequence(
'recompile',
'minify',
'cache-bust',
// 'rev',
// ['rebase-css-paths'],
callback
)
});
gulp.task('minify', function(){
var condition = '**/routing.js';
return gulp.src([
'dist/client/**/*.js',
'!**/routing.js',
'!**/lazy-routes.js',
'!**/routes.js',
'!**/*.map',
'!**/jspm_packages/**/*'
])
// .pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify(
{mangle: false}
).on('error', gulpUtil.log))
// .pipe(sourcemaps.write('.'))
.pipe(gulp.dest(path.distClient))
});
// gulp.task('release', function(callback) {
// return runSequence(
// ['bundle', 'cache-bust'],
// 'minify',
// callback
// );
// });
gulp.task('lint-ui', function() {
var settings = { fail: argv.production ? true : false };
return gulp.src(path.jsSrc)
.pipe(jshint())
.pipe(jshint.reporter(stylish, settings));
});
gulp.task('serve', ['api'], function (done) {
browserSync(browserSyncConfig, done);
});
// gulp.task('lint-api', function () {
// var settings = { fail: argv.production ? true : false };
// return gulp.src([apiPath.source, '!server/public/docs/**/*'])
// .pipe(jshint())
// .pipe(jshint.reporter(stylish, settings));
// });
gulp.task('api', function () {
// check for --debug || --debug-brk flag, if present, run node debugger
var debugArg = argv.debug ? '--debug=5858' : argv['debug-brk'] ? '--debug=5858 --debug-brk' : '';
nodemon({
script: 'server/app.js',
ext: 'js',
ignore: ['client/**','node_modules/**'],
nodeArgs: [ '--harmony', debugArg ]
})
.on('restart', function () {
console.log('API server restarted ' + new Date());
});
});
gulp.task('watch', function () {
var standardWatchOptions = {
usePolling: true
}
var clientJsHtml = path.jsSrc.concat( path.html );
watch(clientJsHtml, standardWatchOptions, function (file) {
console.log( file.event + ' in ' + file.relative + ', re-compiling main assets...');
gulp.start(['lint-ui','compile-other']);
});
watch([path.less], standardWatchOptions, function (file) {
console.log( file.event + ' in ' + file.relative + ', re-compiling LESS...');
gulp.start(['compile-less']);
});
watch([path.ymlSrc], standardWatchOptions, function (file) {
console.log( file.event + ' in ' + file.relative + ', re-compiling YAML...');
gulp.start(['yaml','timestamp']);
});
// watch([apiPath.source], standardWatchOptions, function (file) {
// console.log( file.event + ' in ' + file.relative + ', linting API files');
// gulp.start(['lint-api']);
// });
// watch(['./package.json'], standardWatchOptions, function (file) {
// console.log('package.json changed. Rebuilding systemjs config for jspm dependency changes');
// gulp.start('bundle');
// })
});
gulp.task('bundle', ['compile-production'], function () {
// only bundle routes in production context
if ( ! argv.production ) return;
var routes = require('./client/src/app/routes.json');
// get the source paths of our routes
routes = routes.map(function (r) { return r.src; });
var config = {
baseURL: path.distClient,
main: 'app/app',
routes: routes,
bundleThreshold: 0.6,
config: path.systemJsConfig,
sourceMaps: true,
minify: true,
mangle: true,
dest: path.distClient + '/app',
destJs: path.distClient + '/app/app.js'
}
return routeBundler.build(config);
});
gulp.task('run', function (callback) {
// tasks = ['lint-api','lint-ui'];
tasks = ['lint-ui'];
// only run `build` in production context
if ( argv.production ) {
tasks.push('bundle');
} else {
tasks.push('recompile')
}
tasks.push('watch','serve');
// start node debugger task if --debug || --debug-brk
if ( argv.debug || argv['debug-brk'] ) {
tasks.push('debug');
}
// call apply to pass the array as a chain of params
return runSequence.apply(null, tasks);
});
gulp.task('noop', function(callback) {
console.log("noop");
});
gulp.task('default', ['run']);