-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
351 lines (287 loc) · 9.65 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
const gulp = require('gulp');
const path = require('path');
const { spawn } = require('child_process');
const PluginError = require('plugin-error');
const log = require('fancy-log');
const webpack = require('webpack');
const sass = require('sass');
const fs = require('fs');
function setProduction(done){
process.env.NODE_ENV = 'production';
done();
}
function setQuick(done){
process.env.NODE_ENV = 'quick';
done();
}
function setDevelopment(done){
process.env.NODE_ENV = 'development';
done();
}
function cleanBuildDirectory(done){
const buildDir = "./src/encoded/static/build/";
const pathsToDelete = [];
fs.readdir(buildDir, function(err, files){
files.forEach(function(fileName){
if (fileName === ".gitignore") { // Skip
return;
}
const filePath = path.resolve(buildDir, fileName);
pathsToDelete.push(filePath);
});
const filesToDeleteLen = pathsToDelete.length;
if (filesToDeleteLen === 0) {
done();
return;
}
var countDeleted = 0;
pathsToDelete.forEach(function(filePath){
fs.unlink(filePath, function(err){
countDeleted++;
if (countDeleted === filesToDeleteLen) {
console.log("Cleaned " + countDeleted + " files from " + buildDir);
done();
}
});
});
});
}
function webpackOnBuild(done) {
var start = Date.now();
return function (err, stats) {
if (err) {
throw new PluginError("webpack", err);
}
log("[webpack]", stats.toString({
colors: true
}));
var end = Date.now();
log("Build Completed, running for " + ((end - start)/1000)) + 's';
if (done) { done(err); }
};
}
function doWebpack(cb){
// Import in here so process.env.NODE_ENV changes are picked up.
const webpackConfig = require('./webpack.config.js');
webpack(webpackConfig).run(webpackOnBuild(cb));
}
function watch(){
// Import in here so process.env.NODE_ENV changes are picked up.
const webpackConfig = require('./webpack.config.js');
webpack(webpackConfig).watch(300, webpackOnBuild());
}
function getSharedPortalComponentsLink(){
let sharedComponentPath = path.resolve(__dirname, 'node_modules/@hms-dbmi-bgm/shared-portal-components');
const origPath = sharedComponentPath;
// Follow any symlinks to get to real path.
sharedComponentPath = fs.realpathSync(sharedComponentPath);
const isLinked = origPath !== sharedComponentPath;
console.log(
"`@hms-dbmi-bgm/shared-portal-components` directory is",
isLinked ? "sym-linked to `" + sharedComponentPath + "`." : "NOT sym-linked."
);
return { isLinked, sharedComponentPath: isLinked ? sharedComponentPath : null };
}
function buildSharedPortalComponents(done){
const { isLinked, sharedComponentPath } = getSharedPortalComponentsLink();
if (!isLinked){ // Exit
done();
return;
}
// Same as shared-portal-components own build method
const subP = spawn(
path.join(sharedComponentPath, 'node_modules/.bin/babel'),
[
path.join(sharedComponentPath, 'src'),
"--out-dir",
path.join(sharedComponentPath, 'es'),
"--env-name",
"esm"
],
{ stdio: "inherit" }
);
subP.on("error", (err)=>{
console.log(`buildSharedPortalComponents errored - ${err}`);
return;
});
subP.on("close", (code)=>{
console.log(`buildSharedPortalComponents process exited with code ${code}`);
done();
});
}
function watchSharedPortalComponents(done){
const { isLinked, sharedComponentPath } = getSharedPortalComponentsLink();
if (!isLinked){ // Exit
done();
return;
}
// Same as shared-portal-components own build method, but with "--watch"
const subP = spawn(
path.join(sharedComponentPath, 'node_modules/.bin/babel'),
[
path.join(sharedComponentPath, 'src'),
"--out-dir",
path.join(sharedComponentPath, 'es'),
"--env-name",
"esm",
"--watch"
],
{ stdio: "inherit" }
);
subP.on("error", (err)=>{
console.log(`watchSharedPortalComponents errored - ${err}`);
return;
});
subP.on("close", (code)=>{
console.log(`watchSharedPortalComponents process exited with code ${code}`);
done();
});
}
function getMicroscopyMetadataToolLink(){
let metadataToolPath = path.resolve(__dirname, 'node_modules/micro-meta-app-react');
const origPath = metadataToolPath;
// Follow any symlinks to get to real path.
metadataToolPath = fs.realpathSync(metadataToolPath);
const isLinked = origPath !== metadataToolPath;
console.log(
"`micro-meta-app-react` directory is",
isLinked ? "sym-linked to `" + metadataToolPath + "`." : "NOT sym-linked."
);
return { isLinked, metadataToolPath: isLinked ? metadataToolPath : null };
}
function buildMicroscopyMetadataTool(done){
const { isLinked, metadataToolPath } = getMicroscopyMetadataToolLink();
if (!isLinked){ // Exit
done();
return;
}
// Same as micro-meta-app-react own build method, but with "--watch"
const subP = spawn(
path.join(metadataToolPath, 'node_modules/.bin/babel'),
[
path.join(metadataToolPath, 'src'),
"--out-dir",
path.join(metadataToolPath, 'es'),
"--env-name",
"esm"
],
{ stdio: "inherit" }
);
subP.on("error", (err)=>{
console.log(`buildMicroscopyMetadataTool errored - ${err}`);
return;
});
subP.on("close", (code)=>{
console.log(`buildMicroscopyMetadataTool process exited with code ${code}`);
done();
});
}
function watchMicroscopyMetadataTool(done){
const { isLinked, metadataToolPath } = getMicroscopyMetadataToolLink();
if (!isLinked){ // Exit
done();
return;
}
// Same as micro-meta-app-react own build method, but with "--watch"
const subP = spawn(
path.join(metadataToolPath, 'node_modules/.bin/babel'),
[
path.join(metadataToolPath, 'src'),
"--out-dir",
path.join(metadataToolPath, 'es'),
"--env-name",
"esm",
"--watch"
],
{ stdio: "inherit" }
);
subP.on("error", (err)=>{
console.log(`watchMicroscopyMetadataTool errored - ${err}`);
return;
});
subP.on("close", (code)=>{
console.log(`watchMicroscopyMetadataTool process exited with code ${code}`);
done();
});
}
// TODO: Just use command-line `sass` ?
const cssOutputLocation = './src/encoded/static/css/style.css';
const sourceMapLocation = "./src/encoded/static/css/style.css.map";
const doSassBuild = (done, options = {}) => {
sass.render({
file: './src/encoded/static/scss/style.scss',
outFile: cssOutputLocation,
outputStyle: options.outputStyle || 'compressed',
sourceMap: sourceMapLocation
}, function(error, result) { // node-style callback from v3.0.0 onwards
if (error) {
console.error("Error", error.status, error.file, error.line + ':' + error.column);
console.log(error.message);
done();
} else {
//console.log(result.css.toString());
console.log("Finished compiling SCSS in", result.stats.duration, "ms");
console.log("Writing to", cssOutputLocation);
let countCompleted = 0;
fs.writeFile(cssOutputLocation, result.css.toString(), null, function(err){
if (err){
return console.error(err);
}
console.log("Wrote " + cssOutputLocation);
countCompleted++;
if (countCompleted === 2){
done();
}
});
fs.writeFile(sourceMapLocation, result.map.toString(), null, function(err){
if (err){
return console.error(err);
}
console.log("Wrote " + sourceMapLocation);
countCompleted++;
if (countCompleted === 2){
done();
}
});
}
});
};
//const devSlow = gulp.series(setDevelopment, doWebpack, watch);
//const buildQuick = gulp.series(setQuick, doWebpack);
const devQuick = gulp.series(
cleanBuildDirectory,
setQuick,
doWebpack,
gulp.parallel(watch, watchSharedPortalComponents, watchMicroscopyMetadataTool)
// `watchSharedPortalComponents` will update @hms-dbmi-bgm/shared-portal-components/es/,
// `watchMicroscopyMetadataTool` will update micro-meta-app-react/es/,
// which will be picked up by `watch` and recompiled into bundle.js
);
const devAnalyzed = gulp.series(
cleanBuildDirectory,
setDevelopment,
buildSharedPortalComponents,
buildMicroscopyMetadataTool,
doWebpack
);
const build = gulp.series(
cleanBuildDirectory,
setProduction,
doWebpack
);
//gulp.task('dev', devSlow);
//gulp.task('build-quick', buildQuick);
gulp.task('default', devQuick);
gulp.task('dev-quick', devQuick);
gulp.task('dev-analyzed', devAnalyzed);
gulp.task('build', build);
gulp.task('build-scss', (done) => doSassBuild(done, {}));
gulp.task('build-scss-dev', (done) => {
doSassBuild(
() => {
console.log('Watching for changes (if ran via `npm run watch-scss`)');
done();
},
{ outputStyle : 'expanded' }
);
});