forked from chartjs/chartjs-plugin-zoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
148 lines (129 loc) · 4.15 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
var gulp = require('gulp');
var eslint = require('gulp-eslint');
var file = require('gulp-file');
var util = require('gulp-util');
var htmllint = require('gulp-htmllint');
var replace = require('gulp-replace');
var streamify = require('gulp-streamify');
var zip = require('gulp-zip');
var inquirer = require('inquirer');
var semver = require('semver');
var fs = require('fs');
var {exec} = require('child_process');
var merge = require('merge-stream');
var pkg = require('./package.json');
var srcDir = './src/';
var srcFiles = srcDir + '**.js';
var outDir = './dist/';
function run(bin, args, done) {
var exe = '"' + process.execPath + '"';
var src = require.resolve(bin);
var ps = exec([exe, src].concat(args || []).join(' '));
ps.stdout.pipe(process.stdout);
ps.stderr.pipe(process.stderr);
ps.on('close', () => done());
}
gulp.task('bower', bowerTask);
gulp.task('build', gulp.series(rollupTask, copyDistFilesTask));
gulp.task('package', packageTask);
gulp.task('bump', bumpTask);
gulp.task('lint-html', lintHtmlTask);
gulp.task('lint-js', lintJsTask);
gulp.task('lint', gulp.parallel('lint-html', 'lint-js'));
gulp.task('watch', watchTask);
gulp.task('default', gulp.parallel('lint', 'build', 'watch'));
/**
* 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() {
var json = JSON.stringify({
name: pkg.name,
description: pkg.description,
homepage: pkg.homepage,
license: pkg.license,
version: pkg.version,
main: outDir + pkg.name + '.js'
}, null, 2);
return file('bower.json', json, {src: true})
.pipe(gulp.dest('./'));
}
function rollupTask(done) {
run('rollup/bin/rollup', ['-c'], done);
}
/**
* Copy the files from `/dist` to the root directory.
* @todo remove at version 1.0
*/
function copyDistFilesTask() {
return gulp.src(outDir + '*.js')
.pipe(gulp.dest('./'));
}
function packageTask() {
return merge(
// gather "regular" files landing in the package root
gulp.src([outDir + '*.js', '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(pkg.name + '.zip'))
.pipe(gulp.dest(outDir));
}
/*
* Usage : gulp bump
* Prompts: Version increment to bump
* Output: - New version number written into package.json
*/
function bumpTask(complete) {
util.log('Current version:', util.colors.cyan(pkg.version));
var choices = ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'].map(function(versionType) {
return versionType + ' (v' + semver.inc(pkg.version, versionType) + ')';
});
inquirer.prompt({
type: 'list',
name: 'version',
message: 'What version update would you like?',
choices: choices
}).then(function(res) {
var increment = res.version.split(' ')[0],
newVersion = semver.inc(pkg.version, increment);
// Set the new versions into the package object
pkg.version = newVersion;
// Write these to their own files, then build the output
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));
complete();
});
}
function lintJsTask() {
var 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.
var options = {
rules: {
'complexity': [1, 10],
'max-statements': [1, 30]
}
};
return gulp.src(files)
.pipe(eslint(options))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
}
function lintHtmlTask() {
return gulp.src('samples/**/*.html')
.pipe(htmllint({
failOnError: true,
}));
}
function watchTask() {
return gulp.watch(srcFiles, gulp.parallel('lint', 'build'));
}