forked from oddbird/oddsite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
354 lines (306 loc) · 8.86 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* eslint-disable no-process-exit, global-require */
const browserSync = require('browser-sync').create();
const chalk = require('chalk');
const del = require('del');
const download = require('gulp-download');
const eslint = require('gulp-eslint');
const fs = require('fs-extra');
const gulp = require('gulp');
const gutil = require('gulp-util');
const KarmaServer = require('karma').Server;
const mocha = require('gulp-mocha');
const path = require('path');
const prettier = require('gulp-prettier-plugin');
const rename = require('gulp-rename');
const sasslint = require('gulp-sass-lint');
const svg = require('gulp-svg-symbols');
const svgmin = require('gulp-svgmin');
const webpack = require('webpack');
const { spawn } = require('child_process');
// sanity check Node version
const packageOptions = require('./package.json');
const expectedNodeVersion = `v${packageOptions.engines.node}`;
if (process.version !== expectedNodeVersion) {
process.stdout.write(
`You are not running node ${expectedNodeVersion}. Make sure that you've ` +
'run bin/unpack-node and that your $PATH includes ' +
`${path.resolve(__dirname, 'bin')}\n`,
);
process.exit(1);
}
const paths = {
SRC_TEMPLATES_DIR: 'templates/',
SRC_JS_DIR: 'static/js/',
JS_TESTS_DIR: 'test/js/',
SASS_DIR: 'static/sass/',
SASS_TESTS_DIR: 'test/sass/',
ICONS_DIR: 'templates/icons/',
DIST_DIR: 'dev-output/',
PROD_DIST_DIR: 'output/',
IGNORE: ['!**/.#*', '!**/flycheck_*'],
init() {
this.ALL_JS = [
`${this.SRC_JS_DIR}**/*.js`,
`${this.JS_TESTS_DIR}**/*.js`,
`${this.SASS_TESTS_DIR}**/*.js`,
'*.js',
'.*.js',
].concat(this.IGNORE);
this.SASS = [
`${this.SASS_DIR}**/*.scss`,
`${this.SASS_TESTS_DIR}**/*.scss`,
].concat(this.IGNORE);
return this;
},
}.init();
// Try to ensure that all processes are killed on exit
const spawned = [];
process.on('exit', () => {
spawned.forEach(pcs => {
pcs.kill();
});
});
const onError = function(err) {
gutil.log(chalk.red(err.message));
gutil.beep();
this.emit('end');
};
// Execute a command, logging output live while process runs
const spawnTask = function(command, args, cb) {
spawned.push(
spawn(command, args, { stdio: 'inherit' })
.on('error', err => {
gutil.beep();
return cb(err);
})
.on('exit', cb),
);
};
const eslintTask = (src, failOnError, log) => {
if (log) {
const cmd = `eslint ${src}`;
gutil.log('Running', `'${chalk.cyan(cmd)}'...`);
}
const stream = gulp
.src(src)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
if (!failOnError) {
stream.on('error', onError);
}
return stream;
};
const prettierTask = (src, log) => {
if (log) {
const cmd = `prettier ${src}`;
gutil.log('Running', `'${chalk.cyan(cmd)}'...`);
}
return gulp
.src(src, { base: './' })
.pipe(prettier({ singleQuote: true, trailingComma: 'all' }))
.pipe(gulp.dest('./'))
.on('error', onError);
};
const sasslintTask = (src, failOnError, log) => {
if (log) {
const cmd = `sasslint ${src}`;
gutil.log('Running', `'${chalk.cyan(cmd)}'...`);
}
const stream = gulp
.src(src)
.pipe(sasslint())
.pipe(sasslint.format())
.pipe(sasslint.failOnError());
if (!failOnError) {
stream.on('error', onError);
}
return stream;
};
gulp.task('default', ['eslint', 'sasslint', 'test']);
gulp.task('dev', ['eslint', 'sasslint', 'sasstest', 'serve']);
gulp.task('prod', ['webpack-prod', 'update-subproject-docs']);
gulp.task('serve', ['watch', 'runserver']);
gulp.task('quick-serve', ['runserver', 'webpack']);
gulp.task('test', ['jstest', 'sasstest']);
gulp.task('watch', ['jstest-watch', 'webpack-watch'], () => {
// lint scss on changes
gulp.watch(paths.SASS, ev => {
if (ev.type === 'added' || ev.type === 'changed') {
sasslintTask(ev.path, false, true);
}
});
// lint all scss when rules change
gulp.watch('**/.sass-lint.yml', ['sasslint-nofail']);
// run sass tests on changes
gulp.watch(paths.SASS, ['sasstest']);
// run webpack to compile svg icons and styleguide assets
gulp.watch(
[
`${paths.ICONS_DIR}**/*.svg`,
`${paths.SRC_TEMPLATES_DIR}_icon_template.lodash`,
'./STYLEGUIDE.md',
],
['webpack'],
);
// compile rstblog assets
gulp.watch(
[
`${paths.SRC_TEMPLATES_DIR}**/*.j2`,
`${paths.SRC_TEMPLATES_DIR}**/*.html`,
'content/**/*.rst',
'content/**/*.yml',
'content/static/images/**/*',
],
['dev-rebuild'],
);
});
gulp.task('prettier', () => prettierTask(paths.ALL_JS));
gulp.task('eslint', ['prettier'], () => eslintTask(paths.ALL_JS, true));
gulp.task('eslint-nofail', () => eslintTask(paths.ALL_JS));
gulp.task('sasslint', () => sasslintTask(paths.SASS, true));
gulp.task('sasslint-nofail', () => sasslintTask(paths.SASS));
gulp.task('sasstest', () =>
gulp
.src([`${paths.SASS_TESTS_DIR}test_sass.js`], { read: false })
.pipe(mocha({ reporter: 'dot' })),
);
const karmaOnBuild = done => exitCode => {
if (exitCode) {
gutil.beep();
done(
new gutil.PluginError('karma', {
name: 'KarmaError',
message: `Failed with exit code: ${exitCode}`,
}),
);
} else {
done();
}
process.exit(exitCode);
};
gulp.task('jstest', cb => {
const karmaConf = require('./karma.common.conf.js');
new KarmaServer(karmaConf, karmaOnBuild(cb)).start();
});
// Use karma watcher instead of gulp watcher for tests
gulp.task('jstest-watch', () => {
const karmaConf = require('./karma.common.conf.js');
const conf = Object.assign({}, karmaConf, {
autoWatch: true,
singleRun: false,
});
conf.coverageReporter.reporters = [
{ type: 'html', dir: 'jscov/' },
{ type: 'text-summary' },
];
new KarmaServer(conf).start();
});
gulp.task('sprites-clean', cb => {
fs.remove(`${paths.SRC_TEMPLATES_DIR}_icons.svg`, cb);
});
gulp.task('sprites', ['sprites-clean'], () =>
gulp
.src(`${paths.ICONS_DIR}**/*.svg`)
.pipe(svgmin())
.pipe(
svg({
id: 'icon-%f',
title: '%f icon',
templates: [
path.join(
__dirname,
paths.SRC_TEMPLATES_DIR,
'_icon_template.lodash',
),
],
}),
)
.pipe(rename('_icons.svg'))
.pipe(gulp.dest(paths.SRC_TEMPLATES_DIR)),
);
gulp.task('runserver', ['browser-sync']);
const getServeOpts = dir => ({
server: { baseDir: dir },
open: false,
logLevel: 'info',
logPrefix: 'oddsite',
notify: false,
files: [`${dir}**/*`],
reloadDebounce: 1000,
});
const getBsCb = cb => (err, bs) => {
bs.addMiddleware('*', (req, res) => {
res.writeHead(302, { location: '/404.html' });
res.end();
});
cb(err);
};
gulp.task('browser-sync', cb => {
browserSync.init(getServeOpts(paths.DIST_DIR), cb);
});
gulp.task('prod-serve', cb => {
browserSync.init(getServeOpts(paths.PROD_DIST_DIR), getBsCb(cb));
});
gulp.task('update-spammers', () => {
const url =
'https://raw.githubusercontent.com/piwik/' +
'referrer-spam-blacklist/master/spammers.txt';
return download(url).pipe(gulp.dest(paths.SRC_JS_DIR));
});
const webpackOnBuild = done => (err, stats) => {
if (err) {
gutil.log(chalk.red(err.stack || err));
if (err.details) {
gutil.log(chalk.red(err.details));
}
}
if (err || stats.hasErrors() || stats.hasWarnings()) {
gutil.beep();
}
gutil.log(stats.toString({ colors: true, chunks: false }));
if (done) {
done(err);
}
};
gulp.task('webpack', ['sprites', 'dev-clean'], cb => {
const webpackConfig = require('./webpack.config.js');
webpack(webpackConfig).run(webpackOnBuild(cb));
});
gulp.task('webpack-prod', ['sprites', 'prod-clean', 'update-spammers'], cb => {
const webpackProdConfig = require('./webpack.prod.config.js');
webpack(webpackProdConfig).run(webpackOnBuild(cb));
});
gulp.task('webpack-watch', ['sprites', 'dev-clean-html'], () => {
const webpackConfig = require('./webpack.config.js');
webpack(webpackConfig).watch(300, webpackOnBuild());
});
gulp.task('dev-clean', cb => {
fs.emptyDir(paths.DIST_DIR, cb);
});
gulp.task('dev-clean-html', cb => {
del([`${paths.DIST_DIR}**/*.html`, `!${paths.DIST_DIR}`]).then(() => {
cb();
});
});
gulp.task('prod-clean', cb => {
del(`${paths.PROD_DIST_DIR}*`).then(() => {
cb();
});
});
gulp.task('dev-styleguide-clean', cb => {
fs.emptyDir(`${paths.DIST_DIR}styleguide`, cb);
});
gulp.task('update-subproject-docs', cb => {
spawnTask('bin/update-subproject-docs', [], cb);
});
// @@@ Sassdoc does not properly update the st_mtime (modified time) on
// re-generated files, so we empty the output styleguide/ dir before copying
// changed files.
// See https://github.com/oddbird/oddsite/issues/55
gulp.task('dev-build', ['dev-styleguide-clean'], cb => {
spawnTask('python', ['run.py', 'dev'], cb);
});
gulp.task('dev-rebuild', ['dev-clean-html'], cb => {
spawnTask('python', ['run.py', 'dev'], cb);
});