-
Notifications
You must be signed in to change notification settings - Fork 5
/
fuse.js
454 lines (370 loc) · 16.8 KB
/
fuse.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
448
449
450
451
452
453
454
process.env.SPARKY_LOG = false;
const { FuseBox, Sparky } = require('fuse-box');
const { EnvPlugin, CSSPlugin, CSSResourcePlugin, RawPlugin, WebIndexPlugin, JSONPlugin, QuantumPlugin } = require('fuse-box');
const log = require('fliplog');
const express = require('express');
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs-extra');
const request = require('request');
const progress = require('request-progress');
const projectRoot = () => process.cwd();
projectRoot.backendRoot = () => path.join(projectRoot(), 'backend-bikesurbanfleets');
projectRoot.test = () => path.join(projectRoot(), 'examples/simulation-test');
projectRoot.test.conf = () => path.join(projectRoot(), 'examples/simulation-test/conf');
projectRoot.test.map = () => path.join(projectRoot(), 'examples/simulation-test/maps');
projectRoot.generateuserstest = () => path.join(projectRoot(), 'examples/usergeneration-test');
projectRoot.frontend = () => path.join(projectRoot(), 'frontend-bikesurbanfleets');
projectRoot.frontend.src = () => path.join(projectRoot.frontend(), 'src');
projectRoot.frontend.main = () => path.join(projectRoot.frontend.src(), 'main');
projectRoot.frontend.renderer = () => path.join(projectRoot.frontend.src(), 'renderer');
projectRoot.frontend.assets = () => path.join(projectRoot.frontend(), 'assets');
projectRoot.schema = () => path.join(projectRoot(), 'schema');
projectRoot.tools = () => path.join(projectRoot(), 'tools');
projectRoot.tools.jsonSchemaValidator = () => path.join(projectRoot.tools(), 'jsonschema-validator/src');
projectRoot.build = () => path.join(projectRoot(), 'build');
projectRoot.build.schema = () => path.join(projectRoot.build(), 'schema');
projectRoot.build.frontend = () => path.join(projectRoot.build(), 'frontend');
projectRoot.build.jsonSchemaValidator = () => path.join(projectRoot.build(), 'jsonschema-validator');
projectRoot.fuseCache = () => path.join(projectRoot(), '.fusebox');
projectRoot.schemaCacheDefaults = () => path.join(projectRoot(), '.schema/defaults');
projectRoot.schemaLayoutCacheDefaults = () => path.join(projectRoot(), '.schema/schemas-and-form-definitions');
let production = false;
let fromVSCode = false;
let schemaBuildPath = projectRoot.build.schema();
//BiciMad map
const overpassApiUrl = "http://overpass-api.de/api/interpreter?data=(node(40.362765146988,-3.8349151611328,40.504459339205,-3.5760498046875);<;);out;";
Sparky.task('clean:backend', () => new Promise((resolve, reject) => {
const maven = spawn('mvn', ['clean'], {
cwd: projectRoot.backendRoot(),
shell: true, // necessary for windows
stdio: 'inherit' // pipe to calling process
});
log.time().green('start cleaning and installing backend maven dependencies').echo();
maven.on('error', (error) => {
log.red(error).echo();
});
maven.on('close', (code) => {
if (code === 0) {
log.time().green('finished cleaning and install of backend maven dependencies').echo();
resolve();
} else {
log.time().red(`maven finished with error code ${code}`).echo();
reject();
}});
}));
Sparky.task('build:backend', () => new Promise((resolve, reject) => {
const maven = spawn('mvn', ['clean', 'package'], {
cwd: projectRoot.backendRoot(),
shell: true, // necessary for windows
stdio: 'inherit' // pipe to calling process
});
log.time().green('Started backend-bikesurbanfleets building').echo();
maven.on('error', (error) => {
log.red(error).echo();
});
maven.on('close', (code) => {
if (code === 0) {
let dirs = fs.readdirSync(projectRoot.backendRoot());
dirs = dirs.filter(dirName => dirName.startsWith("backend-bikesurbanfleets"))
.map(dirName => path.join(projectRoot.backendRoot(), `${dirName}/target`));
dirs.forEach(dirName => {
fs.readdirSync(dirName).filter((file) => file.endsWith('jar-with-dependencies.jar')).forEach((file) => {
const target = path.join(dirName, file);
const destination = path.join(projectRoot.build(), file.replace('-jar-with-dependencies', ''));
fs.copySync(target, destination);
log.time().green(`finished packaging ${file}`).echo();
})
});
log.time().green('backend-bikesurbanfleets build finished').echo();
resolve();
} else {
log.time().red(`maven finished with error code ${code}`).echo();
reject();
}
});
}));
Sparky.task('build:schema', ['clean:cache:schema'], () => new Promise((resolve, reject) => {
let command;
let tsc;
if(process.platform === 'darwin') {
command = "'" + path.join(projectRoot(), 'node_modules/.bin/tsc') + "'";
tsc = spawn(command, [], {
cwd: projectRoot.schema(),
shell: true,
stdio: 'inherit'
});
}
else {
tsc = spawn(path.join(projectRoot(), 'node_modules/.bin/tsc'), [], {
cwd: projectRoot.schema(),
shell: true,
stdio: 'inherit'
});
}
tsc.on('error', (error) => {
log.red(error).echo();
});
tsc.on('close', (code) => {
if (code === 0) {
log.time().green('compiling schemas').echo();
// Schema processing
fs.readdirSync(projectRoot.schemaCacheDefaults()).filter((file) => file.endsWith('.js')).forEach((file) => {
const allSchema = require(path.join(projectRoot.schemaCacheDefaults(), file));
const schema = allSchema.default;
const out = path.join(schemaBuildPath, `${file.slice(0, -3)}.json`);
schema.errors.forEach((error) => {
log.red(error).echo();
});
schema.write(out);
log.time().green(`written schema to ${out}`).echo();
});
// Layout processing
fs.readdirSync(projectRoot.schemaLayoutCacheDefaults()).filter((file) => file.endsWith('.js')).forEach((file) => {
const allLayouts = require(path.join(projectRoot.schemaLayoutCacheDefaults(), file));
const out = path.join(schemaBuildPath, `${file.slice(0, -3)}-layout.json`);
if(allLayouts.layout){
fs.writeJsonSync(out, allLayouts.layout, {spaces: 4});
log.time().green(`writen layout to ${out}`).echo();
}
});
//const globalLayout = require(path.join(projectRoot.schemaCacheDefaults(), 'global-config.js')).globalLayout;
//fs.writeJSONSync();
resolve();
} else {
log.time().red(`tsc finished with error code ${code}`).echo();
reject();
}
});
}));
Sparky.task('build:schema-test', () => new Promise((resolve, reject) => {
log.time().green('compiling schemas').echo();
// Schema processing
fs.readdirSync(projectRoot.schemaCacheDefaults()).filter((file) => file.endsWith('.js')).forEach((file) => {
const allSchema = require(path.join(projectRoot.schemaCacheDefaults(), file));
const schema = allSchema.default;
const out = path.join(schemaBuildPath, `${file.slice(0, -3)}.json`);
schema.errors.forEach((error) => {
log.red(error).echo();
});
schema.write(out);
log.time().green(`written schema to ${out}`).echo();
});
// Layout processing
fs.readdirSync(projectRoot.schemaLayoutCacheDefaults()).filter((file) => file.endsWith('.js')).forEach((file) => {
const allLayouts = require(path.join(projectRoot.schemaLayoutCacheDefaults(), file));
const out = path.join(schemaBuildPath, `${file.slice(0, -3)}-layout.json`);
if(allLayouts.layout){
fs.writeJsonSync(out, allLayouts.layout, {spaces: 4});
log.time().green(`writen layout to ${out}`).echo();
}
});
//const globalLayout = require(path.join(projectRoot.schemaCacheDefaults(), 'global-config.js')).globalLayout;
//fs.writeJSONSync();
resolve();
}));
Sparky.task('build:jsonschema-validator', () => {
const fuse = FuseBox.init({
homeDir: projectRoot.tools.jsonSchemaValidator(),
output: path.join(projectRoot.build.jsonSchemaValidator(), '$name.js'),
experimentalFeatures: true,
plugins: [
JSONPlugin()
]
});
fuse.bundle("jsonschema-validator.js").instructions(`>index.ts`);
fuse.run();
});
Sparky.task('build:frontend:main', () => {
const fuse = FuseBox.init({
homeDir: projectRoot.frontend.src(),
output: path.join(projectRoot.build.frontend(), '$name.js'),
sourceMaps: {
project: !production,
vendor: false, // vendor sourcemaps take very long to generate
},
target: 'server',
experimentalFeatures: true,
ignoreModules: ['electron'],
plugins: [
EnvPlugin({ target: production ? 'production' : 'development' }),
JSONPlugin()
]
});
const main = fuse.bundle('main').instructions('>main/main.ts');
if (!production && !fromVSCode) {
// main.watch('main/**');
return fuse.run().then(() => {
const electron = spawn('npm', ['run', 'start:electron'], {
cwd: projectRoot(),
shell: true, // necessary on windows
stdio: 'inherit' // pipe to calling process
});
});
}
return fuse.run();
});
Sparky.task('build:frontend:renderer', () => {
const fuse = FuseBox.init({
homeDir: projectRoot.frontend.src(),
output: path.join(projectRoot.build.frontend(), '$name.js'),
sourceMaps: {
project: !production,
vendor: false, // vendor sourcemaps take very long to generate
},
target: 'electron',
experimentalFeatures: true,
cache: !production,
plugins: [
EnvPlugin({ target: production ? 'production' : 'development' }),
['*.component.html', RawPlugin()],
['*.component.css', RawPlugin()],
[CSSResourcePlugin({ inline: true }), CSSPlugin()],
WebIndexPlugin({
template: path.join(projectRoot.frontend.renderer(), 'index.html'),
path: '.'
}),
JSONPlugin()
]
});
const rendererEntrypoint = 'renderer/renderer.ts';
const vendor = fuse.bundle('vendor').instructions(`~ ${rendererEntrypoint}`);
const renderer = fuse.bundle('renderer').instructions(`!> [${rendererEntrypoint}]`);
/*if (!production) {
fuse.dev({ root: false }, (server) => {
const app = server.httpServer.app;
app.use('/renderer/', express.static(projectRoot.build.frontend()));
app.get('*', (request, response) => {
response.send(path.join(projectRoot.build.frontend(), 'index.html'));
});
// TODO: make the server close on electron window close (note: apparently not possible)
});
renderer.hmr().watch('renderer/**');
}*/
//Global css file to build
const globalCss = path.join(projectRoot.frontend.renderer(), 'styles.css');
const destination = path.join(projectRoot.build.frontend(), 'styles.css');
fs.copySync(globalCss, destination);
const packageProdOrig = path.join(projectRoot(), 'package_prod.json');
const packageProdDest = path.join(projectRoot.build(), 'package.json');
fs.copySync(packageProdOrig, packageProdDest);
//Icons to build - Windows
const originIconWin = path.join(projectRoot.frontend.assets(), 'icon.ico');
const destinationIconWin = path.join(projectRoot.build(), 'icon.ico');
fs.copySync(originIconWin, destinationIconWin);
//Icons to build - Mac And Debian
const originIconMac = path.join(projectRoot.frontend.assets(), 'icon.icns');
const destinationIconMac = path.join(projectRoot.build(), 'icon.icns');
fs.copySync(originIconMac, destinationIconMac);
return fuse.run();
});
Sparky.task('gen-users:dev', () => new Promise((resolve, reject) => {
const userGen = spawn('java', [
`-jar`,
`bikesurbanfleets-config-usersgenerator-1.0.jar`,
'-entryPointsInput', '"' + path.join(projectRoot.generateuserstest(), 'entryPoints.json') + '"',
'-globalInput', '"' + path.join(projectRoot.generateuserstest(), 'global_configuration.json') + '"',
'-output', '"' + projectRoot.generateuserstest() + '/users_configuration.json"',
'-callFromFrontend'
], {
cwd: projectRoot.build(),
shell: true, // necessary for windows
stdio: 'inherit' // pipe to calling process
});
log.time().green('Starting user generation').echo();
userGen.on('error', (error) => {
log.red(error).echo();
});
userGen.on('close', (code) => {
if (code === 0) {
log.time().green('Finished user generation').echo();
resolve();
} else {
log.time().red(`User generation finished with code ${code}`).echo();
reject();
}});
}));
Sparky.task('simulate:dev', () => new Promise((resolve, reject) => {
const userGen = spawn('java', [
`-jar`,
`bikesurbanfleets-core-1.0.jar`,
'-globalConfig', '"' + path.join(projectRoot.test.conf(), 'global_configuration.json') + '"',
'-usersConfig', '"' + path.join(projectRoot.test.conf(), 'users_configuration.json') + '"',
'-stationsConfig', '"' + path.join(projectRoot.test.conf(), 'stations_configuration.json') + '"',
'-historyOutput', '"' + path.join(projectRoot.test(), 'history') + '"',
'-analysisOutput', '"' + path.join(projectRoot.test(), 'analysis') + '"',
`-callFromFrontend`
], {
cwd: projectRoot.build(),
shell: true, // necessary for windows
stdio: 'inherit' // pipe to calling process
});
log.time().green('Starting test simulation').echo();
userGen.on('error', (error) => {
log.red(error).echo();
});
userGen.on('close', (code) => {
if (code === 0) {
log.time().green('Finished test simulation').echo();
resolve();
} else {
log.time().red(`Test simulation finished with code ${code}`).echo();
reject();
}});
}));
Sparky.task('copy:assets', async () => {
await fs.copy(path.join(projectRoot.frontend(), 'assets'), path.join(projectRoot.build.frontend(), 'assets'));
});
Sparky.task('download-map:dev', () => new Promise((resolve, reject) => {
log.time().green('Downloading osm map for development.').echo();
if(!fs.existsSync(projectRoot.test())) {
fs.mkdirSync(projectRoot.test());
}
if(!fs.existsSync(projectRoot.test.map())) {
fs.mkdirSync(projectRoot.test.map());
}
let mapFile = path.resolve(projectRoot.test.map(), 'madrid.osm');
if(fs.existsSync(mapFile)) {
log.time().green('Map is currently downloaded').echo();
resolve();
}
else {
let mapDownloadedFile = fs.createWriteStream(mapFile);
this.request = request(overpassApiUrl);
progress(this.request)
.on('progress', (state) => {
process.stdout.write('Speed: ' + parseFloat(state.speed / 1024 / 1024).toFixed(2) +
' MB/s - Downloaded: ' + parseFloat(state.size.transferred / 1024 / 1024).toFixed(2) + ' MB\r');
})
.on('error', (err) => {
console.log(err);
this.request = undefined;
reject();
})
.on('end', () => {
this.request = undefined;
log.time().green('Downloaded map in: ' + mapFile).echo();
resolve();
})
.pipe(mapDownloadedFile);
}
}));
Sparky.task('clean:build', () => Sparky.src(projectRoot.build()).clean(projectRoot.build()));
Sparky.task('clean:cache:fuse', () => Sparky.src(projectRoot.fuseCache()).clean(projectRoot.fuseCache()));
Sparky.task('clean:cache:schema', () => Sparky.src(projectRoot.schemaCacheDefaults()).clean(projectRoot.schemaCacheDefaults()));
Sparky.task('clean:cache', ['clean:cache:fuse', 'clean:cache:schema'], () => {});
Sparky.task('build:frontend', ['copy:assets', 'build:frontend:renderer', 'build:frontend:main'], () => {});
Sparky.task('build:frontend:vscode', () => {
fromVSCode = true;
return Sparky.start('build:frontend');
});
Sparky.task('build:dev-backend', ['clean:build', 'clean:cache', 'build:backend', 'build:schema', 'build:jsonschema-validator'], () => {});
Sparky.task('configure:dev', ['download-map:dev', 'build:dev-backend'], () => {});
Sparky.task('build:dist', ['download-map:dev', 'build:dev-backend'], () => {
production = true;
return Sparky.start('build:frontend');
});
Sparky.task('build:schema:forBackend', () => {
schemaBuildPath = path.join(projectRoot.backend(), 'schema');
return Sparky.start('build:schema');
});