This repository has been archived by the owner on Oct 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
300 lines (266 loc) · 9.07 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
'use strict';
const gulp = require('gulp');
const runSequence = require('run-sequence');
const concat = require('gulp-concat');
const rename = require('gulp-rename');
const eslint = require('gulp-eslint');
const tslint = require('gulp-tslint');
const plumber = require('gulp-plumber');
const mocha = require('gulp-mocha');
const karmaServer = require('karma').Server;
const uglify = require('gulp-uglify');
const sass = require('gulp-sass');
const cssnano = require('gulp-cssnano');
const autoprefixer = require('gulp-autoprefixer');
const systemjsBuilder = require('gulp-systemjs-builder');
const hashsum = require('gulp-hashsum');
const crypto = require('crypto');
const fs = require('fs');
const spawn = require('child_process').spawn;
let node;
let mongo;
let tsc;
/*
* hashsum identifies build
*
* after build SHA1SUMS.json is generated with sha1 sums for different files
* then sha256 is calculated using stringified file contents
*/
gulp.task('hashsum', () => {
return gulp.src(['./public/*', '!./public/SHA1SUMS.json', './public/app/views/**', './public/css/**', './public/webfonts/**', './public/img/**', './public/js/**'])
.pipe(hashsum({ filename: 'public/SHA1SUMS.json', hash: 'sha1', json: true }));
});
function setEnvBuildHash(env, done) {
fs.readFile('./public/SHA1SUMS.json', (err, data) => {
if (err) throw err;
const hash = crypto.createHmac('sha256', data.toString()).digest('hex');
console.log('BUILD_HASH', hash);
env += 'BUILD_HASH=' + hash + '\n';
fs.writeFile('./.env', env, (err) => {
if (err) throw err;
console.log('# > ENV > .env file was updated');
done();
});
});
}
gulp.task('set-build-hash', (done) => {
fs.readFile('./.env', (err, data) => {
let env = '';
if (err) {
console.log('./.env does not exist');
setEnvBuildHash(env, done);
} else {
env = data.toString().replace(/BUILD_HASH=.+\n/, '');
console.log('./.env exists, updated env', env);
setEnvBuildHash(env, done);
}
});
});
gulp.task('database', (done) => {
if (mongo) mongo.kill();
mongo = spawn('npm', ['run','mongo-start'], {stdio: 'inherit'});
mongo.on('close', (code) => {
if (code === 8) {
console.log('Error detected, waiting for changes...');
}
});
done();
});
gulp.task('server', (done) => {
if (node) node.kill();
node = spawn('node', ['server.js'], {stdio: 'inherit'});
node.on('close', (code) => {
if (code === 8) {
console.log('Error detected, waiting for changes...');
}
});
done();
});
gulp.task('tsc', (done) => {
if (tsc) tsc.kill();
tsc = spawn('tsc', [], {stdio: 'inherit'});
tsc.on('close', (code) => {
if (code === 8) {
console.log('Error detected, waiting for changes...');
} else {
done();
}
});
});
gulp.task('server-test', () => {
return gulp.src(['./test/server/*.js'], { read: false })
.pipe(mocha({ reporter: 'spec' }))
.on('error', (error) => console.log('error', error));
});
gulp.task('client-unit-test', (done) => {
const server = new karmaServer({
configFile: require('path').resolve('test/karma.conf.js'),
singleRun: false
});
server.on('browser_error', (browser, err) => {
throw new Error('=====\nKarma > Run Failed\n=====\n', err);
});
server.on('run_complete', (browsers, results) => {
if (results.failed) {
throw new Error('=====\nKarma > Tests Failed\n=====\n', results);
}
console.log('=====\nKarma > Complete With No Failures\n=====\n', results);
done();
});
server.start();
});
gulp.task('client-unit-test-single-run', (done) => {
const server = new karmaServer({
configFile: require('path').resolve('test/karma.conf.js'),
singleRun: true
});
server.on('browser_error', (browser, err) => {
throw new Error('=====\nKarma > Run Failed\n=====\n', err);
});
server.on('run_complete', (browsers, results) => {
if (results.failed) {
throw new Error('=====\nKarma > Tests Failed\n=====\n', results);
}
console.log('=====\nKarma > Complete With No Failures\n=====\n', results);
done();
});
server.start();
});
gulp.task('build-system-js', () => {
/*
* this task builds angular application
* components, angular modules, and some dependencies
*
* nonangular components related to design, styling, data visualization etc.
* are built by another task
*/
return systemjsBuilder('/','./systemjs.config.js')
.buildStatic('app', 'bundle.min.js', {
minify: true,
mangle: true
})
.pipe(gulp.dest('./public/js'));
});
gulp.task('pack-vendor-js', () => {
/*
* nonangular js bundle
* components related to design, styling, data visualization etc.
*/
return gulp.src([
'./node_modules/d3/d3.js',
'./node_modules/nvd3/build/nv.d3.js',
'./node_modules/zone.js/dist/zone.min.js',
'./node_modules/reflect-metadata/Reflect.js',
'./node_modules/web-animations-js/web-animations.min.js'
])
.pipe(plumber())
.pipe(concat('vendor-bundle.js'))
.pipe(uglify())
.pipe(plumber.stop())
.pipe(rename('vendor-bundle.min.js'))
.pipe(gulp.dest('./public/js'));
});
gulp.task('pack-vendor-css', () => {
return gulp.src([
/*
* add paths to required third party css files
*/
'./node_modules/nvd3/build/nv.d3.css',
'./node_modules/components-font-awesome/css/fontawesome-all.css',
/*
* Angular material theme should be chosen and loaded here
*/
'./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css'
//'./node_modules/@angular/material/prebuilt-themes/indigo-pink.css'
//'./node_modules/@angular/material/prebuilt-themes/pink-bluegrey.css'
//'./node_modules/@angular/material/prebuilt-themes/purple-green.css'
])
.pipe(plumber())
.pipe(concat('vendor-bundle.css'))
.pipe(cssnano())
.pipe(plumber.stop())
.pipe(rename('vendor-bundle.min.css'))
.pipe(gulp.dest('./public/css'));
});
gulp.task('move-vendor-fonts', () => {
return gulp.src([
'./node_modules/components-font-awesome/webfonts/*.*',
// material design icons
'./node_modules/material-design-icon-fonts/iconfont/*.eot',
'./node_modules/material-design-icon-fonts/iconfont/*.woff2',
'./node_modules/material-design-icon-fonts/iconfont/*.woff',
'./node_modules/material-design-icon-fonts/iconfont/*.ttf'
])
.pipe(gulp.dest('./public/webfonts'));
});
gulp.task('sass-autoprefix-minify-css', () => {
return gulp.src('./public/app/scss/*.scss')
.pipe(plumber())
.pipe(concat('packed-app.css'))
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 versions']
}))
.pipe(cssnano())
.pipe(plumber.stop())
.pipe(rename('bundle.min.css'))
.pipe(gulp.dest('./public/css'));
});
gulp.task('eslint', () => {
return gulp.src(['./app/**', './public/js/*.js', './*.js']) // uses ignore list from .eslintignore
.pipe(eslint('./.eslintrc.json'))
.pipe(eslint.format());
});
gulp.task('tslint', () => {
return gulp.src(['./public/app/*.ts', './public/app/**/*.ts'])
.pipe(tslint({
formatter: 'verbose' // 'verbose' - extended info | 'prose' - brief info
}))
.pipe(tslint.report({
emitError: false
}));
});
gulp.task('watch', () => {
gulp.watch(['./server.js', './app/**/*.js'], ['server']);
gulp.watch(['./test/server/test.js'], ['server-test']);
gulp.watch(['./server.js', './app/models/*.js'], ['database']);
gulp.watch(['./gulpfile.js'], ['pack-vendor-js', 'pack-vendor-css', 'move-vendor-fonts']);
gulp.watch('./public/app/scss/*.scss', ['sass-autoprefix-minify-css']);
gulp.watch(['./public/app/*.ts', './public/app/**/*.ts', './test/client/**/*.ts', './tslint.json'], ['spawn-rebuild-app']);
gulp.watch(['./app/**', './public/js/*.js', './*.js', './.eslintignore', './.eslintrc.json'], ['eslint']);
});
gulp.task('watch-and-lint', () => {
gulp.watch(['./app/**', './public/js/*.js', './*.js', './.eslintignore', './.eslintrc.json'], ['eslint']); // watch files to be linted or eslint config files and lint on change
gulp.watch(['./public/app/*.ts', './public/app/**/*.ts', './tslint.json'], ['tslint']); // watch files to be linted or eslint config files and lint on change
});
gulp.task('watch-client-and-test', () => {
gulp.watch(['./public/app/*.ts','./test/client/*.ts'], ['tsc']); //watch unit test changes and run tests
gulp.watch(['./public/app/*.js','./test/client/*.js','./test/karma.conf.js','./test/karma.test-shim.js'], ['client-unit-test']); //watch unit test changes and run tests
});
gulp.task('build', (done) => {
runSequence('build-system-js', 'pack-vendor-js', 'pack-vendor-css', 'move-vendor-fonts', 'sass-autoprefix-minify-css', 'hashsum', 'set-build-hash', done);
});
gulp.task('compile-and-build', (done) => {
runSequence('tsc', 'build', done);
});
gulp.task('rebuild-app', (done) => { // should be used in watcher to rebuild the app on *.ts file changes
runSequence('tslint', 'compile-and-build', done);
});
let rebuildApp;
gulp.task('spawn-rebuild-app', (done) => {
if (rebuildApp) rebuildApp.kill();
rebuildApp = spawn('gulp', ['rebuild-app'], {stdio: 'inherit'});
rebuildApp.on('close', (code) => {
console.log(`rebuildApp closed with code ${code}`);
});
done();
});
gulp.task('lint', (done) => {
runSequence('eslint', 'tslint', done);
});
gulp.task('default', (done) => {
runSequence('lint', 'compile-and-build', 'database', 'server', 'watch', done);
});
process.on('exit', (code) => {
console.log(`PROCESS EXIT CODE ${code}`);
});