forked from Mikhus/canvas-gauges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
589 lines (504 loc) · 16.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
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
/**
* Canv-Gauge Dev Tasks Runner
*
* @author Mykhailo Stadnyk <[email protected]>
*/
const gulp = require('gulp');
const usage = require('gulp-help-doc');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const rename = require('gulp-rename');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const minify = require('gulp-uglify/minifier');
const uglify6 = require('uglify-js-harmony');
const rimraf = require('rimraf');
const esdoc = require('gulp-esdoc');
const eslint = require('gulp-eslint');
const gzip = require('gulp-gzip');
const KarmaServer = require('karma').Server;
const gutil = require('gulp-util');
const chalk = require('chalk');
const http = require('https');
const fs = require('fs');
const selenium = require('selenium-standalone');
const mocha = require('gulp-mocha');
const cp = require('child_process');
const concat = require('gulp-concat');
const yargs = require('yargs');
const replace = require('gulp-replace');
const babel = require('gulp-babel');
const fsc = require('fs-cli');
const semver = require('semver');
const inject = require('gulp-inject-string');
const version = require('./package.json').version;
const plato = require('gulp-plato');
/**
* @typedef {{argv: object}} yargs
* @typedef {{parse:function, stringify:function}} JSON
*/
let types = ['all', 'radial', 'linear'];
function es6concat(type = 'all', clean = false) {
let bundle = [
'lib/polyfill.js',
'lib/vendorize.js',
'lib/EventEmitter.js',
'lib/Animation.js',
'lib/DomObserver.js',
'lib/SmartCanvas.js',
'lib/GenericOptions.js',
'lib/Collection.js',
'lib/BaseGauge.js',
'lib/drawings.js'
];
if (clean) bundle.shift();
switch (type.toLowerCase()) {
case 'radial':
case 'radial-gauge':
case 'radialgauge':
bundle.push('lib/RadialGauge.js');
break;
case 'linear':
case 'linear-gauge':
case 'lineargauge':
bundle.push('lib/LinearGauge.js');
break;
default:
bundle.push('lib/RadialGauge.js', 'lib/LinearGauge.js');
}
return gulp.src(bundle)
.pipe(concat('gauge.es6.js'))
.pipe(replace(/((var|const|let)\s+.*?=\s*)?require\(.*?\);?/g, ''))
.pipe(replace(/(module\.)?exports(.default)?\s+=\s*.*?\r?\n/g, ''))
.pipe(replace(/export\s+(default\s+)?(GenericOptions;)?/g, ''))
.pipe(replace(/%VERSION%/g, version));
}
function wrap(stream) {
return stream.pipe(replace(/^/, license() + '(function(ns) {'))
.pipe(replace(/$/,
';typeof module !== "undefined" && Object.assign(ns, {' +
'Collection: Collection,' +
'GenericOptions: GenericOptions,' +
'Animation: Animation,' +
'BaseGauge: BaseGauge,' +
'drawings: drawings,' +
'SmartCanvas: SmartCanvas,' +
'vendorize: vendorize' +
'});' +
'}(typeof module !== "undefined" ? ' +
'module.exports : window));'));
}
function es5transpile(type = 'all', withSourceMaps = true, resolve = () => {}) {
let stream = wrap(es6concat(type)
.pipe(rename('gauge.es5.js'))
.pipe(babel({
presets: ['es2015'],
compact: false
}))
.on('error', function(err) {
gutil.log(err);
this.emit('end');
resolve();
})
.pipe(rename('gauge.min.js')));
if (withSourceMaps) {
stream = stream.pipe(sourcemaps.init({ loadMaps: true }));
}
stream = stream.pipe(uglify({
preserveComments: (node, comment) => comment.line === 1
}));
if (withSourceMaps) {
stream = stream.pipe(sourcemaps.write('.'));
}
return stream;
}
function license() {
let src = fs.readFileSync('./LICENSE')
.toString()
.split(/\r?\n/);
src.pop();
return '/*!\n * ' + src.join('\n * ') + '\n *\n * @version ' +
version + '\n */\n';
}
/**
* Builds code complexity report
*
* @task {complexity}
*/
gulp.task('complexity', done => {
rimraf('complexity', () => gulp.src('lib/**/*.js')
.pipe(plato('complexity', {
complexity: { trycatch: true },
jshint: {
options: {
strict: false,
browser: true,
browserify: true,
devel: true,
expr: true,
esversion: 6,
laxbreak: true,
laxcomma: true,
sub: true
}
}
}))
.on('finish', () => done())
);
});
/**
* Displays this usage information.
*
* @task {help}
*/
gulp.task('help', () => usage(gulp, { emptyLineBetweenTasks: false }));
/**
* Builds production packages
*
* @task {build:prod}
* @order {5}
*/
gulp.task('build:prod', done => {
rimraf('dist', () => {
Promise.all(types.map(type => {
return new Promise(resolve => {
es5transpile(type, false, resolve)
.pipe(gulp.dest('dist/' + type))
.on('end', () => {
let pkg = JSON.parse(fs.readFileSync('./package.json'));
delete pkg.devDependencies;
delete pkg.scripts;
delete pkg.directories;
if (type !== 'all') {
pkg.version += '-' + type;
pkg.keywords.push(type + '-gauge');
}
else {
for (let i = 1; i < types.length; i++) {
pkg.keywords.push(types[i] + '-gauge');
}
}
fs.writeFileSync('dist/' + type + '/package.json',
JSON.stringify(pkg, null, 2));
fs.writeFileSync('dist/' + type + '/README.md',
fs.readFileSync('README.md'));
fs.writeFileSync('dist/' + type + '/LICENSE',
fs.readFileSync('LICENSE'));
resolve();
});
});
})).then(() => {
let cmd = '';
console.log(chalk.bold.green('Production packages are now ready!'));
console.log('To publish each production package, please run the ' +
'following command:');
types.reverse().forEach(type => {
let v = version;
let entry = type === 'linear' ? './' : '../../';
if (cmd) cmd += ' && ';
cmd += 'cd ' + entry + 'dist/' + type;
if (type === 'all') type = 'latest';
else v += '-' + type;
cmd += ' && npm publish';
if (type !== 'latest')
cmd += ' && npm dist-tag add canvas-gauges@' + v + ' ' +
type;
});
console.log(chalk.grey(cmd));
let dst = '../canvas-gauges-pages/download';
fsc.rm(dst + '/' + version);
fsc.cp('dist', dst + '/' + version, true);
fsc.rm(dst + '/latest');
let releases = fsc.ls(dst);
let latest = semver.maxSatisfying(releases, '*');
fsc.cp(dst + '/' + latest, dst + '/latest');
let info = {};
releases.forEach(release => {
info[release] = {
name: release
};
types.forEach(type => {
info[release][type] = {
bytes: fs.statSync(dst + '/' + release + '/' + type +
'/gauge.min.js').size
};
info[release][type].kb = (info[release][type].bytes / 1024)
.toFixed(1);
info[release][type].mb = (info[release][type].bytes /
(1024 * 1024)).toFixed(1);
info[release][type].gb = (info[release][type].bytes /
(1024 * 1024 * 1024)).toFixed(1);
});
});
info.latest = JSON.parse(JSON.stringify(info[latest]));
fs.writeFileSync('../canvas-gauges-pages/_data/releases.json',
JSON.stringify(info, null, 2));
done();
});
});
});
/**
* Currently there is no way to minify es6 code, so this task is
* temporally useless. Awaiting for support of es6 in minifiers.
*
* @task {build:es6}
* @arg {type} build type: 'radial' - Gauge object only, 'linear' - LinearGauge object only, 'all' - everything (default)
* @order {4}
*/
gulp.task('build:es6', done => {
es6concat(yargs.argv.type || 'all', true)
// .pipe(minify({
// preserveComments: (node, comment) => comment.line === 1
// }, uglify6))
.pipe(gulp.dest('.'))
.on('end', () => done());
});
/**
* Builds and minifies es5 code
*
* @task {build:es5}
* @arg {type} build type: 'radial' - Gauge object only, 'linear' - LinearGauge object only, 'all' - everything (default)
* @order {3}
*/
gulp.task('build:es5', ['clean'], done => {
es5transpile(yargs.argv.type || 'all')
.pipe(gulp.dest('.'))
.on('end', () => {
if (!process.env.TRVIS) {
fs.writeFileSync(
'../canvas-gauges-pages/' +
'assets/js/gauge.min.js',
fs.readFileSync('gauge.min.js')
);
fs.writeFileSync(
'../canvas-gauges-pages/' +
'assets/js/gauge.min.js.map',
fs.readFileSync('gauge.min.js.map')
);
}
done();
});
});
/**
* Clean-ups files from previous build.
*
* @task {clean}
* @order {1}
*/
gulp.task('clean', done => {
rimraf('gauge.js', () =>
rimraf('gauge.min.js', () =>
rimraf('gauge.min.js.map', () =>
rimraf('gauge.min.js.gz', done))));
});
/**
* Cleans docs directory
*
* @task {clean:docs}
* @order {6}
*/
gulp.task('clean:docs', done => {
rimraf('docs', done);
});
/**
* Automatically generates API documentation for this project
* from a source code.
*
* @task {doc}
* @order {7}
*/
gulp.task('doc', ['clean:docs'], done => {
gulp.src('./lib')
.pipe(esdoc({
destination: './docs',
package: './package.json',
title: 'HTML5 Canvas Gauges API Documentation',
styles: [
'./assets/styles/docs.css',
]
}))
.on('finish', () => {
if (process.env.TRAVIS) {
return ;
}
console.log(chalk.bold.green('Generating badge...'));
let coverageDataFile = './docs/coverage.json';
let coverage = require(coverageDataFile).coverage;
let color = 'green';
if (parseFloat(coverage) < 90) {
color = 'red';
}
let url = 'https://img.shields.io/badge/docs-' +
coverage.replace(/%/g, '%25') +
'-' + color + '.svg';
http.get(url, response => {
if ((response instanceof Error)) {
return console.error(response);
}
response.pipe(fs.createWriteStream('docs-coverage.svg'));
});
//move to pages
let target = '../canvas-gauges-pages/docs/' + version;
rimraf(target, () => fs.rename('docs', target, done));
});
});
/**
* Transpiles, combines and minifies JavaScript code.
*
* @task {build}
* @arg {target} compile target, could be 'es5' (default) or 'es6'
* @arg {type} build type: 'radial' - Gauge object only, 'linear' - LinearGauge object only, 'all' - everything (default)
* @order {2}
*/
gulp.task('build', () => {
//noinspection JSUnresolvedVariable
gulp.start('build:' + (yargs.argv.target || 'es5'));
});
/**
* Watch for source code changes and automatically re-build
* when wny of them detected.
*
* @task {watch}
*/
gulp.task('watch', ['build'], () => {
gulp.watch('lib/**/*.js', () => gulp.start('build'));
});
/**
* Runs gzipping for minified file.
*
* @task {gzip}
*/
gulp.task('gzip', ['build:prod'], done => {
Promise.all(types.map(type => new Promise(resolve => {
gulp.src('dist/' + type + '/gauge.min.js')
.pipe(gzip({ gzipOptions: { level: 9 } }))
.pipe(gulp.dest('dist/' + type))
.on('end', resolve);
}))).then(() => done());
});
/**
* Performs JavaScript syntax check.
*
* @task {lint}
*/
gulp.task('lint', () => {
console.log(chalk.bold.green('\nStarting linting checks...\n'));
return gulp.src([
'**/*.js',
'!node_modules/**',
'!docs/**',
'!**.min.js',
'!coverage/**',
'!complexity/**',
'!dist/**',
'!test/cjs/**'
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
/**
* Runs unit tests.
*
* @task {test:spec}
*/
gulp.task('test:spec', done => {
console.log(chalk.bold.green('Starting unit tests...'));
new KarmaServer({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, exitCode => {
if (!exitCode) {
if (process.env.TRAVIS) {
return done();
}
console.log(chalk.bold.green('Generating badge...'));
// create badges
let rx = new RegExp(
'[\u001b\u009b][[()#;?]*' +
'(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?' +
'[0-9A-ORZcf-nqry=><]', 'g');
let coverage = fs.readFileSync(
'./coverage/report/coverage.txt',
{ encoding: 'utf8' }
)
.replace(rx, '')
.split(/\r?\n/)[2]
.split(':')[1]
.split('(')[0]
.trim();
let color = 'green';
if (parseFloat(coverage) < 90) {
color = 'red';
}
let url = 'https://img.shields.io/badge/coverage-' +
coverage.replace(/%/g, '%25') +
'-' + color + '.svg';
http.get(url, response => {
if ((response instanceof Error)) {
console.error(response);
process.exit(0);
}
response
.pipe(fs.createWriteStream('test-coverage.svg'))
.on('finish', () => process.exit(0));
});
return done();
}
process.exit(exitCode);
}).start();
});
/**
* Installs and starts selenium server
*
* @task {selenium}
*/
gulp.task('selenium', done => {
cp.execSync('pkill -f selenium-standalone');
selenium.install({ logger: gutil.log }, err => {
if (err) return done(err);
selenium.start((err, child) => {
if (err) return done(err);
selenium.child = child;
done();
});
});
});
/**
* Runs end-to-end tests.
*
* @task {test:e2e}
*
* Testing concept could be following:
* 1. Create various gauge configuration HTML pages
* 2. Use protractor to navigate this pages
* 3. On each page, depending on the gauge config and screen res/dpi
* checking the proper pixels on canvas if them matching expected color
* This could be checks, for example if highlight area in this pixel is
* present or not, if arrow starts/ends at this point, etc.
* This would be valuable checks, because if something wrong happen with
* drawing, most probably expected pixels won't match the expected specs,
* so we can automatically figure something is broken.
*/
gulp.task('test:e2e', ['selenium'], done => {
console.log(chalk.bold.green('\nStarting end-to-end tests...\n'));
gulp.src(['test/e2e/**/*.e2e.js'])
.pipe(mocha({ reporter: 'spec' }))
.once('error', err => {
console.error(err);
cp.execSync('pkill -f selenium-standalone');
process.exit(1);
})
.once('end', () => {
cp.execSync('pkill -f selenium-standalone');
done();
});
});
/**
* Runs all tests and checks.
*
* @task {test}
*/
gulp.task('test', ['lint', 'test:spec', 'test:e2e']);
gulp.task('default', ['help']);