forked from mykola-digtiar/chartjs-plugin-piechart-outlabels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
128 lines (111 loc) · 3.48 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
'use strict';
var gulp = require('gulp');
var eslint = require('gulp-eslint');
var file = require('gulp-file');
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 {exec} = require('mz/child_process');
var pkg = require('./package.json');
var argv = require('yargs')
.option('output', {alias: 'o', default: 'dist'})
.option('sample-dir', {default: 'sample'})
.option('docs-dir', {default: 'docs'})
.option('www-dir', {default: 'www'})
.argv;
function watch(glob, task) {
gutil.log('Waiting for changes...');
return gulp.watch(glob, function(e) {
gutil.log('Changes detected for', path.relative('.', e.path), '(' + e.type + ')');
var r = task();
gutil.log('Waiting for changes...');
return r;
});
}
gulp.task('default', ['build']);
gulp.task('build', function() {
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({preserveComments: 'license'})))
.pipe(gulp.dest(out));
};
var tasks = [task()];
if (argv.watch) {
tasks.push(watch('src/**/*.js', task));
}
return tasks;
});
gulp.task('lint', function() {
var files = [
'sample/**/*.js',
'src/**/*.js',
'*.js'
];
return gulp.src(files)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('docs', function(done) {
var script = require.resolve('gitbook-cli/bin/gitbook.js');
var out = path.join(argv.output, argv.docsDir);
var cmd = 'node';
exec([cmd, script, 'install', './'].join(' ')).then(() => {
return exec([cmd, script, 'build', './', out].join(' '));
}).then(() => {
done();
}).catch((err) => {
done(new Error(err.stdout || err));
});
});
gulp.task('sample', function() {
// since we moved the dist files one folder up (package root), we need to rewrite
// sample src="../dist/ to src="../ and then copy them in the /sample directory.
var out = path.join(argv.output, argv.sampleDir);
return gulp.src('sample/**/*', {base: 'sample'})
.pipe(streamify(replace(/src="((?:\.\.\/)+)dist\//g, 'src="$1', {skipBinary: true})))
.pipe(gulp.dest(out));
});
gulp.task('package', ['build', 'sample'], function() {
var out = argv.output;
return merge(
gulp.src(path.join(out, argv.sampleDir, '**/*'), {base: out}),
gulp.src([path.join(out, '*.js'), 'LICENSE'])
)
.pipe(zip(pkg.name + '.zip'))
.pipe(gulp.dest(out));
});
gulp.task('netlify', ['build', 'sample', 'docs'], function() {
var root = argv.output;
var out = path.join(root, argv.wwwDir);
return merge(
gulp.src(path.join(root, argv.docsDir, '**/*'), {base: path.join(root, argv.docsDir)}),
gulp.src(path.join(root, argv.sampleDir, '**/*'), {base: root}),
gulp.src(path.join(root, '*.js'))
)
.pipe(streamify(replace(/https?:\/\/chartjs-plugin-piechart-outlabels\.netlify\.com\/?/g, '/', {skipBinary: true})))
.pipe(gulp.dest(out));
});
gulp.task('bower', function() {
var json = JSON.stringify({
name: pkg.name,
description: pkg.description,
homepage: pkg.homepage,
license: pkg.license,
version: pkg.version,
main: argv.output + '/' + pkg.name + '.js'
}, null, 2);
return file('bower.json', json, {src: true})
.pipe(gulp.dest('./'));
});