-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
487 lines (428 loc) · 14.9 KB
/
cli.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env node
'use strict';
const { name, version } = require('./package.json');
const process = require('process');
const fs = require('fs');
const path = require('path');
const { ncp } = require('ncp');
const rimraf = require("rimraf");
const mkdirp = require('mkdirp');
const { spawn, execFile } = require('child_process');
const { path7za } = require('7zip-bin');
const { appBuilderPath } = require('app-builder-bin');
const cli = (function() {
const buildDir = path.resolve(__dirname, 'build');
const packageDir = path.resolve(buildDir, `${name}-${version}`);
const appsDir = path.resolve(packageDir, 'apps');
const stockPortfolio = {
srcDir: path.resolve(__dirname, 'packages/stock-portfolio/dist'),
destDir: appsDir
};
const stockCharts = {
srcDir: path.resolve(__dirname, 'packages/stock-charts/build'),
destDir: path.resolve(appsDir, 'stock-charts')
};
const stockTrading = {
srcDir: path.resolve(__dirname, 'packages/stock-trading/dist'),
destDir: path.resolve(appsDir, 'stock-trading')
};
const openfinRvm = {
srcPath: path.resolve(__dirname, 'resources/win/OpenFinRVM.exe'),
destPath: path.resolve(packageDir, 'OpenFinRVM.exe'),
};
const openfinLauncher = {
srcPath: path.resolve(__dirname, 'resources/win/run.cmd'),
destPath: path.resolve(packageDir, 'run.cmd')
};
const node = {
srcPath: path.resolve(__dirname, 'resources/win/node_6.5.0.exe'),
destPath: path.resolve(packageDir, 'node_6.5.0.exe'),
};
const nodeScript = {
srcPath: path.resolve(__dirname, 'src/index.js'),
destPath: path.resolve(packageDir, 'index.js'),
};
function executeCommand(command) {
switch (command) {
case '--build-package':
buildPackage();
break;
case '--build-portable':
buildPortable();
break;
case '--update-version':
updateVersion();
break;
default:
console.log(`Unknown command "${command}" specified`);
process.exit(1);
break;
}
}
function updateVersion() {
console.log('Updating version...');
const filePath = path.resolve(__dirname, 'packages/stock-core/version.js');
const fileContent = [
`// IMPORTANT: THIS FILE IS AUTO GENERATED!`,
`export const Version = '${version}';`
].join('\n');
fs.writeFile(filePath, fileContent, 'utf8', (err) => {
if (err) {
console.error('Error occured while updating version');
console.error(err);
process.exit(1);
}
console.log("Version updated successfully");
});
}
function buildPortable() {
console.log('Building portable...');
const exports = {
buildDir: buildDir,
packageDir: packageDir,
removeFile: removeFile,
readFile: readFile
};
NsisApi.buildPortable(exports).then(() => {
console.log("Portable built successfully");
}).catch((err) => {
console.error('Error occured while building portable');
console.error(err);
process.exit(1);
});
}
function buildPackage() {
console.log('Building package...');
Promise.resolve()
.then(() => removeDir(appsDir))
.then(() => makeDir(appsDir))
.then(() => {
return Promise.all([
copyDir(stockPortfolio.srcDir, stockPortfolio.destDir),
copyDir(stockCharts.srcDir, stockCharts.destDir),
copyDir(stockTrading.srcDir, stockTrading.destDir),
copyFile(openfinRvm.srcPath, openfinRvm.destPath),
copyFile(openfinLauncher.srcPath, openfinLauncher.destPath),
copyFile(node.srcPath, node.destPath),
copyFile(nodeScript.srcPath, nodeScript.destPath)
]);
})
.then(() => {
console.log('Package built successfully');
process.exit(0);
})
.catch(err => {
console.error('Error occured while building package');
console.error(err);
process.exit(1);
});
}
function removeFile(path) {
if (!fs.existsSync(path)) {
return Promise.resolve();
}
let promise = new Promise((resolve, reject) => {
fs.unlink(path, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promise;
}
function readFile(path) {
let promise = new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
return promise;
}
function copyFile(sourcePath, destPath) {
let promise = new Promise((resolve, reject) => {
fs.copyFile(sourcePath, destPath, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promise;
}
function removeDir(path) {
let promise = new Promise((resolve, reject) => {
rimraf(path, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promise;
}
function copyDir(sourcePath, destPath) {
let promise = new Promise((resolve, reject) => {
ncp.limit = 16;
ncp(sourcePath, destPath, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promise;
}
function makeDir(path) {
let promise = new Promise((resolve, reject) => {
mkdirp(path, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promise;
}
return {
run: function () {
const args = process.argv.slice(2);
if (args.length !== 1) {
console.log(`Incorrect command "${args}" specified`);
process.exit(1);
}
executeCommand(args[0]);
}
};
})();
const NsisApi = (function() {
const nsisScript = {
path: path.resolve(__dirname, 'resources/nsis/portable.nsi'),
};
const binariesCache = new Map();
function archive7z(outFile, dirToArchive) {
const args = [
'a',
'-bd',
'-mx=9',
'-mtc=off',
'-mtm=off',
'-mta=off',
outFile,
'.'
];
const options = {
cwd: dirToArchive
};
return exec(path7za, args, options);
}
function exec(file, args, options){
return new Promise((resolve, reject) => {
execFile(file, args, {
...options,
maxBuffer: 1000 * 1024 * 1024
}, (error, stdout, stderr) => {
if (error == null) {
resolve(stdout.toString());
}
else {
let message = `Exit code: ${error.code}. ${error.message}`;
if (stdout.length !== 0) {
message += `\n${stdout.toString()}`;
}
if (stderr.length !== 0) {
message += `\n${stderr.toString()}`;
}
reject(new Error(message));
}
});
});
}
function makeNsis(defines, commands, script) {
const binaries = {};
return getBinFromUrl("nsis", "3.0.4", "MNETIF8tex6+oiA0mgBi3/XKNH+jog4IBUp/F+Or7zUEhIP+c7cRjb9qGuBIofAXQ51z3RpyCfII4aPadsZB5Q==")
.then(nsisPath => {
binaries.nsisPath = nsisPath;
console.log(`NsisPath: ${nsisPath}`);
return getBinFromUrl("nsis-resources", "3.4.1", "Dqd6g+2buwwvoG1Vyf6BHR1b+25QMmPcwZx40atOT57gH27rkjOei1L0JTldxZu4NFoEmW4kJgZ3DlSWVON3+Q==");
})
.then(nsisResourcePath => {
binaries.nsisResourcePath = nsisResourcePath;
console.log(`NsisResourcePath: ${nsisResourcePath}`);
})
.then(() => {
const command = path.join(
binaries.nsisPath,
process.platform === "darwin" ? "mac" : (process.platform === "win32" ? "Bin" : "linux"),
process.platform === "win32" ? "makensis.exe" : "makensis");
const args = buildNsisArgs(defines, commands, binaries.nsisResourcePath);
return spawnAndWrite(command, args, script, {
env: {
...process.env,
NSISDIR: binaries.nsisPath
}
});
});
}
function buildNsisArgs(defines, commands, nsisResourcePath) {
const args = ["-WX"];
const pluginsArch = 'x86-ansi';
defines.PLUGINS_ARCH = pluginsArch;
defines.PLUGINS_PATH = path.resolve(nsisResourcePath, 'plugins', pluginsArch);
for (const name of Object.keys(defines)) {
const value = defines[name];
args.push(`-D${name}=${value}`);
}
for (const name of Object.keys(commands)) {
const value = commands[name];
args.push(`-X${name} ${value}`);
}
args.push("-");
return args;
}
function spawnAndWrite(command, args, data, options) {
try {
const childProcess = spawn(command, args, {
env: options.env,
stdio: ["pipe", "ignore", "inherit"]
});
const timeout = setTimeout(() => childProcess.kill(), 4 * 60 * 1000);
return new Promise((resolve, reject) => {
handleProcess("close", childProcess, command, () => {
try {
clearTimeout(timeout);
}
finally {
resolve();
}
}, error => {
try {
clearTimeout(timeout);
}
finally {
reject(error.stack || error.toString());
}
});
const stdin = childProcess.stdin;
if (stdin) {
stdin.end(data);
}
});
}
catch (e) {
throw new Error(`Cannot spawn ${command}: ${e.stack || e}`);
}
}
function getBinFromUrl(name, version, checksum) {
const dirName = `${name}-${version}`;
const baseUrl = "https://github.com/electron-userland/electron-builder-binaries/releases/download/";
let url = `${baseUrl}${dirName}/${dirName}.7z`;
return getBin(dirName, url, checksum);
}
function getBin(name, url, checksum) {
let promise = binariesCache.get(name);
// if rejected, we will try to download again
if (promise != null) {
return promise;
}
promise = doGetBin(name, url, checksum);
binariesCache.set(name, promise);
return promise;
}
function doGetBin(name, url, checksum) {
const args = ["download-artifact", "--name", name];
if (url != null) {
args.push("--url", url);
}
if (checksum != null) {
args.push("--sha512", checksum);
}
return executeAppBuilder(args);
}
function executeAppBuilder(args) {
return new Promise((resolve, reject) => {
const command = appBuilderPath;
const env = {
...process.env,
SZA_PATH: path7za
};
try {
const childProcess = spawn(command, args, {
env,
stdio: ["ignore", "pipe", process.stdout]
});
handleProcess("close", childProcess, command, resolve, error => {
reject(error);
});
}
catch (e) {
throw new Error(`Cannot spawn ${command}: ${e.stack || e}`);
}
});
}
function handleProcess(event, childProcess, command, resolve, reject) {
childProcess.on("error", reject);
let out = "";
if (childProcess.stdout != null) {
childProcess.stdout.on("data", (data) => {
out += data;
});
}
let errorOut = "";
if (childProcess.stderr != null) {
childProcess.stderr.on("data", (data) => {
errorOut += data;
});
}
childProcess.once(event, (code) => {
if (code === 0) {
if (resolve != null) {
resolve(out);
}
}
else {
let message = `${command} exited with code ${code}`;
message += `${formatOut(out, "Output")}`;
message += `${formatOut(errorOut, "Error output")}`;
reject(new Error(message));
}
});
}
function formatOut(text, title) {
return text.length === 0 ? "" : `\n${title}:\n${text}`;
}
return {
buildPortable: function(io) {
const archiveFile = path.resolve(io.buildDir, `${name}-${version}-x64.nsis.7z`);
const defines = {
APP_64: archiveFile,
APP_NAME: name,
APP_VERSION: version,
APP_EXE: 'run.cmd --wait-for-exit'
};
const commands = {
OutFile: path.resolve(io.buildDir, `${name}-${version}.exe`),
Icon: path.resolve(__dirname, 'resources/icon.ico')
};
return io.removeFile(archiveFile).then(() => {
return archive7z(archiveFile, io.packageDir);
}).then(() => {
return io.readFile(nsisScript.path);
}).then(script => {
return makeNsis(defines, commands, script);
}).then(() => {
return io.removeFile(archiveFile);
});
}
};
})();
cli.run();