forked from CSNW/d3.compose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
274 lines (235 loc) · 6.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
'use strict';
const path = require('path');
const gulp = require('gulp');
const runSequence = require('run-sequence');
const gulpLoadPlugins = require('gulp-load-plugins');
const rimraf = require('rimraf');
const GithubApi = require('github');
const inquirer = require('inquirer');
const $ = gulpLoadPlugins();
const pkg = require('./package.json');
const paths = {
tmp: './.tmp/',
dist: './dist/',
src: 'src/**/*.js',
css: 'src/css/*.css',
test: 'test/**/*.test.js',
library: 'd3.compose.js',
mixins: 'd3.compose-mixins.js',
all: 'd3.compose-all.js',
zip: `${pkg.name}-v${pkg.version}.zip`
};
const banner = '/*!\n' +
' * <%= pkg.name %> - <%= pkg.description %>\n' +
' * v<%= pkg.version %> - <%= pkg.homepage %> - license: <%= pkg.license %>\n' +
' */\n';
/**
Build temporary version of library
*/
gulp.task('build:tmp', series(
'clean:tmp',
parallel('build:tmp:all', 'build:tmp:css')
));
gulp.task('build:tmp:library', build(paths.library, paths.tmp));
gulp.task('build:tmp:mixins', build(paths.mixins, paths.tmp));
gulp.task('build:tmp:all', build(paths.all, paths.tmp));
gulp.task('build:tmp:css', css(paths.css, paths.tmp));
/**
Rebuild temp on change in js/css
*/
gulp.task('build:watch', series(
'build:tmp',
parallel('build:watch:js', 'build:watch:css')
));
gulp.task('build:watch:js', () => {
gulp.watch([paths.src, paths.all], parallel('build:tmp:all'));
});
gulp.task('build:watch:css', () => {
gulp.watch([paths.css], parallel('build:tmp:css'));
});
/**
Build distribution version of library
*/
const dist_options = {minify: true, banner: true};
gulp.task('build:dist', series(
'clean:dist',
parallel('build:dist:library', 'build:dist:mixins', 'build:dist:all', 'build:dist:css')
));
gulp.task('build:dist:library', build(paths.library, paths.dist, dist_options));
gulp.task('build:dist:mixins', build(paths.mixins, paths.dist, dist_options));
gulp.task('build:dist:all', build(paths.all, paths.dist, dist_options));
gulp.task('build:dist:css', css(paths.css, paths.dist, dist_options));
/**
Clean output folders
*/
gulp.task('clean:tmp', cb => rimraf(paths.tmp, cb));
gulp.task('clean:dist', cb => rimraf(paths.dist, cb));
/**
Bump the bower version to match package.json
*/
gulp.task('version:bower', () => {
return gulp.src('./bower.json')
.pipe($.bump({version: pkg.version}))
.pipe(gulp.dest('./'));
});
/**
Prepare files for docs
*/
gulp.task('docs', () => {
return gulp.src([`${paths.dist}*`, 'package.json', 'CHANGELOG.md'])
.pipe($.copy('_docs/additional/'));
});
/**
Create zip for github
*/
gulp.task('zip:github', () => {
return gulp.src(`${paths.dist}*`, {base: 'dist'})
.pipe($.zip(paths.zip))
.pipe(gulp.dest(paths.tmp));
});
/**
Publish release to github
*/
gulp.task('publish:github', series('zip:github', (cb) => {
const github = new GithubApi({
version: '3.0.0',
protocol: 'https'
});
const tag_name = `v${pkg.version}`;
const owner = 'CSNW';
const repo = 'd3.compose';
const name = `${pkg.name} ${tag_name}`;
inquirer.prompt([{
type: 'input',
name: 'token',
message: 'Enter your GitHub token'
}], (answers) => {
const token = answers.token;
if (!token)
return cb(new Error('A GitHub token is required to publish'));
github.authenticate({
type: 'oauth',
token
});
console.log(`Creating release: "${name}"`)
github.releases.createRelease({
owner,
repo,
tag_name,
name
}, (err, response) => {
if (err) return cb(err);
console.log(`Uploading zip: "${paths.zip}"`);
github.releases.uploadAsset({
owner,
repo,
id: response.id,
name: paths.zip,
filePath: path.join(__dirname, paths.tmp, paths.zip)
}, cb);
})
});
}));
/**
Create js library build function
@method build
@param {String} entry file for rollup
@param {String} output folder
@param {Object} options
@param {Boolean} [options.minify = false] Output minified build
@param {Boolean} [options.banner = false] Include banner with build
@return {Function}
*/
function build(entry, output, options) {
options = Object.assign({
minify: false,
banner: false
}, options);
const filename = path.basename(entry, '.js');
return () => {
var build = gulp.src(entry, {read: false})
.pipe($.plumber(onError));
// For minify, use bundled for sourcemap
if (!options.minify)
build = build.pipe($.sourcemaps.init());
build = build
.pipe($.rollup({
moduleName: 'd3c',
sourceMap: !options.minify,
sourceMapFile: filename + '.js.map',
external: ['d3', 'd3.chart'],
format: 'umd'
}))
.pipe($.replace(/\{version\}/g, pkg.version));
if (options.banner)
build = build.pipe($.header(banner, {pkg}));
if (options.minify) {
// Remove sourcemap from unminified and save
// then rename and uglify
build = build
.pipe(gulp.dest(output))
.pipe($.sourcemaps.init())
.pipe($.rename(filename + '.min.js'))
.pipe($.uglify());
}
build = build
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(output));
return build;
};
}
/**
Create css build function
@method css
@param {String|Array} files glob
@param {String} output folder
@param {Object} [options]
@param {Boolean} [options.banner = false] Include banner with build
@return {Function}
*/
function css(files, output, options) {
options = Object.assign({
banner: false
}, options);
return () => {
var build = gulp.src(files)
if (options.banner)
build = build.pipe($.header(banner, {pkg}));
build = build
.pipe($.rename('d3.compose.css'))
.pipe(gulp.dest(output));
return build;
};
}
function onError(err) {
$.util.log($.util.colors.red('Error (' + err.plugin + '): ' + err.message));
$.util.log(err);
this.emit('end');
}
/**
Approximate gulp 4.0 series
@param {...String} ...tasks
@param {Function} [fn] Function to call at end of series
@return {Function}
*/
function series() {
const tasks = Array.prototype.slice.call(arguments);
var fn = cb => cb();
if (typeof tasks[tasks.length - 1] === 'function')
fn = tasks.pop();
return (cb) => {
const tasks_with_cb = tasks.concat([(err) => {
if (err) return cb(err);
fn(cb);
}]);
runSequence.apply(this, tasks_with_cb);
}
}
/**
Approximate gulp 4.0 parallel
@param {...String} ...tasks
@return {Array}
*/
function parallel() {
return Array.prototype.slice.call(arguments);
}