-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.js
447 lines (395 loc) · 11.6 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* eslint-env node */
'use strict';
const gulp = require('gulp');
const babel = require('gulp-babel');
const eslint = require('gulp-eslint');
const zip = require('gulp-zip');
const del = require('del');
const runSequence = require('run-sequence');
const webserver = require('gulp-webserver');
const mocha = require('gulp-mocha');
const gls = require('gulp-live-server');
const gsww = require('gulp-sww');
const pkg = require('./package.json');
const esdoc = require('gulp-esdoc');
const sourcemaps = require('gulp-sourcemaps');
const rename = require('gulp-rename');
const merge = require('merge2');
const rollup = require('gulp-rollup');
const rollupBabelPlugin = require('rollup-plugin-babel');
const rollupIncludePathsPlugin = require('rollup-plugin-includepaths');
const rollupUglifyPlugin = require('rollup-plugin-uglify');
const minifier = require('gulp-uglify/minifier');
const uglifyjs = require('uglify-js');
const postcss = require('gulp-postcss');
const postcssImport = require('postcss-import');
const postcssUrl = require('postcss-url');
const cssnano = require('cssnano');
const stylelint = require('gulp-stylelint');
const git = require('gulp-git');
const ghPages = require('gulp-gh-pages');
const APP_ROOT = './app/';
const TESTS_ROOT = './tests/';
const DOC_ROOT = './doc/';
const DIST_ROOT = './dist/';
const DIST_APP_ROOT = './dist/app/';
const DIST_TESTS_ROOT = './dist/tests/';
let webserverStream;
let foxboxSimulator;
let registrationServerSimulator;
/**
* Runs eslint on all javascript files found in the app and tests dirs.
*/
gulp.task('lint', () => {
return merge(
// Note: To have the process exit with an error code (1) on lint error,
// return the stream and pipe to failOnError last.
gulp
.src([`${APP_ROOT}**/*.{js,jsx}`, `${TESTS_ROOT}**/*.js`, './*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError()),
gulp
.src('app/**/*.css')
.pipe(
stylelint({ reporters: [{ formatter: 'verbose', console: true }] })
)
);
});
gulp.task('copy-app-common', () => {
return merge(
gulp.src([
`${APP_ROOT}**`,
// Don't copy documentation files.
`!${APP_ROOT}**/*.md`,
// Don't copy JS, it will be compiled and copied on the compile step.
`!${APP_ROOT}js/**`,
// Don't copy CSS, it will be compiled and copied on the compile step.
`!${APP_ROOT}**/*.css`,
], { nodir: true }),
// Module loader.
gulp.src('./node_modules/alameda/alameda.js')
.pipe(rename('js/alameda.js')),
// Polyfills.
gulp.src('./node_modules/whatwg-fetch/fetch.js')
.pipe(rename('js/polyfills/fetch.js'))
)
.pipe(gulp.dest(DIST_APP_ROOT));
});
/**
* Copy all required app resources/assets/external components to be used in
* development build.
*/
gulp.task('copy-app-dev', ['copy-app-common'], () => {
return merge(
// Components.
gulp.src('./node_modules/react/dist/react.js')
.pipe(rename('js/components/react.js')),
gulp.src('./node_modules/react-dom/dist/react-dom.js')
.pipe(rename('js/components/react-dom.js')),
// Polyfills.
gulp.src('./node_modules/url-search-params/build/url-search-params.max.js')
.pipe(rename('js/polyfills/url-search-params.js'))
)
.pipe(gulp.dest(DIST_APP_ROOT));
});
/**
* Copy all required app resources/assets/external components to be used in
* production build.
*/
gulp.task('copy-app-production', ['copy-app-common'], () => {
return merge(
// Components.
gulp.src('./node_modules/react/dist/react.min.js')
.pipe(rename('js/components/react.js')),
gulp.src('./node_modules/react-dom/dist/react-dom.min.js')
.pipe(rename('js/components/react-dom.js')),
// Polyfills.
gulp.src('./node_modules/url-search-params/build/url-search-params.js')
.pipe(rename('js/polyfills/url-search-params.js'))
)
.pipe(gulp.dest(DIST_APP_ROOT));
});
/**
* Compiles app source i.e. processes source with Babel and Rollup.
*/
gulp.task('compile-app-dev', () => {
return gulp
.src(`${APP_ROOT}js/*.js`)
.pipe(
rollup({
sourceMap: true,
format: 'amd',
external: ['components/react', 'components/react-dom'],
plugins: [
rollupBabelPlugin(),
rollupIncludePathsPlugin({ extensions: ['.js', '.jsx'] }),
],
})
)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${DIST_APP_ROOT}js`));
});
/**
* Compiles app source for production i.e. processes source with Babel, Rollup
* and UglifyJS.
*/
gulp.task('compile-app-production', ['compress-external-modules'], () => {
return gulp
.src(`${APP_ROOT}js/*.js`)
.pipe(
rollup({
sourceMap: true,
format: 'amd',
external: ['components/react', 'components/react-dom'],
plugins: [
rollupBabelPlugin(),
rollupUglifyPlugin({ sourceMap: true }, uglifyjs.minifier),
rollupIncludePathsPlugin({ extensions: ['.js', '.jsx'] }),
],
})
)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${DIST_APP_ROOT}js`));
});
/**
* Compresses external modules that don't provide compressed version.
*/
gulp.task('compress-external-modules', () => {
return gulp
.src([
`${DIST_APP_ROOT}js/alameda.js`,
`${DIST_APP_ROOT}js/polyfills/fetch.js`,
], { base: DIST_APP_ROOT })
.pipe(sourcemaps.init())
.pipe(minifier({}, uglifyjs))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(DIST_APP_ROOT));
});
/**
* Applies Babel transform to the app/tests JS and JSX files. The only
* difference between normal app build and build for unit tests is that Rollup
* is not used here.
*/
gulp.task('compile-unit-tests', () => {
process.env.BABEL_ENV = 'test';
return gulp
.src([`${APP_ROOT}js/**/*.{js,jsx}`, `${TESTS_ROOT}unit/**/*.js`])
.pipe(babel())
.pipe(gulp.dest(`${DIST_TESTS_ROOT}unit/`));
});
/**
* Pipes CSS through several postCSS plugins and outputs single CSS file.
*/
gulp.task('compile-css', () => {
return gulp.src(`${APP_ROOT}css/app.css`)
.pipe(sourcemaps.init())
.pipe(postcss([
postcssImport(),
postcssUrl({ url: 'rebase' }),
cssnano(),
]))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${DIST_APP_ROOT}css/`));
});
/**
* Packages the application into a zip.
*/
gulp.task('zip', () => {
return gulp.src(DIST_APP_ROOT)
.pipe(zip('app.zip'))
.pipe(gulp.dest(DIST_ROOT));
});
/**
* Builds the app for development.
*/
gulp.task('build-dev', (cb) => {
process.env.BABEL_ENV = 'development';
runSequence(
'lint', 'clobber-app', 'copy-app-dev', 'compile-app-dev', 'compile-css',
'offline', cb
);
});
/**
* Builds the app for the production.
*/
gulp.task('build-production', (cb) => {
process.env.BABEL_ENV = 'production';
runSequence(
'lint', 'clobber-app', 'copy-app-production', 'compile-app-production',
'compile-css', 'offline', cb
);
});
/**
* Add offline support.
*/
gulp.task('offline', () => {
return gulp.src(
[
'**/*',
'!**/*.map',
],
{ cwd: DIST_APP_ROOT }
)
.pipe(gsww({
version: pkg.version,
hookSW: 'hookSW.js',
}))
.pipe(gulp.dest(DIST_APP_ROOT));
});
/**
* Watch for changes on the file system, and rebuild if so.
*/
gulp.task('watch', () => {
gulp.watch([`${APP_ROOT}**`], ['build-dev']);
});
gulp.task('webserver', () => {
webserverStream = gulp.src(DIST_APP_ROOT)
.pipe(webserver({
port: process.env.PORT || 8000,
host: process.env.HOSTNAME || 'localhost',
livereload: false,
directoryListing: false,
open: false,
https: { key: './certs/key.pem', cert: './certs/cert.pem' },
}));
});
gulp.task('stop-webserver', () => {
webserverStream.emit('kill');
});
/**
* The default task when `gulp` is run.
* Adds a listener which will re-build on a file save.
*/
gulp.task('default', (cb) => {
runSequence('build-dev', 'webserver', 'watch', cb);
});
/**
* Remove the distributable files.
*/
gulp.task('clobber-app', () => del(DIST_APP_ROOT));
gulp.task('clobber-tests', () => del(DIST_TESTS_ROOT));
gulp.task('clobber-doc', () => del(DOC_ROOT));
/**
* Cleans all created files by this gulpfile, and node_modules.
*/
gulp.task('clean', () => {
return del([
DIST_ROOT,
DOC_ROOT,
'node_modules/',
]);
});
/**
* Generate documentation from the code.
*/
gulp.task('generate-doc', () => {
return gulp.src(`${APP_ROOT}js/lib/foxbox/`)
.pipe(esdoc({
destination: DOC_ROOT,
title: 'Project Link web app',
test: {
type: 'mocha',
source: TESTS_ROOT,
},
}));
});
gulp.task('copy-doc-badge', () => {
return gulp.src(`${DOC_ROOT}badge.svg`)
.pipe(gulp.dest('./assets/'));
});
gulp.task('start-simulators', () => {
foxboxSimulator = gls(
`${TESTS_ROOT}foxbox-simulator/http-server.js`, undefined, false
);
foxboxSimulator.start();
registrationServerSimulator = gls(
`${TESTS_ROOT}registration-server-simulator/http-server.js`, undefined,
false
);
registrationServerSimulator.start();
});
gulp.task('stop-simulators', () => {
foxboxSimulator.stop();
registrationServerSimulator.stop();
});
gulp.task('run-unit-tests', ['compile-unit-tests'], (cb) => {
const server = new (require('karma').Server)(
{ configFile: `${__dirname}/karma.conf.js` }, cb
);
server.start();
});
gulp.task('run-test-integration', () => {
return gulp.src(
`${TESTS_ROOT}{common,integration}/**/*_test.js`, { read: false }
)
.pipe(mocha());
});
gulp.task('doc', () => {
runSequence(['clobber-doc'], ['generate-doc'], ['copy-doc-badge']);
});
gulp.task('test-integration', (cb) => {
runSequence('start-simulators', 'run-test-integration', () => {
// Tear down whatever the result is
runSequence('stop-simulators', cb);
});
});
gulp.task('run-test-e2e', () => {
return gulp.src(`${TESTS_ROOT}{common,e2e}/**/*_test.js`, { read: false })
.pipe(mocha());
});
gulp.task('test', ['clobber-tests', 'build-production'], (cb) => {
runSequence(
'run-unit-tests',
'webserver',
'test-integration',
() => { // Tear down whatever the result is
runSequence('stop-webserver', cb);
}
);
});
// @todo Should be included in 'test' once less manual steps are required.
gulp.task('test-e2e', () => {
runSequence(
'build-production',
'webserver',
'run-test-e2e',
'stop-webserver'
);
});
/**
* Deploys production-optimized app build to the "origin/gh-branch". Every new
* deployment comes with a separate commit attributed with the date and source
* branch revision. If tree has some changes that are not committed yet, there
* will be a warning, but all local changes will be deployed anyway.
*/
gulp.task('deploy', ['build-production'], () => {
const wrapIntoPromise = (method, args) => new Promise((resolve, reject) => {
git[method].call(
git, args, (err, result) => err ? reject(err) : resolve(result)
);
});
return Promise
.all([
wrapIntoPromise('revParse', { args: '--short HEAD', quiet: true }),
wrapIntoPromise('status', { args: '--porcelain', quiet: true }),
])
.then((results) => {
const revision = results[0];
const status = results[1];
let message = `Deployment is based on ${revision}`;
if (status) {
console.log(
'\x1b[31m',
`You have uncommitted changes that will be deployed!\n${status}`,
'\x1b[0m'
);
message += ' (includes uncommitted changes)';
}
return new Promise((resolve, reject) => {
merge(gulp.src(`${DIST_APP_ROOT}**/*`).pipe(ghPages({ message })))
.on('end', resolve)
.on('error', reject);
});
});
});