-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgulpfile.js
More file actions
264 lines (213 loc) · 6.41 KB
/
gulpfile.js
File metadata and controls
264 lines (213 loc) · 6.41 KB
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
'use strict';
const fs = require('fs');
const _ = require('lodash');
const del = require('del');
const gulp = require('gulp');
const argv = require('minimist')(process.argv.slice(2));
const webpack = require('webpack');
const browserSync = require('browser-sync');
const htmlcompress = require('gulp-minify-html');
const gutil = require('gulp-util');
const gif = require('gulp-if');
// const filelog = require('gulp-filelog'); // NOTE: Used for debug
const runSequence = require('run-sequence');
const zip = require('gulp-zip');
const mocha = require('gulp-mocha');
const path = require('path');
const walk = require('walk');
const execSync = require('child_process').execSync;
const settings = {
index: __dirname + '/src/index.html',
entry: __dirname + '/src/index.js',
output: __dirname + '/dist',
server: __dirname + '/dist',
assets: __dirname + '/assets/**/*'
};
const testSettings = {
input: path.join(__dirname, 'test'),
output: path.join(__dirname, 'dist-test'),
testSuffix: /\.spec\.ts$/
};
let WATCH = !!argv.watch;
let RELEASE = !!argv.release;
const DEBUG = !!argv.debug;
function getBundleConfig() {
const config = _.defaultsDeep({}, require('./webpack.config'));
config.entry = settings.entry;
config.output.path = settings.output;
if (WATCH) {
// config.chunkModules = false;
config.watch = true;
}
if (RELEASE) {
config.plugins = config.plugins.concat(
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.OccurenceOrderPlugin()
);
} else {
config.output.pathinfo = true;
}
if (!RELEASE || DEBUG) {
config.devtool = '#inline-source-map';
}
return config;
}
function getTests(cb) {
const files = [];
const walker = walk.walk(testSettings.input, { followLinks: false });
walker.on('file', (root, stat, next) => {
if (stat.name.match(testSettings.testSuffix)) {
files.push(path.join(root, stat.name));
}
next();
});
walker.on('end', () => {
cb(files);
});
}
/**
* auto-generate a file with the glimpse client version as pulled from package.json.
*/
function generateGlimpseVersionFile(cb) {
const packageJSON = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const contents =
`'use strict'
// this file is automatically generated by the build
module.exports = {
version: '${packageJSON.version}'
};
`;
fs.writeFileSync('./glimpseClientVersion.js', contents, 'utf8');
cb();
}
gulp.task('bundle', (cb) => {
let started = false;
const config = getBundleConfig();
function processResult(err, stats) {
if (err) {
return cb(err);
}
gutil.log('Webpack\n' + stats.toString(config.log));
if (stats.hasErrors()) {
return cb(new Error('Webpack completed with errors.'))
}
if (config.watch) {
browserSync.reload(settings.entry);
}
if (!started) {
started = true;
cb();
}
}
const compiler = webpack(config);
if (config.watch) {
compiler.watch(200, processResult);
} else {
compiler.run(processResult);
}
});
gulp.task('pages', () => {
return gulp.src(settings.index)
.pipe(gif(RELEASE, htmlcompress()))
.pipe(gulp.dest(settings.output))
.pipe(gif(WATCH, browserSync.reload({ stream: true })));
});
gulp.task('assets', () => {
return gulp.src(settings.assets)
.pipe(gulp.dest(settings.output + '/assets'))
.pipe(gif(WATCH, browserSync.reload({ stream: true })));
});
gulp.task('clean', () => {
return del(settings.server + '/**');
});
gulp.task('clean-test', () => {
return del(testSettings.output + '/**');
});
gulp.task('generate-client-version', (cb) => {
generateGlimpseVersionFile(cb);
});
// NOTE: was running in parallel but don't like the output
//gulp.task('build', ['pages', 'bundle']);
gulp.task('build', (cb) => {
runSequence('generate-client-version', 'pages', 'assets', 'bundle', cb);
});
gulp.task('build-dev', (cb) => {
RELEASE = false;
runSequence('build', cb);
});
gulp.task('build-prod', (cb) => {
RELEASE = true;
runSequence('build', cb);
});
gulp.task('build-ci', ['clean'], (cb) => {
RELEASE = false;
runSequence('build', cb);
});
gulp.task('build-test', (cb) => {
getTests((tests) => {
const config = _.defaultsDeep({}, require('./webpack.config'));
const pathPrefix = path.join(testSettings.input, path.sep);
config.entry = {};
tests.forEach(function collectTest(test) {
// strip out the prefix from the full path name
let pathName = test.replace(pathPrefix, '');
// strip out the suffix of the file name
pathName = pathName.replace(testSettings.testSuffix, '');
// the key in the entry determines the target folder structure of the test file
config.entry[pathName] = test;
});
config.output.path = testSettings.output;
let started = false;
function processResult(err, stats) {
gutil.log('Webpack\n' + stats.toString(config.log));
if (!started) {
started = true;
cb();
}
}
const compiler = webpack(config);
compiler.run(processResult);
});
});
gulp.task('test', ['clean-test', 'build-test'], () => {
return gulp.src([testSettings.output + '/**/*.js'])
.pipe(mocha())
});
gulp.task('server', (cb) => {
browserSync({
server: {
baseDir: [settings.server]
}
});
cb();
});
gulp.task('dev', (cb) => {
WATCH = true;
runSequence('build-dev', 'server', cb);
});
gulp.task('prod', (cb) => {
WATCH = true;
runSequence('build-prod', 'server', cb);
});
gulp.task('ci', ['build-ci'], () => {
return gulp.src(['dist/**', '!dist/client.zip'])
.pipe(zip('client.zip'))
.pipe(gulp.dest('dist'));
});
gulp.task('update-deps', (cb) => {
del(path.join(__dirname, 'npm-shrinkwrap.json')).then(() => {
execSync('npm update', {
cwd: __dirname,
stdio: 'inherit'
});
execSync('npm shrinkwrap', {
cwd: __dirname,
stdio: 'inherit'
});
execSync('npm outdated', {
cwd: __dirname,
stdio: 'inherit'
});
});
});
gulp.task('default', ['dev']);