forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
1016 lines (877 loc) · 25.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var buildConfig = require('./scripts/build/config');
var gulp = require('gulp');
var path = require('path');
var argv = require('yargs').argv;
var del = require('del');
var rename = require('gulp-rename');
var through2 = require('through2');
var runSequence = require('run-sequence');
var watch = require('gulp-watch');
var tsc = require('gulp-typescript');
var cache = require('gulp-cached');
var remember = require('gulp-remember');
var minimist = require('minimist');
var connect = require('gulp-connect');
var docsConfig = require('./scripts/config.json');
var flagConfig = {
string: ['port'],
boolean: ['debug', 'typecheck'],
alias: {'p': 'port'},
default: { 'port': 8000, 'debug': true, 'typecheck': false }
};
var flags = minimist(process.argv.slice(2), flagConfig);
var DEBUG = flags.debug;
var TYPECHECK = flags.typecheck;
function getTscOptions(name) {
var opts = {
emitDecoratorMetadata: true,
experimentalDecorators: true,
target: "es5",
module: "commonjs",
isolatedModules: true,
typescript: require('typescript')
}
if (name === "typecheck") {
opts.declaration = true;
delete opts.isolatedModules;
} else if (name === "es6") {
opts.target = "es6";
delete opts.module;
}
return opts;
}
var tscReporter = {
error: function (error) {
// TODO
// suppress type errors until we convert everything to TS
// console.error(error.message);
}
};
// We use Babel to easily create named System.register modules
// See: https://github.com/Microsoft/TypeScript/issues/4801
// and https://github.com/ivogabe/gulp-typescript/issues/211
var babelOptions = {
presets: ['es2015'],
plugins: ['transform-es2015-modules-systemjs'],
moduleIds: true,
getModuleId: function(name) {
return 'ionic-angular/' + name;
}
}
/**
* Builds Ionic sources to dist. When the '--typecheck' flag is specified,
* generates .d.ts definitions and does typechecking.
*/
gulp.task('build', function(done){
runSequence(
'copy.libs',
['bundle', 'sass', 'fonts', 'copy.scss'],
done
);
});
/**
* Builds sources to dist and watches for changes. Runs 'transpile' for .ts
* changes and 'sass' for .scss changes.
*/
gulp.task('watch', ['build'], function() {
watchTask('transpile');
});
function watchTask(task){
watch([
'src/**/*.ts',
'!src/components/*/test/**/*',
'!src/util/test/*'
],
function(file) {
if (file.event === "unlink") {
deleteFile(file);
} else {
gulp.start(task);
}
}
);
watch('src/**/*.scss', function() {
gulp.start('sass');
});
gulp.start('serve');
function deleteFile(file) {
//TODO
// var basePath = file.base.substring(0, file.base.lastIndexOf("ionic/"));
// var relativePath = file.history[0].replace(file.base, '').replace('.ts', '.js');
//
// var filePath = basePath + 'dist/' + relativePath;
// var typingPath = filePath.replace('.js', '.d.ts');
//
// delete cache.caches['no-typecheck'][file.history[0]];
// remember.forget('no-typecheck', file.history[0]);
//
// del([filePath, typingPath], function(){
// gulp.start('bundle.system');
// });
}
}
gulp.task('serve', function() {
connect.server({
port: flags.port,
livereload: {
port: 35700
}
});
});
gulp.task('clean', function(done) {
del(['dist/**', '!dist'], done);
});
/**
* Source build tasks
*/
/**
* Creates CommonJS and SystemJS bundles from Ionic source files.
*/
gulp.task('bundle', ['bundle.cjs', 'bundle.system']);
/**
* Creates CommonJS bundle from Ionic source files.
*/
gulp.task('bundle.cjs', ['transpile', 'copy.libs'], function(done){
var config = require('./scripts/npm/ionic.webpack.config.js');
bundle({ config: config, stats: true });
// build minified bundle
var minConfig = require('./scripts/npm/ionic.min.webpack.config.js');
bundle({ config: minConfig, cb: finished, stats: true });
var outputPaths = [
config.output.path + path.sep + config.output.filename,
minConfig.output.path + path.sep + minConfig.output.filename
];
function bundle(args) {
var webpack = require('webpack');
var path = require('path');
webpack(args.config, function(err, stats){
if (args.stats) {
var statsOptions = {
'colors': true,
'modules': false,
'chunks': false,
'exclude': ['node_module'],
'errorDetails': true
}
console.log(stats.toString(statsOptions));
}
args.cb && args.cb();
})
}
function finished(){
gulp.src(outputPaths)
.pipe(connect.reload())
.on('end', done);
}
});
/**
* Creates SystemJS bundle from Ionic source files.
*/
gulp.task('bundle.system', function(){
var babel = require('gulp-babel');
var concat = require('gulp-concat');
var gulpif = require('gulp-if');
var stripDebug = require('gulp-strip-debug');
var merge = require('merge2');
var tsResult = tsCompile(getTscOptions('es6'), 'system')
.pipe(babel(babelOptions));
var swiper = gulp.src('src/components/slides/swiper-widget.system.js');
return merge([tsResult, swiper])
.pipe(remember('system'))
.pipe(gulpif(!DEBUG, stripDebug()))
.pipe(concat('ionic.system.js'))
.pipe(gulp.dest('dist/bundles'))
.pipe(connect.reload())
});
/**
* Transpiles TypeScript sources to ES5 in the CommonJS module format and outputs
* them to dist. When the '--typecheck' flag is specified, generates .d.ts
* definitions and does typechecking.
*/
gulp.task('transpile', function(){
var gulpif = require('gulp-if');
var stripDebug = require('gulp-strip-debug');
var tscOpts = getTscOptions(TYPECHECK ? 'typecheck' : undefined);
var tsResult = tsCompile(tscOpts, 'transpile')
.on('error', function(err) {
console.log(err.message);
});
if (TYPECHECK) {
tsResult.on('error', function(err) {
process.exit(1);
});
var merge = require('merge2');
var js = tsResult.js;
var dts = tsResult.dts;
if (!DEBUG) js = js.pipe(stripDebug());
// merge definition and source streams
return merge([js, dts])
.pipe(gulp.dest('dist'));
}
if (!DEBUG) tsResult = tsResult.pipe(stripDebug());
return tsResult.pipe(gulp.dest('dist'));
});
function tsCompile(options, cacheName){
return gulp.src([
'typings/main.d.ts',
'src/**/*.ts',
'!src/**/*.d.ts',
'!src/components/*/test/**/*',
'!src/util/test/*',
'!src/config/test/*',
'!src/platform/test/*',
'!src/**/*.spec.ts'
])
.pipe(cache(cacheName, { optimizeMemory: true }))
.pipe(tsc(options, undefined, tscReporter));
}
gulp.task('bundle.es6', function() {
var babel = require('gulp-babel');
gulp.src([
'src/components/slides/swiper-widget.js'
])
.pipe(gulp.dest('dist/esm/components/slides'));
return tsCompile(getTscOptions('es6'), 'bundle.es6')
.pipe(babel({
presets: ['es2015-native-modules']
}))
.pipe(gulp.dest('dist/esm'));
});
/**
* Compiles Ionic Sass sources to stylesheets and outputs them to dist/bundles.
*/
gulp.task('sass', function() {
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var minifyCss = require('gulp-minify-css');
gulp.src([
'src/ionic.ios.scss',
'src/ionic.md.scss',
'src/ionic.wp.scss',
'src/ionic.scss'
])
.pipe(sass({
includePaths: [__dirname + '/node_modules/ionicons/dist/scss/'],
}).on('error', sass.logError)
)
.pipe(autoprefixer(buildConfig.autoprefixer))
.pipe(gulp.dest('dist/bundles/'))
.pipe(minifyCss())
.pipe(rename({ extname: '.min.css' }))
.pipe(gulp.dest('dist/bundles/'));
});
/**
* Creates Ionic themes for testing.
*/
gulp.task('sass.themes', function() {
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
function buildTheme(mode) {
gulp.src([
'scripts/e2e/ionic.' + mode + '.dark.scss'
])
.pipe(sass({
includePaths: [__dirname + '/node_modules/ionicons/dist/scss/'],
}).on('error', sass.logError)
)
.pipe(autoprefixer(buildConfig.autoprefixer))
.pipe(gulp.dest('dist/bundles/'));
}
buildTheme('ios');
buildTheme('md');
buildTheme('wp');
});
/**
* Copies fonts and Ionicons to dist/fonts
*/
gulp.task('fonts', function() {
gulp.src([
'src/fonts/*.+(ttf|woff|woff2)',
'node_modules/ionicons/dist/fonts/*.+(ttf|woff|woff2)'
])
.pipe(gulp.dest('dist/fonts'));
});
/**
* Copies Ionic Sass sources to dist
*/
gulp.task('copy.scss', function() {
return gulp.src([
'src/**/*.scss',
'!src/components/*/test/**/*',
'!src/util/test/*'
])
.pipe(gulp.dest('dist'));
});
/**
* Lint the scss files using a ruby gem
*/
gulp.task('lint.scss', function() {
var scsslint = require('gulp-scss-lint');
return gulp.src([
'src/**/*.scss',
'!src/components/*/test/**/*',
'!src/util/test/*'
])
.pipe(scsslint())
.pipe(scsslint.failReporter());
});
/**
* Copies miscellaneous scripts to dist.
*/
gulp.task('copy.libs', function() {
var merge = require('merge2');
var extModules = gulp.src([
'node_modules/es6-shim/es6-shim.min.js',
'node_modules/systemjs/node_modules/es6-module-loader/dist/es6-module-loader.src.js', //npm2
'node_modules/es6-module-loader/dist/es6-module-loader.src.js', //npm3
'node_modules/systemjs/dist/system.src.js',
'node_modules/rxjs/bundles/Rx.js',
'node_modules/zone.js/dist/zone.js',
'node_modules/reflect-metadata/Reflect.js'
])
.pipe(gulp.dest('dist/js'));
// for swiper-widget
var libs = gulp.src([
'src/**/*.js',
'src/**/*.d.ts'
])
.pipe(gulp.dest('dist'));
return merge([extModules, libs]);
});
/**
* Test build tasks
*/
/**
* Builds e2e tests to dist/e2e and watches for changes. Runs 'bundle.system' or
* 'sass' on Ionic source changes and 'e2e.build' for e2e test changes.
*/
gulp.task('watch.e2e', ['e2e'], function() {
watchTask('bundle.system');
watch('src/components/*/test/**/*', function(file) {
gulp.start('e2e.build');
});
});
/**
* Builds Ionic e2e tests to dist/e2e and creates the necessary files for tests
* to run.
*/
gulp.task('e2e', ['e2e.build', 'bundle.system', 'copy.libs', 'sass', 'fonts']);
/**
* Builds Ionic e2e tests to dist/e2e.
*/
gulp.task('e2e.build', function() {
var gulpif = require('gulp-if');
var merge = require('merge2');
var _ = require('lodash');
var fs = require('fs');
var VinylFile = require('vinyl');
var indexTemplate = _.template(
fs.readFileSync('scripts/e2e/e2e.template.html')
)({
buildConfig: buildConfig
});
var testTemplate = _.template(fs.readFileSync('scripts/e2e/e2e.template.js'));
var platforms = [
'android',
'ios',
'windows'
];
// Get each test folder with gulp.src
var tsResult = gulp.src([
'src/components/*/test/*/**/*.ts',
'!src/components/*/test/*/**/*.spec.ts'
])
.pipe(cache('e2e.ts'))
.pipe(tsc(getTscOptions(), undefined, tscReporter))
.on('error', function(error) {
console.log(error.message);
})
.pipe(gulpif(/index.js$/, createIndexHTML()))
.pipe(gulpif(/e2e.js$/, createPlatformTests()))
var testFiles = gulp.src([
'src/components/*/test/*/**/*',
'!src/components/*/test/*/**/*.ts'
])
.pipe(cache('e2e.files'))
return merge([
tsResult,
testFiles
])
.pipe(rename(function(file) {
var sep = path.sep;
file.dirname = file.dirname.replace(sep + 'test' + sep, sep);
}))
.pipe(gulp.dest('dist/e2e/'))
.pipe(connect.reload());
function createIndexHTML() {
return through2.obj(function(file, enc, next) {
this.push(new VinylFile({
base: file.base,
contents: new Buffer(indexTemplate),
path: path.join(path.dirname(file.path), 'index.html'),
}));
next(null, file);
});
}
function createPlatformTests(file) {
return through2.obj(function(file, enc, next) {
var self = this;
var relativePath = path.dirname(file.path.replace(/^.*?src(\/|\\)components(\/|\\)/, ''));
relativePath = relativePath.replace('/test/', '/');
var contents = file.contents.toString();
platforms.forEach(function(platform) {
var platformContents = testTemplate({
contents: contents,
buildConfig: buildConfig,
relativePath: relativePath,
platform: platform
});
self.push(new VinylFile({
base: file.base,
contents: new Buffer(platformContents),
path: file.path.replace(/e2e.js$/, platform + '.e2e.js')
}));
})
next();
})
}
});
/**
* Builds Ionic unit tests to dist/tests.
*/
gulp.task('tests', function() {
return gulp.src('src/**/test/**/*.spec.ts')
.pipe(cache('tests'))
.pipe(tsc(getTscOptions(), undefined, tscReporter))
.pipe(rename(function(file) {
var regex = new RegExp(path.sep + 'test(' + path.sep + '|$)');
file.dirname = file.dirname.replace(regex, path.sep);
}))
.pipe(gulp.dest('dist/tests'))
});
gulp.task('watch.tests', ['tests'], function(){
watch('src/**/test/**/*.spec.ts', function(){
gulp.start('tests');
});
});
/**
* Demos
*/
/**
* Builds Ionic demos to dist/demos, copies them to ../ionic-site and watches
* for changes.
*/
//TODO, decide on workflow for site demos (dev and prod), vs local dev (in dist)
var LOCAL_DEMOS = false;
gulp.task('watch.demos', function(done) {
LOCAL_DEMOS = true;
runSequence(
['build.demos', 'transpile', 'copy.libs', 'sass', 'fonts'],
function(){
watchTask('bundle.system');
watch('demos/**/*', function(file) {
gulp.start('build.demos');
});
done();
}
);
});
/**
* Copies bundled demos from dist/demos to ../ionic-site/docs/v2 (assumes there is a
* sibling repo to this one named 'ionic-site')
*/
gulp.task('demos', ['bundle.demos'], function() {
var merge = require('merge2');
var demosStream = gulp.src([
'dist/demos/**/*',
'!dist/demos/**/*.scss',
])
.pipe(gulp.dest(docsConfig.docsDest + '/demos/'));
var cssStream = gulp.src('dist/bundles/**/*.css')
.pipe(gulp.dest(docsConfig.sitePath + '/dist/bundles'));
return merge([demosStream, cssStream]);
});
/**
* Builds necessary files for each demo then bundles them using webpack. Unlike
* e2e tests, demos are bundled for performance (but have a slower build).
*/
gulp.task('bundle.demos', ['build.demos', 'transpile', 'copy.libs', 'sass', 'fonts'], function(done) {
var glob = require('glob');
var webpack = require('webpack');
var path = require('path');
return glob('dist/demos/**/index.js', function(err, files){
var numTasks = files.length;
files.forEach(function(file){
var config = require('./scripts/demos/webpack.config.js');
// add our bundle entry, removing previous if necessary
// since config is cached
if (config.entry.length > 3) {
config.entry.pop();
}
config.entry.push('./' + file);
config.output = {
filename: path.dirname(file) + '/bundle.js'
}
webpack(config, function(err, stats){
var statsOptions = {
'colors': true,
'modules': false,
'chunks': false,
'exclude': ['node_modules'],
'errorDetails': true
}
console.log(stats.toString(statsOptions));
if (--numTasks === 0) done();
})
})
});
});
/**
* Transpiles and copies Ionic demo sources to dist/demos.
*/
gulp.task('build.demos', function() {
var gulpif = require('gulp-if');
var merge = require('merge2');
var _ = require('lodash');
var fs = require('fs');
var VinylFile = require('vinyl');
var indexTemplateName = LOCAL_DEMOS ? 'index.template.dev.html' : 'index.template.html';
var baseIndexTemplate = _.template(fs.readFileSync('scripts/demos/' + indexTemplateName))();
console.log(flags);
if (flags.production) {
buildDemoSass(true);
} else {
buildDemoSass(false);
}
var tsResult = gulp.src(['demos/**/*.ts'])
.pipe(cache('demos.ts'))
.pipe(tsc(getTscOptions(), undefined, tscReporter))
.on('error', function(error) {
console.log(error.message);
})
.pipe(gulpif(/index.js$/, createIndexHTML())) //TSC changes .ts to .js
var demoFiles = gulp.src([
'demos/**/*',
'!demos/**/*.ts'
])
.pipe(cache('demos.files'));
return merge([
tsResult,
demoFiles
])
.pipe(gulp.dest('dist/demos'))
.pipe(connect.reload());
function createIndexHTML() {
return through2.obj(function(file, enc, next) {
var indexTemplate = baseIndexTemplate;
var customTemplateFp = file.path.split('/').slice(0, -1).join('/') + '/index.html';
if (fs.existsSync(customTemplateFp)) {
indexTemplate = _.template(fs.readFileSync(customTemplateFp))();
}
this.push(new VinylFile({
base: file.base,
contents: new Buffer(indexTemplate),
path: path.join(path.dirname(file.path), 'index.html'),
}));
next(null, file);
});
}
});
function buildDemoSass(isProductionMode) {
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var minifyCss = require('gulp-minify-css');
var concat = require('gulp-concat');
var sassVars = isProductionMode ? 'demos/app.variables.production.scss': 'demos/app.variables.local.scss';
(function combineSass() {
gulp.src([
sassVars,
'demos/app.variables.scss',
'demos/app.ios.scss'
])
.pipe(concat('output.ios.scss'))
.pipe(gulp.dest('demos/'))
gulp.src([
sassVars,
'demos/app.variables.scss',
'demos/app.md.scss'
])
.pipe(concat('output.md.scss'))
.pipe(gulp.dest('demos/'))
gulp.src([
sassVars,
'demos/app.variables.scss',
'demos/app.wp.scss'
])
.pipe(concat('output.wp.scss'))
.pipe(gulp.dest('demos/'))
})();
gulp.src([
'demos/output.ios.scss',
'demos/output.md.scss',
'demos/output.wp.scss'
])
.pipe(sass({
includePaths: [__dirname + '/node_modules/ionicons/dist/scss/'],
}).on('error', sass.logError)
)
.pipe(autoprefixer(buildConfig.autoprefixer))
.pipe(gulp.dest('dist/demos/'))
.pipe(minifyCss())
.pipe(rename({ extname: '.min.css' }))
.pipe(gulp.dest('dist/bundles/'));
}
/**
* Tests
*/
require('./scripts/snapshot/snapshot.task')(gulp, argv, buildConfig);
// requires bundle.system to be run once
gulp.task('karma', ['tests'], function(done) {
var karma = require('karma').server;
karma.start({
configFile: __dirname + '/scripts/karma/karma.conf.js'
}, function(result) {
if (result > 0) {
return done(new Error('Karma exited with an error'));
}
done();
});
});
gulp.task('karma-watch', ['watch.tests', 'bundle.system'], function() {
watchTask('bundle.system');
var karma = require('karma').server;
return karma.start({ configFile: __dirname + '/scripts/karma/karma-watch.conf.js'})
});
/**
* Release
*/
/**
* Builds Ionic sources to dist with typechecking and d.ts definitions, does
* some prerelease magic (see 'prepare') and copies npm package and tooling
* files to dist.
*/
gulp.task('prerelease', function(done){
runSequence(
'validate',
'prepare',
'package',
done
);
});
/**
* Publishes to npm and creates a new tag and release on GitHub.
*/
gulp.task('release', ['publish.npm', 'publish.github']);
/**
* Pulls latest, ensures there are no unstaged/uncommitted changes, updates
* package.json minor version and generates CHANGELOG for release.
*/
gulp.task('prepare', ['git-pull-latest'], function(){
var semver = require('semver');
var fs = require('fs');
var changelog = require('gulp-conventional-changelog');
//Update package.json version
var packageJSON = require('./package.json');
packageJSON.version = semver.inc(packageJSON.version, 'prerelease', 'beta');
fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, 2));
//Update changelog
return gulp.src('./CHANGELOG.md')
.pipe(changelog({
preset: 'angular'
}))
.pipe(gulp.dest('./'));
});
gulp.task('git-pull-latest', function() {
var execSync = require('child_process').execSync;
var spawnSync = require('child_process').spawnSync;
function fail(context, msg) {
// remove gulp's 'Finished 'task' after 10ms' message
context.removeAllListeners('task_stop');
console.error('Prepare aborted.');
console.error(msg);
}
//Check for uncommitted changes
var gitStatusResult = execSync('git status --porcelain');
if (gitStatusResult.toString().length > 0) {
return fail(this, 'You have uncommitted changes, please stash or commit them before running prepare');
}
//Pull latest
var gitPullResult = spawnSync('git', ['pull', 'origin', 'master']);
if (gitPullResult.status !== 0) {
fail('There was an error running \'git pull\':\n' + gitPullResult.stderr.toString());
}
});
/**
* Copies npm package and tooling files to dist.
*/
gulp.task('package', function(done){
var fs = require('fs');
var distDir = 'dist';
gulp.src([
'scripts/npm/.npmignore',
'scripts/npm/README.md',
'*tooling/**/*'
])
.pipe(gulp.dest(distDir));
var templatePackageJSON = require('./scripts/npm/package.json');
var sourcePackageJSON = require('./package.json');
var sourceDependencies = sourcePackageJSON.dependencies;
// copy source package.json data to template
templatePackageJSON.version = sourcePackageJSON.version
templatePackageJSON.description = sourcePackageJSON.description
templatePackageJSON.keywords = sourcePackageJSON.keywords
// copy source dependencies versions to the template's peerDependencies
// only copy dependencies that show up as peerDependencies in the template
for (var dependency in sourceDependencies) {
if (dependency in templatePackageJSON.peerDependencies) {
templatePackageJSON.peerDependencies[dependency] = sourceDependencies[dependency];
}
}
fs.writeFileSync(distDir + '/package.json', JSON.stringify(templatePackageJSON, null, 2));
done();
});
/**
* Creates a new tag and release on GitHub.
*/
gulp.task('publish.github', function(done){
var changelog = require('conventional-changelog');
var GithubApi = require('github');
var packageJSON = require('./package.json');
var github = new GithubApi({
version: '3.0.0'
});
github.authenticate({
type: 'oauth',
token: process.env.GH_TOKEN
});
return changelog({
preset: 'angular'
})
.pipe(through2.obj(function(file, enc, cb){
github.releases.createRelease({
owner: 'driftyco',
repo: 'ionic',
target_commitish: 'master',
tag_name: 'v' + packageJSON.version,
name: packageJSON.version,
body: file.toString(),
prerelease: true
}, done);
}));
});
/**
* Publishes to npm.
*/
gulp.task('publish.npm', function(done) {
var spawn = require('child_process').spawn;
var npmCmd = spawn('npm', ['publish', './dist']);
npmCmd.stdout.on('data', function (data) {
console.log(data.toString());
});
npmCmd.stderr.on('data', function (data) {
console.log('npm err: ' + data.toString());
});
npmCmd.on('close', function() {
done();
});
});
/**
* Execute this task to validate current code and then
*/
gulp.task('publish.nightly', function(done){
runSequence('git-pull-latest', 'validate', 'nightly', done);
});
/**
* Publishes a new tag to npm with a nightly tag.
* This will only update the dist package.json file.
*/
gulp.task('nightly', ['package'], function(done) {
var fs = require('fs');
var spawn = require('child_process').spawn;
var packageJSON = require('./dist/package.json');
// Generate a unique id formatted from current timestamp
function createTimestamp() {
// YYYYMMDDHHMM
var d = new Date();
return d.getUTCFullYear() + // YYYY
('0' + (d.getUTCMonth() + 1)).slice(-2) + // MM
('0' + (d.getUTCDate())).slice(-2) + // DD
('0' + (d.getUTCHours())).slice(-2) + // HH
('0' + (d.getUTCMinutes())).slice(-2); // MM
}
/**
* Split the version on dash so that we can add nightly
* to all production, beta, and nightly releases
* 0.1.0 -becomes- 0.1.0-r8e7684t
* 0.1.0-beta.0 -becomes- 0.1.0-beta.0-r8e7684t
* 0.1.0-beta.0-t5678e3v -becomes- 0.1.0-beta.0-r8e7684t
*/
packageJSON.version = packageJSON.version.split('-')
.slice(0, 2)
.concat(createTimestamp())
.join('-');
fs.writeFileSync('./dist/package.json', JSON.stringify(packageJSON, null, 2));
var npmCmd = spawn('npm', ['publish', '--tag=nightly', './dist']);
npmCmd.stdout.on('data', function (data) {
console.log(data.toString());
});
npmCmd.stderr.on('data', function (data) {
console.log('npm err: ' + data.toString());
});
npmCmd.on('close', function() {
done();
});
});
/**
* Build Ionic sources, with typechecking, .d.ts definition generation and debug
* statements removed.
*/
gulp.task('build.release', function(done){
DEBUG = false;
TYPECHECK = true;
runSequence(
'clean',
'copy.libs',
['bundle', 'bundle.es6', 'sass', 'fonts', 'copy.scss'],
done
);
});
/**
* Docs
*/
require('./scripts/docs/gulp-tasks')(gulp, flags)
/**
* Tooling
*/
gulp.task('tooling', function(){
gulp.src('*tooling/**/*')
.pipe(gulp.dest('dist'));
watch('tooling/**/*', function(){
gulp.src('*tooling/**/*')
.pipe(gulp.dest('dist'));
})
});
/**
* Validate Task
* - This task
*/
gulp.task('validate', function(done) {
runSequence(
'lint.scss',
'tslint',
'build.release',
'karma',