forked from ciprianciurea/chartjs-plugin-doughnutlabel
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
122 lines (109 loc) · 3.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
'use strict';
var gulp = require('gulp');
var eslint = require('gulp-eslint');
var rename = require('gulp-rename');
var replace = require('gulp-replace');
var streamify = require('gulp-streamify');
var uglify = require('gulp-uglify');
var gutil = require('gulp-util');
var zip = require('gulp-zip');
var merge = require('merge2');
var path = require('path');
var rollup = require('rollup-stream');
var source = require('vinyl-source-stream');
var pkg = require('./package.json');
var gulpIf = require('gulp-if');
const browserSync = require('browser-sync');
const clean = require('gulp-clean');
var argv = require('yargs')
.option('output', { alias: 'o', default: 'dist' })
.option('samples-dir', { default: 'samples' })
.option('docs-dir', { default: 'docs' })
.option('www-dir', { default: 'www' }).argv;
function watch(glob, task, done) {
gutil.log('Waiting for changes...');
browserSync.init({
server: {
baseDir: ['./', './samples/'],
},
});
return gulp
.watch(glob, task)
.on('end', done)
.on('change', function (file) {
gutil.log('Changes detected for', path.relative('.', file));
})
.on('change', browserSync.reload);
}
function isFixed(file) {
return file.eslint !== null && file.eslint.fixed;
}
gulp.task(
'clean',
gulp.series(function () {
const out = argv.output;
return gulp.src(out, { read: false, allowEmpty: true }).pipe(clean());
})
);
gulp.task(
'lint',
gulp.series(function () {
var files = ['samples/**/*.js', 'src/**/*.js', 'test/**/*.js', '*.js'];
return gulp
.src(files)
.pipe(eslint({ fix: true }))
.pipe(eslint.format())
.pipe(
gulpIf(
isFixed,
gulp.dest((file) => file.base)
)
)
.pipe(eslint.failAfterError());
})
);
gulp.task(
'build',
gulp.series('clean', function (done) {
var out = argv.output;
var task = function () {
return rollup('rollup.config.js')
.pipe(source(pkg.name + '.js'))
.pipe(gulp.dest(out))
.pipe(rename(pkg.name + '.min.js'))
.pipe(streamify(uglify({ output: { comments: 'some' } })))
.pipe(gulp.dest(out));
};
return argv.watch
? [task(), watch(['src/**/*.js', 'samples/**/*.js'], task, done)]
: task();
})
);
gulp.task(
'samples',
gulp.series(function () {
// 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.
var out = path.join(argv.output, argv.samplesDir);
return gulp
.src('samples/**/*', { base: 'samples' })
.pipe(
streamify(
replace(/src="((?:\.\.\/)+)dist\//g, 'src="$1', { skipBinary: true })
)
)
.pipe(gulp.dest(out));
})
);
gulp.task(
'package',
gulp.series('build', 'samples', function () {
var out = argv.output;
var streams = merge(
gulp.src(path.join(out, argv.samplesDir, '**/*'), { base: out }),
gulp.src([path.join(out, '*.js'), 'LICENSE.md'], { allowEmpty: true })
);
return streams.pipe(zip(pkg.name + '.zip')).pipe(gulp.dest(out));
})
);
gulp.task('default', gulp.series('build'));