-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.js
334 lines (279 loc) · 7.4 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
// Require environment
require('./lib/env');
// Require dependencies
const fs = require('fs-extra');
const Cp = require('child_process');
const gulp = require('gulp');
const glob = require('@edenjs/glob');
const util = require('util');
const fetch = require('node-fetch');
const config = require('config');
const deepMerge = require('deepmerge');
// Require local dependencies
const loader = require('lib/loader');
const parser = require('lib/utilities/parser');
/**
* Create Loader class
*/
class Loader {
/**
* Construct Loader class
*/
constructor() {
// Bind public methods
this.build = this.build.bind(this);
this.files = this.files.bind(this);
this.restart = this.restart.bind(this);
this.merge = util.deprecate(this.merge, 'Please use a custom method instead').bind(this);
// Bind private methods
this._task = this._task.bind(this);
this._watch = this._watch.bind(this);
this.server = null;
this.serverRestartingPromise = null;
this.serverRestartWaiting = false;
// Run build
this.build();
// Add dev server task
gulp.task('server', gulp.series('install', () => {
this.restart(true);
gulp.task('watch')();
}));
// Build default task
gulp.task('default', gulp.series('server'));
}
/**
* Build Loader
*
* This has to be a sync method because gulp won't change core to allow async task loading
*/
build() {
fs.ensureDirSync(`${global.appRoot}/data/cache`);
// Glob tasks
let done = [];
this._locations = global.bundleLocations;
// Get files
const tasks = glob.sync(this.files('tasks/*.js'));
const watchers = [];
const installers = [];
// Loop tasks
for (const rawTask of tasks) {
// Load task
const task = parser.task(rawTask);
// Create task
const Task = this._task(task);
// Add to default task
if (done.indexOf(task.task) === -1) installers.push(task.task);
// Push to done
done.push(task.task);
// Check befores
if (task.before) {
// Push to dones
done = done.concat(task.before);
// Remove before from defaults
for (const taskBefore of task.before) {
// Set index
const index = installers.indexOf(taskBefore);
// Check defaults
if (index > -1) installers.splice(index, 1);
}
}
// Check afters
if (task.after) {
// Push to dones
done = done.concat(task.after);
// Remove after from defaults
for (const taskAfter of task.after) {
// Set index
const index = installers.indexOf(taskAfter);
// Check defaults
if (index > -1) installers.splice(index, 1);
}
}
// Add watch to watchers
if (Task.watch) watchers.push(`${task.task}.watch`);
}
// Create tasks
gulp.task('watch', gulp.parallel(...watchers));
gulp.task('install', gulp.series(...installers));
}
/**
* restarts server
*/
async _restart() {
// set restarting
this.serverRestartingPromise = true;
// set server
if (this.server !== null) {
// dead promise
const deadPromise = new Promise(resolve => this.server.once('exit', resolve));
// kill server
this.server.kill();
// await dying
await deadPromise;
}
// server
this.server = Cp.fork(`${__dirname}/index.js`, ['start']);
}
/**
* Restarts dev server
*/
restart(create = false) {
// Clearly not a production env
process.env.NODE_ENV = 'development';
if (this.server === null && !create) {
// Nothing to restart, and not initial start
return;
}
if (this.serverRestartWaiting) {
// Wait for other queue'd task to run instead of ours, they are just as good
return;
}
(async () => {
let didSetWaiting = false;
// Already ongoing, lets wait
if (this.serverRestartingPromise !== null) {
// Note that we set waiting, so we can unset it
didSetWaiting = true;
// Let other callers know we're already waiting
this.serverRestartWaiting = true;
// Wait for ongoing task
await this.serverRestartingPromise;
}
// Set ongoing to be our task
this.serverRestartingPromise = this._restart();
// Let other callers know we're done waiting
if (didSetWaiting) this.serverRestartWaiting = false;
// Await our task
await this.serverRestartingPromise;
// Reset ongoing task promise
this.serverRestartingPromise = null;
})();
}
/**
* Emits Args
*
* @param {String} type
* @param {...any} args
*/
async emit(type, ...args) {
// try/catch
try {
// emit build event
await fetch(`http://localhost:${config.get('port')}/dev/event`, {
body : JSON.stringify({
type,
args,
}),
headers : {
'Content-Type' : 'application/json',
authentication : `AUTH:${config.get('secret')}`,
},
method : 'POST',
});
} catch (e) {
// Remove build errors by setting a random variable
let gotErrorMessage = e;
if (gotErrorMessage !== null) {
gotErrorMessage = null;
}
}
}
/**
* Writes config file
*
* @param {string} name
* @param {object} obj
*/
async write(name, obj) {
// Write file
await fs.writeJson(`${global.appRoot}/data/cache/${name}.json`, obj);
}
/**
* Merges two objects
*
* @param {object} obj1
* @param {object} obj2
*
* @returns {object}
*/
merge(obj1, obj2) {
return deepMerge(obj1, obj2);
}
/**
* gets files
*
* @param {Array} files
*/
files(files) {
// return not included
return [...(loader.getFiles(files, this._locations).filter(i => !(config.get('ignore') || []).find(c => i.includes(c)))), ...(config.get('ignore') || []).map((i) => {
return `!${i}/*/**`;
})];
}
/**
* Runs gulp task
*
* @param {object} task
*
* @returns {*}
*
* @private
*/
_task(task) {
// Create task
let Task = require(task.file); // eslint-disable-line global-require, import/no-dynamic-require
// New task
Task = new Task(this);
// Create gulp task
gulp.task(`${task.task}.run`, () => {
// return task
return Task.run(Task.watch ? this.files(Task.watch()) : undefined);
});
// Create task args
let args = [];
// Check before
if (task.before && task.before.length) {
// Push to args
args = args.concat(task.before);
}
// Push actual function
args.push(`${task.task}.run`);
// Check after
if (task.after && task.after.length) {
// Push to args
args = args.concat(task.after);
}
// Create task
gulp.task(task.task, gulp.series(...args));
// Check watch
if (Task.watch) {
// Create watch task
this._watch(task.task, Task);
}
// Return task
return Task;
}
/**
* Creates watch task
*
* @param {string} task
* @param {*} Task
*
* @private
*/
_watch(task, Task) {
// Create watch task
gulp.task(`${task}.watch`, () => {
// return watch
return gulp.watch(this.files(Task.watch()), {
awaitWriteFinish : true,
}, gulp.series(task));
});
}
}
/**
* Export new Loader instance
*
* @type {Loader}
*/
module.exports = new Loader();