/
electron-forge-make.js
112 lines (98 loc) · 3.69 KB
/
electron-forge-make.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
import 'colors';
import fs from 'fs-promise';
import path from 'path';
import program from 'commander';
import ora from 'ora';
import './util/terminate';
import electronHostArch from './util/electron-host-arch';
import getForgeConfig from './util/forge-config';
import packager from './electron-forge-package';
import resolveDir from './util/resolve-dir';
const main = async () => {
const resolveSpinner = ora.ora('Resolving Forge Config').start();
let dir = process.cwd();
program
.version(require('../package.json').version)
.arguments('[cwd]')
.option('-s, --skip-package', 'Assume the app is already packaged')
.option('-a, --arch [arch]', 'Target architecture')
.option('-p, --platform [platform]', 'Target build platform')
.action((cwd) => {
if (!cwd) return;
if (path.isAbsolute(cwd) && fs.existsSync(cwd)) {
dir = cwd;
} else if (fs.existsSync(path.resolve(dir, cwd))) {
dir = path.resolve(dir, cwd);
}
})
.parse(process.argv);
dir = await resolveDir(dir);
if (!dir) {
resolveSpinner.fail();
console.error('Failed to locate makeable Electron application'.red);
if (global._resolveError) global._resolveError();
process.exit(1);
}
resolveSpinner.succeed();
if (program.platform && program.platform !== process.platform && !(process.platform === 'darwin' && program.platform === 'mas')) {
console.error('You can not "make" for a platform other than your systems platform'.red);
process.exit(1);
}
if (!program.skipPackage) {
console.info('We need to package your application before we can make it'.green);
await packager();
} else {
console.warn('WARNING: Skipping the packaging step, this could result in an out of date build'.red);
}
const declaredArch = program.arch || electronHostArch();
const declaredPlatform = program.platform || process.platform;
const forgeConfig = await getForgeConfig(dir);
const targets = forgeConfig.make_targets[declaredPlatform];
console.info('Making for the following targets:', `${targets.join(', ')}`.cyan);
let targetArchs = [declaredArch];
if (declaredArch === 'all') {
switch (process.platform) {
case 'darwin':
targetArchs = ['x64'];
break;
case 'linux':
targetArchs = ['ia32', 'x64', 'armv7l'];
break;
case 'win32':
default:
targetArchs = ['ia32', 'x64'];
break;
}
}
const packageJSON = JSON.parse(await fs.readFile(path.resolve(dir, 'package.json'), 'utf8'));
const appName = packageJSON.productName || packageJSON.name;
for (const targetArch of targetArchs) {
const packageDir = path.resolve(dir, `out/${appName}-${declaredPlatform}-${targetArch}`);
if (!(await fs.exists(packageDir))) {
throw new Error(`Couldn't find packaged app at: ${packageDir}`);
}
for (const target of targets) {
const makeSpinner = ora.ora(`Making for target: ${target.cyan} - On platform: ${declaredPlatform.cyan} - For arch: ${targetArch.cyan}`).start();
let maker;
try {
maker = require(`./makers/${process.platform}/${target}.js`);
} catch (err1) {
try {
maker = require(`./makers/generic/${target}.js`);
} catch (err2) {
makeSpinner.fail();
throw new Error(`Could not find a build target with the name: ${target} for the platform: ${declaredPlatform}`);
}
}
try {
await (maker.default || maker)(packageDir, appName, targetArch, forgeConfig, packageJSON);
} catch (err) {
makeSpinner.fail();
if (err) throw err;
throw new Error(`An error occurred while making for target: ${target}`);
}
makeSpinner.succeed();
}
}
};
main();