2020-02-19 20:24:16 +01:00
|
|
|
/* eslint-disable import/no-nodejs-modules, import/no-commonjs, no-use-before-define */
|
2020-02-08 00:03:33 +01:00
|
|
|
const gulp = require('gulp');
|
|
|
|
const eslint = require('gulp-eslint');
|
|
|
|
const file = require('gulp-file');
|
|
|
|
const replace = require('gulp-replace');
|
|
|
|
const size = require('gulp-size');
|
|
|
|
const streamify = require('gulp-streamify');
|
|
|
|
const terser = require('gulp-terser');
|
|
|
|
const zip = require('gulp-zip');
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
const karma = require('karma');
|
|
|
|
const merge = require('merge-stream');
|
|
|
|
const yargs = require('yargs');
|
|
|
|
const path = require('path');
|
|
|
|
const htmllint = require('gulp-htmllint');
|
|
|
|
const typescript = require('gulp-typescript');
|
2020-02-09 17:50:11 +01:00
|
|
|
const typedoc = require('gulp-typedoc');
|
2020-02-08 00:03:33 +01:00
|
|
|
|
|
|
|
const pkg = require('./package.json');
|
|
|
|
const tsProject = typescript.createProject('./tsconfig.json');
|
|
|
|
|
|
|
|
const argv = yargs
|
2020-02-09 17:50:11 +01:00
|
|
|
.option('verbose', {default: false})
|
|
|
|
.argv;
|
2018-01-13 18:17:38 +01:00
|
|
|
|
2020-02-08 00:03:33 +01:00
|
|
|
const srcDir = './src/';
|
|
|
|
const outDir = './dist/';
|
2016-05-24 15:02:02 +02:00
|
|
|
|
2016-08-25 21:13:27 +02:00
|
|
|
gulp.task('bower', bowerTask);
|
2015-08-29 00:58:23 +02:00
|
|
|
gulp.task('build', buildTask);
|
2016-05-15 01:19:57 +02:00
|
|
|
gulp.task('package', packageTask);
|
2018-02-03 13:28:54 +01:00
|
|
|
gulp.task('lint-html', lintHtmlTask);
|
|
|
|
gulp.task('lint-js', lintJsTask);
|
2018-11-18 09:33:34 +01:00
|
|
|
gulp.task('lint', gulp.parallel('lint-html', 'lint-js'));
|
2020-02-08 00:03:33 +01:00
|
|
|
gulp.task('tsc', typescriptTask);
|
2017-03-21 01:36:54 +01:00
|
|
|
gulp.task('docs', docsTask);
|
2015-08-29 00:58:23 +02:00
|
|
|
gulp.task('unittest', unittestTask);
|
2020-02-08 00:03:33 +01:00
|
|
|
gulp.task('test', gulp.parallel('lint', 'tsc', 'unittest'));
|
2015-08-29 00:58:23 +02:00
|
|
|
gulp.task('library-size', librarySizeTask);
|
|
|
|
gulp.task('module-sizes', moduleSizesTask);
|
2018-11-18 09:33:34 +01:00
|
|
|
gulp.task('size', gulp.parallel('library-size', 'module-sizes'));
|
2018-12-14 20:20:43 +01:00
|
|
|
gulp.task('default', gulp.parallel('build'));
|
|
|
|
|
2020-02-09 17:50:11 +01:00
|
|
|
function run(bin, args) {
|
2020-02-19 20:24:16 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-02-09 17:50:11 +01:00
|
|
|
const exe = '"' + process.execPath + '"';
|
|
|
|
const src = require.resolve(bin);
|
|
|
|
const cmd = [exe, src].concat(args || []).join(' ');
|
|
|
|
const ps = exec(cmd);
|
|
|
|
|
|
|
|
ps.stdout.pipe(process.stdout);
|
|
|
|
ps.stderr.pipe(process.stderr);
|
2020-02-19 20:24:16 +01:00
|
|
|
ps.on('close', (error) => {
|
2020-02-09 17:50:11 +01:00
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-12-14 20:20:43 +01:00
|
|
|
}
|
2015-08-29 00:58:23 +02:00
|
|
|
|
2016-08-25 21:13:27 +02:00
|
|
|
/**
|
|
|
|
* Generates the bower.json manifest file which will be pushed along release tags.
|
|
|
|
* Specs: https://github.com/bower/spec/blob/master/json.md
|
|
|
|
*/
|
|
|
|
function bowerTask() {
|
2020-02-09 17:50:11 +01:00
|
|
|
const json = JSON.stringify({
|
|
|
|
name: pkg.name,
|
|
|
|
description: pkg.description,
|
|
|
|
homepage: pkg.homepage,
|
|
|
|
license: pkg.license,
|
|
|
|
version: pkg.version,
|
|
|
|
main: outDir + 'Chart.js',
|
|
|
|
ignore: [
|
|
|
|
'.github',
|
|
|
|
'.codeclimate.yml',
|
|
|
|
'.gitignore',
|
|
|
|
'.npmignore',
|
|
|
|
'.travis.yml',
|
|
|
|
'scripts'
|
|
|
|
]
|
|
|
|
}, null, 2);
|
|
|
|
|
|
|
|
return file('bower.json', json, {src: true})
|
|
|
|
.pipe(gulp.dest('./'));
|
2016-08-25 21:13:27 +02:00
|
|
|
}
|
2015-08-29 00:58:23 +02:00
|
|
|
|
|
|
|
function buildTask() {
|
2020-02-09 17:50:11 +01:00
|
|
|
return run('rollup/dist/bin/rollup', ['-c', argv.watch ? '--watch' : '']);
|
2015-08-29 00:58:23 +02:00
|
|
|
}
|
2014-06-29 19:37:07 +02:00
|
|
|
|
2016-05-15 01:19:57 +02:00
|
|
|
function packageTask() {
|
2020-02-09 17:50:11 +01:00
|
|
|
return merge(
|
|
|
|
// gather "regular" files landing in the package root
|
|
|
|
gulp.src([outDir + '*.js', outDir + '*.css', 'LICENSE.md']),
|
|
|
|
|
|
|
|
// since we moved the dist files one folder up (package root), we need to rewrite
|
|
|
|
// samples src="../dist/ to src="../ and then copy them in the /samples directory.
|
|
|
|
gulp.src('./samples/**/*', {base: '.'})
|
|
|
|
.pipe(streamify(replace(/src="((?:\.\.\/)+)dist\//g, 'src="$1')))
|
|
|
|
)
|
|
|
|
// finally, create the zip archive
|
|
|
|
.pipe(zip('Chart.js.zip'))
|
|
|
|
.pipe(gulp.dest(outDir));
|
2016-05-15 01:19:57 +02:00
|
|
|
}
|
|
|
|
|
2018-02-03 13:28:54 +01:00
|
|
|
function lintJsTask() {
|
2020-02-09 17:50:11 +01:00
|
|
|
const files = [
|
|
|
|
'samples/**/*.html',
|
|
|
|
'samples/**/*.js',
|
|
|
|
'src/**/*.js',
|
|
|
|
'test/**/*.js'
|
|
|
|
];
|
|
|
|
|
|
|
|
// NOTE(SB) codeclimate has 'complexity' and 'max-statements' eslint rules way too strict
|
|
|
|
// compare to what the current codebase can support, and since it's not straightforward
|
|
|
|
// to fix, let's turn them as warnings and rewrite code later progressively.
|
|
|
|
const options = {
|
|
|
|
rules: {
|
|
|
|
complexity: [1, 10],
|
|
|
|
'max-statements': [1, 30]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return gulp.src(files)
|
|
|
|
.pipe(eslint(options))
|
|
|
|
.pipe(eslint.format())
|
|
|
|
.pipe(eslint.failAfterError());
|
2015-08-29 00:58:23 +02:00
|
|
|
}
|
2014-06-29 19:37:07 +02:00
|
|
|
|
2020-02-08 00:03:33 +01:00
|
|
|
function typescriptTask() {
|
2020-02-09 17:50:11 +01:00
|
|
|
return tsProject.src()
|
|
|
|
.pipe(tsProject())
|
|
|
|
.js.pipe(gulp.dest('dist'));
|
2020-02-08 00:03:33 +01:00
|
|
|
}
|
|
|
|
|
2018-02-03 13:28:54 +01:00
|
|
|
function lintHtmlTask() {
|
2020-02-09 17:50:11 +01:00
|
|
|
return gulp.src('samples/**/*.html')
|
|
|
|
.pipe(htmllint({
|
|
|
|
failOnError: true,
|
|
|
|
}));
|
2018-02-03 13:28:54 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 13:28:21 +01:00
|
|
|
function docsTask(done) {
|
2020-02-09 17:50:11 +01:00
|
|
|
const bin = require.resolve('gitbook-cli/bin/gitbook.js');
|
|
|
|
const cmd = argv.watch ? 'serve' : 'build';
|
|
|
|
|
|
|
|
return run(bin, ['install', './'])
|
|
|
|
.then(() => run(bin, [cmd, './', './dist/docs']))
|
|
|
|
.then(() => {
|
|
|
|
const config = {
|
|
|
|
moduleResolution: 'Node',
|
|
|
|
target: 'ES6',
|
|
|
|
out: './dist/docs/typedoc'
|
|
|
|
};
|
|
|
|
gulp.src(['./src/**/*.js'], {read: false})
|
|
|
|
.pipe(typedoc(config, done));
|
|
|
|
}).catch((err) => {
|
|
|
|
done(new Error(err.stdout || err));
|
|
|
|
});
|
2017-03-21 01:36:54 +01:00
|
|
|
}
|
|
|
|
|
2017-03-04 16:47:53 +01:00
|
|
|
function unittestTask(done) {
|
2020-02-22 20:36:05 +01:00
|
|
|
// use `env.test` from `babel.config.json` for karma builds
|
|
|
|
process.env.NODE_ENV = 'test';
|
2020-02-09 17:50:11 +01:00
|
|
|
new karma.Server({
|
|
|
|
configFile: path.join(__dirname, 'karma.conf.js'),
|
|
|
|
singleRun: !argv.watch,
|
|
|
|
args: {
|
|
|
|
coverage: !!argv.coverage,
|
|
|
|
inputs: argv.inputs,
|
|
|
|
browsers: argv.browsers,
|
|
|
|
watch: argv.watch
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// https://github.com/karma-runner/gulp-karma/issues/18
|
2020-02-19 20:24:16 +01:00
|
|
|
(error) => {
|
2020-02-09 17:50:11 +01:00
|
|
|
error = error ? new Error('Karma returned with the error code: ' + error) : undefined;
|
|
|
|
done(error);
|
|
|
|
}).start();
|
2015-08-29 01:09:39 +02:00
|
|
|
}
|
2015-08-29 01:02:37 +02:00
|
|
|
|
2015-08-29 00:58:23 +02:00
|
|
|
function librarySizeTask() {
|
2020-02-09 17:50:11 +01:00
|
|
|
return gulp.src('dist/Chart.bundle.min.js')
|
|
|
|
.pipe(size({
|
|
|
|
gzip: true
|
|
|
|
}));
|
2015-08-29 00:58:23 +02:00
|
|
|
}
|
2014-06-29 19:37:07 +02:00
|
|
|
|
2015-08-29 00:58:23 +02:00
|
|
|
function moduleSizesTask() {
|
2020-02-09 17:50:11 +01:00
|
|
|
return gulp.src(srcDir + '**/*.js')
|
|
|
|
.pipe(terser())
|
|
|
|
.pipe(size({
|
|
|
|
showFiles: true,
|
|
|
|
gzip: true
|
|
|
|
}));
|
2015-08-29 00:58:23 +02:00
|
|
|
}
|