forked from highcharts/highcharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
910 lines (851 loc) · 31.4 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
/* eslint-env node, es6 */
/* eslint no-console:0, valid-jsdoc:0 */
/* eslint-disable func-style */
'use strict';
const colors = require('colors');
const gulp = require('gulp');
const argv = require('yargs').argv;
/**
* Get the product version from build.properties.
* The product version is used in license headers and in package names.
* @return {string|null} Returns version number or null if not found.
*/
const getProductVersion = () => {
const fs = require('fs');
const D = require('./assembler/dependencies.js');
const properties = fs.readFileSync('./build.properties', 'utf8');
return D.regexGetCapture(/product\.version=(.+)/, properties);
};
/**
* Returns fileOptions for the build script
* @todo Move this functionality to the build script,
* and reuse it on github.highcharts.com
* @return {Object} Object containing all fileOptions
*/
const getFileOptions = (base) => {
const B = require('./assembler/build.js');
const DS = '[\\\\\\\/]';
const NOTDS = '[^\\\\\\\/]';
const SINGLEDS = DS + NOTDS; // Regex: Single directory seperator
const folders = {
'parts': 'parts' + SINGLEDS + '+\.js$',
'parts-more': 'parts-more' + SINGLEDS + '+\.js$',
'highchartsFiles': [
'parts' + DS + 'Globals\.js$',
'parts' + DS + 'SvgRenderer\.js$',
'parts' + DS + 'Html\.js$',
'parts' + DS + 'VmlRenderer\.js$',
'parts' + DS + 'Axis\.js$',
'parts' + DS + 'DateTimeAxis\.js$',
'parts' + DS + 'LogarithmicAxis\.js$',
'parts' + DS + 'Tooltip\.js$',
'parts' + DS + 'Pointer\.js$',
'parts' + DS + 'TouchPointer\.js$',
'parts' + DS + 'MSPointer\.js$',
'parts' + DS + 'Legend\.js$',
'parts' + DS + 'Chart\.js$',
'parts' + DS + 'Stacking\.js$',
'parts' + DS + 'Dynamics\.js$',
'parts' + DS + 'AreaSeries\.js$',
'parts' + DS + 'SplineSeries\.js$',
'parts' + DS + 'AreaSplineSeries\.js$',
'parts' + DS + 'ColumnSeries\.js$',
'parts' + DS + 'BarSeries\.js$',
'parts' + DS + 'ScatterSeries\.js$',
'parts' + DS + 'PieSeries\.js$',
'parts' + DS + 'DataLabels\.js$',
'modules' + DS + 'overlapping-datalabels.src\.js$',
'parts' + DS + 'Interaction\.js$',
'parts' + DS + 'Responsive\.js$',
'parts' + DS + 'Color\.js$',
'parts' + DS + 'Options\.js$',
'parts' + DS + 'PlotLineOrBand\.js$',
'parts' + DS + 'Tick\.js$',
'parts' + DS + 'Point\.js$',
'parts' + DS + 'Series\.js$',
'parts' + DS + 'Utilities\.js$'
]
};
// Modules should not be standalone, and they should exclude all parts files.
const fileOptions = B.getFilesInFolder(base, true, '')
.reduce((obj, file) => {
if (file.indexOf('modules') > -1 || file.indexOf('themes') > -1) {
obj[file] = {
exclude: new RegExp(folders.parts),
umd: false
};
}
return obj;
}, {});
/**
* Special cases
* solid-gauge should also exclude gauge-series
* highcharts-more and highcharts-3d is also not standalone.
*/
fileOptions['modules/solid-gauge.src.js'].exclude = new RegExp([folders.parts, 'GaugeSeries\.js$'].join('|'));
fileOptions['modules/map.src.js'].product = 'Highmaps';
fileOptions['modules/map-parser.src.js'].product = 'Highmaps';
fileOptions['modules/stock.src.js'].exclude = new RegExp(folders.highchartsFiles.join('|'));
Object.assign(fileOptions, {
'highcharts-more.src.js': {
exclude: new RegExp(folders.parts),
umd: false
},
'highcharts-3d.src.js': {
exclude: new RegExp(folders.parts),
umd: false
},
'highmaps.src.js': {
product: 'Highmaps'
},
'highstock.src.js': {
product: 'Highstock'
}
});
return fileOptions;
};
/**
* Gulp task to run the building process of distribution files. By default it builds all the distribution files. Usage: "gulp build".
* @param {string} --file Optional command line argument. Use to build a one or sevral files. Usage: "gulp build --file highcharts.js,modules/data.src.js"
* @return undefined
*/
const scripts = () => {
const build = require('./assembler/build').build;
// const argv = require('yargs').argv; Already declared in the upper scope
const files = (argv.file) ? argv.file.split(',') : null;
const type = (argv.type) ? argv.type : 'both';
const debug = argv.d || false;
const version = getProductVersion();
const base = './js/masters/';
const fileOptions = getFileOptions(base);
return build({
base: base,
debug: debug,
fileOptions: fileOptions,
files: files,
output: './code/',
type: type,
version: version
});
};
/**
* Creates a set of ES6-modules which is distributable.
* @return {undefined}
*/
const buildModules = () => {
const B = require('./assembler/build');
B.buildModules({
base: './js/',
output: './code/modules/',
type: 'both'
});
};
const styles = () => {
const sass = require('node-sass');
const U = require('./assembler/utilities.js');
const fileName = 'highcharts';
return new Promise((resolve, reject) => {
sass.render({
file: './css/' + fileName + '.scss',
outputStyle: 'expanded'
}, (err, result) => {
if (err) {
console.error(err);
reject(err);
} else {
U.writeFile('./code/css/' + fileName + '.css', result.css);
resolve();
}
});
});
};
/**
* Gulp task to execute ESLint. Pattern defaults to './js/**".'
* @parameter {string} -p Command line parameter to set pattern. To lint sample
* files, see the lintSamples function.
* @return undefined Returns nothing
*/
const lint = () => {
const CLIEngine = require('eslint').CLIEngine;
const cli = new CLIEngine();
const formatter = cli.getFormatter();
let pattern = (typeof argv.p === 'string') ? [argv.p] : ['./js/**/*.js'];
let report = cli.executeOnFiles(pattern);
console.log(formatter(report.results));
};
/**
* Gulp task to execute ESLint on samples.
* @parameter {string} -p Command line parameter to set pattern. Example usage
* gulp lint -p './samples/**'
* @return undefined Returns nothing
*/
const lintSamples = () => {
const CLIEngine = require('eslint').CLIEngine;
const cli = new CLIEngine({
ignorePattern: ['./samples/highcharts/common-js/*/demo.js']
});
const formatter = cli.getFormatter();
let report = cli.executeOnFiles([
'./samples/*/*/*/demo.js',
'./samples/*/*/*/test.js',
'./samples/*/*/*/unit-tests.js'
]);
console.log(formatter(report.results));
};
/**
* Watch changes to JS and SCSS files
*/
gulp.task('default', ['styles', 'scripts'], () => {
// If styling changes, then build new css and js files.
gulp.watch(['./css/*.scss'], ['styles', 'scripts']);
// If js parts files changes, then build new js files.
gulp.watch(['./js/!(adapters|builds)/*.js'], ['scripts']);
});
gulp.task('ftp', function () {
const ftp = require('vinyl-ftp');
const fs = require('fs');
const paths = {
buildsDir: './js/builds',
distributions: [
'./js/highcharts.src.js',
'./js/highmaps.src.js',
'./js/highstock.src.js',
'./js/highcharts-3d.src.js',
'./js/highcharts-more.src.js'
],
assemblies: [
'./js/highcharts.src.js',
'./js/highstock.src.js',
'./js/highcharts-3d.src.js',
'./js/highcharts-more.src.js',
'./js/highmaps.src.js',
'./js/modules/map.src.js',
'./js/modules/heatmap.src.js'
],
modules: ['./js/modules/*.js'],
parts: ['./js/parts/*.js'],
parts3D: ['./js/parts-3d/*.js'],
partsMap: ['./js/parts-map/*.js'],
partsMore: ['./js/parts-more/*.js'],
partsGantt: ['./js/parts-gantt/*.js'],
themes: ['./js/themes/*.js']
};
fs.readFile('./git-ignore-me.properties', 'utf8', function (err, lines) {
if (err) {
throw err;
}
let config = {};
lines.split('\n').forEach(function (line) {
line = line.split('=');
if (line[0]) {
config[line[0]] = line[1];
}
});
let conn = ftp.create({
host: config['ftp.host'],
user: config['ftp.user'],
password: config['ftp.password']
});
let globs = paths.distributions.concat(paths.modules);
return gulp.src(globs, { base: './js', buffer: false })
.pipe(conn.newer(config['ftp.dest']))
.pipe(conn.dest(config['ftp.dest']));
});
});
gulp.task('ftp-watch', function () {
gulp.watch('./js/*/*.js', ['scripts', 'ftp']);
});
/**
* Run the test suite. The task spawns a child process running PhantomJS.
*/
gulp.task('test', function () {
const spawn = require('child_process').spawn;
spawn('phantomjs', ['phantomtest.js'].concat(process.argv.slice(3)), {
cwd: 'utils/samples',
stdio: 'inherit'
});
});
/**
* Run the nightly. The task spawns a child process running node.
*/
gulp.task('nightly', function () {
const spawn = require('child_process').spawn;
spawn('node', ['nightly.js'].concat(process.argv.slice(3)), {
cwd: 'utils/samples',
stdio: 'inherit'
});
});
/**
* Automated generation for internal API docs.
* Run with --watch argument to watch for changes in the JS files.
*/
gulp.task('jsdoc', function (cb) {
const jsdoc = require('gulp-jsdoc3');
const templateDir = './../highcharts-docstrap';
gulp.src([
'README.md',
'./js/parts/Utilities.js',
'./js/parts/Axis.js',
'./js/parts/Chart.js',
'./js/parts/Dynamics.js',
'./js/parts/Globals.js',
'./js/parts/Options.js',
'./js/parts/PlotLineOrBand.js',
'./js/modules/drilldown.src.js',
'./js/modules/exporting.src.js',
'./js/modules/offline-exporting.src.js'
], { read: false })
// gulp.src(['README.md', './js/parts/Options.js'], { read: false })
.pipe(jsdoc({
navOptions: {
theme: 'highsoft'
},
opts: {
destination: './internal-docs/',
private: false,
template: templateDir + '/template'
},
plugins: [
templateDir + '/plugins/markdown',
// templateDir + '/plugins/optiontag',
templateDir + '/plugins/sampletag'
],
templates: {
logoFile: 'img/highcharts-logo.svg',
systemName: 'Highcharts',
theme: 'highsoft'
}
}, function (err) {
cb(err); // eslint-disable-line
if (!err) {
console.log(
colors.green('Wrote JSDoc to ./internal-docs/index.html')
);
}
}));
if (argv.watch) {
gulp.watch(['./js/!(adapters|builds)/*.js'], ['jsdoc']);
}
});
const compile = (files, sourceFolder) => {
const createSourceMap = true;
console.log(colors.yellow('Warning: This task may take a few minutes on Mac, and even longer on Windows.'));
return new Promise((resolve) => {
files.forEach(path => {
const closureCompiler = require('google-closure-compiler-js');
// const fs = require('fs');
const U = require('./assembler/utilities.js');
const sourcePath = sourceFolder + path;
const outputPath = sourcePath.replace('.src.js', '.js');
const src = U.getFile(sourcePath);
const out = closureCompiler.compile({
compilationLevel: 'SIMPLE_OPTIMIZATIONS',
jsCode: [{
src: src
}],
languageIn: 'ES5',
languageOut: 'ES5',
createSourceMap: createSourceMap
});
U.writeFile(outputPath, out.compiledCode);
if (createSourceMap) {
U.writeFile(outputPath + '.map', out.sourceMap);
}
// @todo add filesize information
console.log(colors.green('Compiled ' + sourcePath + ' => ' + outputPath));
});
resolve('Compile is complete');
});
};
/**
* Compile the JS files in the /code folder
*/
const compileScripts = () => {
const B = require('./assembler/build.js');
const sourceFolder = './code/';
const files = B.getFilesInFolder(sourceFolder, true, '').filter(path => path.endsWith('.src.js'));
return compile(files, sourceFolder)
.then(console.log)
.catch(console.log);
};
/**
* Compile the JS files in the /code folder
*/
const compileLib = () => {
const sourceFolder = './vendor/';
const files = ['canvg.src.js', 'rgbcolor.src.js'];
return compile(files, sourceFolder)
.then(console.log)
.catch(console.log);
};
const cleanCode = () => {
const B = require('./assembler/build.js');
const U = require('./assembler/utilities.js');
const codeFolder = './code/';
const files = B.getFilesInFolder(codeFolder, true, '');
const keep = ['.gitignore', '.htaccess', 'css/readme.md', 'js/modules/readme.md', 'js/readme.md', 'modules/readme.md', 'readme.txt'];
const promises = files
.filter(file => keep.indexOf(file) === -1)
.map(file => U.removeFile(codeFolder + file));
return Promise.all(promises)
.then(() => console.log('Successfully removed code directory.'));
};
const cleanDist = () => {
const U = require('./assembler/utilities.js');
return U.removeDirectory('./build/dist').then(() => {
console.log('Successfully removed dist directory.');
}).catch(console.log);
};
const copyToDist = () => {
const fs = require('fs');
const B = require('./assembler/build.js');
const U = require('./assembler/utilities.js');
const sourceFolder = './code/';
const libFolder = './vendor/';
const distFolder = './build/dist/';
const files = B.getFilesInFolder(sourceFolder, true, '');
// Files that should not be distributed with certain products
const filter = {
highcharts: ['highmaps.js', 'highstock.js', 'modules/canvasrenderer.experimental.js', 'modules/map.js', 'modules/map-parser.js'],
highstock: ['highcharts.js', 'highmaps.js', 'modules/broken-axis.js', 'modules/canvasrenderer.experimental.js', 'modules/map.js', 'modules/map-parser.js'],
highmaps: ['highstock.js', 'modules/broken-axis.js', 'modules/canvasrenderer.experimental.js', 'modules/map-parser.js', 'modules/series-label.js', 'modules/solid-gauge.js']
};
// Copy source files to the distribution packages.
files.filter((path) => (
path.endsWith('.js') ||
path.endsWith('.js.map') ||
path.endsWith('.css')
))
.forEach((path) => {
const content = fs.readFileSync(sourceFolder + path);
const filename = path.replace('.src.js', '.js').replace('js/', '');
['highcharts', 'highstock', 'highmaps'].forEach((lib) => {
if (filter[lib].indexOf(filename) === -1) {
U.writeFile(distFolder + lib + '/code/' + path, content);
}
});
});
// Copy readme to distribution packages
['readme.txt'].forEach((path) => {
const content = fs.readFileSync(sourceFolder + path);
['highcharts', 'highstock', 'highmaps'].forEach((lib) => {
U.writeFile(distFolder + lib + '/code/' + path, content);
});
});
// Copy lib files to the distribution packages. These files are used in the
// offline-export.
[
'canvg.js',
'canvg.src.js',
'jspdf.js',
'jspdf.src.js',
'rgbcolor.js',
'rgbcolor.src.js',
'svg2pdf.js',
'svg2pdf.src.js'
].forEach((path) => {
const content = fs.readFileSync(libFolder + path);
['highcharts', 'highstock', 'highmaps'].forEach((lib) => {
U.writeFile(distFolder + lib + '/code/lib/' + path, content);
});
});
// Copy radial gradient to dist.
['vml-radial-gradient.png'].forEach((path) => {
const content = fs.readFileSync('./gfx/' + path);
['highcharts', 'highstock', 'highmaps'].forEach((lib) => {
U.writeFile(distFolder + lib + '/gfx/' + path, content);
});
});
};
const createProductJS = () => {
const U = require('./assembler/utilities.js');
const D = require('./assembler/dependencies.js');
const path = './build/dist/products.js';
// @todo Get rid of build.properties and perhaps use package.json in stead.
const buildProperties = U.getFile('./build.properties');
let date = D.regexGetCapture(/highcharts\.product\.date=(.+)/, buildProperties);
let version = D.regexGetCapture(/highcharts\.product\.version=(.+)/, buildProperties);
// @todo Add reasonable defaults
date = date === null ? '' : date;
version = version === null ? '' : version;
const content = `var products = {
"Highcharts": {
"date": "${date}",
"nr": "${version}"
},
"Highstock": {
"date": "${date}",
"nr": "${version}"
},
"Highmaps": {
"date": "${date}",
"nr": "${version}"
}
}`;
U.writeFile(path, content);
};
/**
* Left pad a string
* @param {string} str The string we want to pad.
* @param {string} char The character we want it to be padded with.
* @param {number} length The length of the resulting string.
* @return {string} The string with padding on left.
*/
const leftPad = (str, char, length) => char.repeat(length - str.length) + str;
/**
* Returns time of date as a string in the format of HH:MM:SS
* @param {Date} d The date object we want to get the time from
* @return {string} The string represantation of the Date object.
*/
const toTimeString = (d) => {
const pad = (s) => leftPad(s, '0', 2);
return pad('' + d.getHours()) + ':' + pad('' + d.getMinutes()) + ':' + pad('' + d.getSeconds());
};
/**
* Returns a string which tells the time difference between to dates.
* Difference is formatted as xh xm xs xms. Where x is a number.
* @param {Date} d1 First date
* @param {Date} d2 Second date
* @return {string} The time difference between the two dates.
*/
const timeDifference = (d1, d2) => {
const seconds = 1000;
const minutes = 60 * seconds;
const hours = 60 * minutes;
let diff = d2 - d1;
let x = 0;
let time = [];
if (diff > hours) {
x = Math.floor(diff / hours);
diff -= x * hours;
time.push(x + 'h');
}
if (diff > minutes || (time.length > 0 && diff > 0)) {
x = Math.floor(diff / minutes);
diff -= x * minutes;
time.push(x + 'm');
}
if (diff > seconds || (time.length > 0 && diff > 0)) {
x = Math.floor(diff / seconds);
diff -= x * seconds;
time.push(x + 's');
}
if (diff > 0 || time.length === 0) {
time.push(diff + 'ms');
}
return time.join(' ');
};
/**
* Mirrors the same feedback which gulp gives when executing its tasks.
* Says when a task started, when it finished, and how long it took.
* @param {string} name Name of task which is beeing executed.
* @param {string} task A function to execute
* @return {*} Returns whatever the task function returns when it is finished.
*/
const gulpify = (name, task) => {
// const colors = require('colors');
const isPromise = (value) => (typeof value === 'object' && typeof value.then === 'function');
return function () {
const d1 = new Date();
console.log('[' + colors.gray(toTimeString(d1)) + '] Starting \'' + colors.cyan(name) + '\'...');
let result = task.apply(null, Array.from(arguments));
if (!isPromise(result)) {
result = Promise.resolve(result);
}
return result.then(() => {
const d2 = new Date();
console.log('[' + colors.gray(toTimeString(d2)) + '] Finished \'' + colors.cyan(name) + '\' after ' + colors.blue(timeDifference(d1, d2)));
});
};
};
/**
* Executes a single terminal command and returns when finished.
* Outputs stdout to the console.
* @param {string} command Command to execute in terminal
* @return {string} Returns all output to the terminal in the form of a string.
*/
const commandLine = (command) => {
const exec = require('child_process').exec;
return new Promise((resolve, reject) => {
const cli = exec(command, (error, stdout) => {
if (error) {
console.log(error);
reject(error);
} else {
console.log('Command finished: ' + command);
resolve(stdout);
}
});
cli.stdout.on('data', (data) => console.log(data.toString()));
});
};
const filesize = () => {
const sourceFolder = './code/';
// @todo Correct type names to classic and styled
const types = argv.type ? [argv.type] : ['classic', 'css'];
const filenames = argv.file ? argv.file.split(',') : ['highcharts.src.js'];
const files = filenames.reduce((arr, name) => {
const p = types.map(t => (t === 'css' ? 'js/' : '') + name);
return arr.concat(p);
}, []);
const getGzipSize = (content) => {
const gzipSize = require('gzip-size');
return gzipSize.sync(content);
};
// const pad = (str, x) => ' '.repeat(x) + str;
const padRight = (str, x) => str + ' '.repeat(x - str.length);
const printRow = (sizes, content) => content.map((c, i) => padRight(c.toString(), sizes[i])).join('');
const report = (name, current, head) => {
const colsizes = [10, 10, 10, 10];
const diff = (a, b) => {
const d = a - b;
const sign = d > 0 ? '+' : '';
// const color = diff > 0 ? 'yellow' : 'green';
return sign + d;
};
console.log([
'',
colors.cyan(name),
printRow(colsizes, ['', 'gzipped', 'compiled', 'size']),
printRow(colsizes, ['New:', current.gzip, current.compiled, current.size]),
printRow(colsizes, ['HEAD:', head.gzip, head.compiled, head.size]),
printRow(colsizes, ['Diff:', diff(current.gzip, head.gzip) + 'B', diff(current.compiled, head.compiled) + 'B', diff(current.size, head.size) + 'B']),
''
].join('\n'));
};
const runFileSize = (obj, key) => {
return Promise.resolve(scripts())
.then(() => compile(files, sourceFolder))
.then(() => {
return files.reduce((o, n) => {
const getFile = require('./assembler/utilities.js').getFile;
const filename = n.replace('.src.js', '.js');
const compiled = getFile(sourceFolder + filename);
const content = getFile(sourceFolder + n);
if (!o[filename]) {
o[filename] = {};
}
o[filename][key] = {
gzip: getGzipSize(compiled),
size: content.length,
compiled: compiled.length
};
return o;
}, obj);
});
};
return runFileSize({}, 'new')
.then((obj) => {
return commandLine('git stash')
.then(() => obj); // Pass obj to next function
})
.then((obj) => runFileSize(obj, 'head'))
.then((obj) => {
return commandLine('git stash apply && git stash drop')
.then(() => obj); // Pass obj to next function
})
.then((obj) => {
const keys = Object.keys(obj);
keys.forEach((key) => {
const values = obj[key];
report(key, values.new, values.head);
});
})
.catch(console.log);
};
/**
* Download a version of the API for Highstock, Highstock or Highmaps.
* Executes a grunt task through command line.
* @param {string} product Which api to download. Must be lowercase.
* @param {string} version Which version to download.
* @return {Promise} Returns a promise which resolves when download is completed.
*/
const downloadAPI = (product, version) => commandLine('grunt download-api:' + product + ':' + version);
/**
* Download all the API's of Highcharts, Highstock and Highmaps.
* @return {Promise} Returns a promise which resolves when all downloads are completed.
*/
const downloadAllAPI = () => new Promise((resolve, reject) => {
// @todo Pass in version, instead of hardcoding it.
const version = getProductVersion();
const promises = ['highcharts', 'highstock', 'highmaps'].map((product) => downloadAPI(product, version));
Promise.all(promises).then(() => {
resolve('Finished downloading api\'s for Highcharts, Highstock and Highmaps');
}).catch((err) => {
reject(err);
});
});
/**
* Run remaining dist tasks in build.xml.
* @return {Promise} Returns a promise which resolves when scripts is finished.
*/
const antDist = () => commandLine('ant dist');
/**
* Gzip a single file.
* @param {string} file Path to input file.
* @param {string} output Path to where output the result.
* @return {undefined}
* TODO Promisify to use in dist task.
*/
const gzipFile = (file, output) => {
const zlib = require('zlib');
const fs = require('fs');
const gzip = zlib.createGzip();
const inp = fs.createReadStream(file);
const out = fs.createWriteStream(output);
inp.pipe(gzip).pipe(out);
};
const getDirectories = (path) => {
const fs = require('fs');
return fs.readdirSync(path).filter(file => fs.lstatSync(path + file).isDirectory());
};
const replaceAll = (str, search, replace) => str.split(search).join(replace);
const assembleSample = (template, content) => {
return Object.keys(content).reduce((str, key) => {
return str.replace('@demo.' + key + '@', content[key]);
}, template);
};
const createExamples = (title, samplesFolder, output) => {
const U = require('./assembler/utilities.js');
const getFile = U.getFile;
const writeFile = U.writeFile;
const template = getFile('samples/template-example.htm');
const samples = getDirectories(samplesFolder);
const convertURLToLocal = str => {
const stock = 'src="https://code.highcharts.com/stock/';
const maps = 'src="https://code.highcharts.com/maps/';
const chart = 'src="https://code.highcharts.com/';
const mapdata = 'src="https://code.highcharts.com/mapdata';
const localPath = 'src="../../code/';
str = replaceAll(str, stock, localPath);
str = replaceAll(str, maps, localPath);
str = replaceAll(str, chart, localPath);
str = replaceAll(str, '../../js/mapdata', mapdata);
return str;
};
samples.forEach((name) => {
const folder = samplesFolder + name + '/';
const contents = ['html', 'css', 'js'].reduce((obj, key) => {
let content = getFile(folder + 'demo.' + key);
obj[key] = content ? content : '';
return obj;
}, {});
contents.title = title;
let sample = assembleSample(template, contents);
sample = convertURLToLocal(sample);
writeFile(output + name + '/index.htm', sample);
});
const index = getFile(samplesFolder + 'index.htm');
writeFile(output + '../index.htm', index);
};
const copyFile = (source, target) => new Promise((resolve, reject) => {
const fs = require('fs');
const U = require('./assembler/utilities.js');
const directory = U.folder(target);
U.createDirectory(directory);
let read = fs.createReadStream(source);
let write = fs.createWriteStream(target);
const onError = (err) => {
read.destroy();
write.end();
reject(err);
};
read.on('error', onError);
write.on('error', onError);
write.on('finish', resolve);
read.pipe(write);
});
const copyFolder = (input, output) => {
const getFilesInFolder = require('./assembler/build.js').getFilesInFolder;
const files = getFilesInFolder(input);
const promises = files.map(file => copyFile(input + file, output + file));
return Promise.all(promises)
.then(() => console.log('Copied folder ' + input + ' to ' + output));
};
const copyGraphicsToDist = () => {
const dist = 'build/dist/';
const promises = ['highcharts', 'highstock', 'highmaps'].map((lib) => {
return copyFolder('samples/graphics/', dist + lib + '/graphics/');
});
return Promise.all(promises)
.then(() => console.log('Copied all graphics to dist folders.'));
};
const createAllExamples = () => new Promise((resolve) => {
const config = {
'Highcharts': {
samplesFolder: 'samples/highcharts/demo/',
output: 'build/dist/highcharts/examples/'
},
'Highstock': {
samplesFolder: 'samples/stock/demo/',
output: 'build/dist/highstock/examples/'
},
'Highmaps': {
samplesFolder: 'samples/maps/demo/',
output: 'build/dist/highmaps/examples/'
}
};
Object.keys(config).forEach(lib => {
const c = config[lib];
createExamples(lib, c.samplesFolder, c.output);
});
resolve();
});
gulp.task('create-productjs', createProductJS);
gulp.task('clean-dist', cleanDist);
gulp.task('clean-code', cleanCode);
gulp.task('copy-to-dist', copyToDist);
gulp.task('filesize', filesize);
gulp.task('styles', styles);
gulp.task('scripts', scripts);
gulp.task('build-modules', buildModules);
gulp.task('lint', lint);
gulp.task('lint-samples', lintSamples);
gulp.task('compile', compileScripts);
gulp.task('compile-lib', compileLib);
gulp.task('download-api', downloadAllAPI);
gulp.task('copy-graphics-to-dist', copyGraphicsToDist);
gulp.task('examples', createAllExamples);
/**
* Create distribution files
*/
gulp.task('dist', () => {
return Promise.resolve()
.then(gulpify('cleanCode', cleanCode))
.then(gulpify('styles', styles))
.then(gulpify('scripts', scripts))
.then(gulpify('lint', lint))
.then(gulpify('compile', compileScripts))
.then(gulpify('cleanDist', cleanDist))
.then(gulpify('copyToDist', copyToDist))
.then(gulpify('downloadAllAPI', downloadAllAPI))
.then(gulpify('createProductJS', createProductJS))
.then(gulpify('createExamples', createAllExamples))
.then(gulpify('copyGraphicsToDist', copyGraphicsToDist))
.then(gulpify('ant-dist', antDist));
});
gulp.task('browserify', function () {
const fs = require('fs');
const browserify = require('browserify');
browserify('./samples/highcharts/common-js/browserify/app.js')
.bundle(function (err, buf) {
if (err) {
// @todo Do something meaningful with err
}
fs.writeFileSync('./samples/highcharts/common-js/browserify/demo.js', buf);
});
});
gulp.task('webpack', function () {
const webpack = require('webpack');
webpack({
entry: './samples/highcharts/common-js/browserify/app.js', // Share the same unit tests
output: {
filename: './samples/highcharts/common-js/webpack/demo.js'
}
}, function (err) {
if (err) {
throw new Error('Webpack failed.');
}
});
});
gulp.task('common', ['scripts', 'browserify', 'webpack']);