/
start.ts
151 lines (128 loc) · 4.25 KB
/
start.ts
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
import 'colors';
import { asyncOra } from '@electron-forge/async-ora';
import { StartOptions, ForgePlatform, ForgeArch } from '@electron-forge/shared-types';
import { spawn, ChildProcess, SpawnOptions } from 'child_process';
import path from 'path';
import { readMutatedPackageJson } from '../util/read-package-json';
import rebuild from '../util/rebuild';
import resolveDir from '../util/resolve-dir';
import getForgeConfig from '../util/forge-config';
import { runHook } from '../util/hook';
import { getElectronVersion } from '../util/electron-version';
export { StartOptions };
export default async ({
dir = process.cwd(),
appPath = '.',
interactive = false,
enableLogging = false,
args = [],
runAsNode = false,
inspect = false,
}: StartOptions) => {
asyncOra.interactive = interactive;
await asyncOra('Locating Application', async () => {
const resolvedDir = await resolveDir(dir);
if (!resolvedDir) {
throw new Error('Failed to locate startable Electron application');
}
dir = resolvedDir;
});
const forgeConfig = await getForgeConfig(dir);
const packageJSON = await readMutatedPackageJson(dir, forgeConfig);
if (!packageJSON.version) {
throw new Error(`Please set your application's 'version' in '${dir}/package.json'.`);
}
await rebuild(
dir,
await getElectronVersion(dir, packageJSON),
process.platform as ForgePlatform,
process.arch as ForgeArch,
forgeConfig.electronRebuildConfig,
);
await runHook(forgeConfig, 'generateAssets');
let lastSpawned: ChildProcess | null = null;
const forgeSpawn = async () => {
let electronExecPath: string | null = null;
// If a plugin has taken over the start command let's stop here
const spawnedPluginChild = await forgeConfig.pluginInterface.overrideStartLogic({
dir,
appPath,
interactive,
enableLogging,
args,
runAsNode,
inspect,
});
let prefixArgs: string[] = [];
if (typeof spawnedPluginChild === 'string') {
electronExecPath = spawnedPluginChild;
} else if (Array.isArray(spawnedPluginChild)) {
[electronExecPath, ...prefixArgs] = spawnedPluginChild;
} else if (spawnedPluginChild) {
await runHook(forgeConfig, 'postStart', spawnedPluginChild);
return spawnedPluginChild;
}
if (!electronExecPath) {
// eslint-disable-next-line import/no-dynamic-require, global-require
electronExecPath = require(path.resolve(dir, 'node_modules/electron'));
}
const spawnOpts = {
cwd: dir,
stdio: 'inherit',
env: ({
...process.env,
...(enableLogging ? {
ELECTRON_ENABLE_LOGGING: 'true',
ELECTRON_ENABLE_STACK_DUMPING: 'true',
} : {}),
}) as NodeJS.ProcessEnv,
};
if (runAsNode) {
spawnOpts.env.ELECTRON_RUN_AS_NODE = 'true';
} else {
delete spawnOpts.env.ELECTRON_RUN_AS_NODE;
}
if (inspect) {
args = ['--inspect' as (string|number)].concat(args);
}
let spawned!: ChildProcess;
await asyncOra('Launching Application', async () => {
spawned = spawn(
electronExecPath!,
prefixArgs.concat([appPath]).concat(args as string[]),
spawnOpts as SpawnOptions,
);
});
await runHook(forgeConfig, 'postStart', spawned);
return spawned;
};
const forgeSpawnWrapper = async () => {
const spawned = await forgeSpawn();
// When the child app is closed we should stop listening for stdin
if (spawned) {
if (interactive && process.stdin.isPaused()) {
process.stdin.resume();
}
spawned.on('exit', () => {
if ((spawned as any).restarted) return;
if (!process.stdin.isPaused()) process.stdin.pause();
});
} else if (interactive && !process.stdin.isPaused()) {
process.stdin.pause();
}
lastSpawned = spawned;
return lastSpawned;
};
if (interactive) {
process.stdin.on('data', async (data) => {
if (data.toString().trim() === 'rs' && lastSpawned) {
// eslint-disable-next-line no-console
console.info('\nRestarting App\n'.cyan);
(lastSpawned as any).restarted = true;
lastSpawned.kill('SIGTERM');
lastSpawned.emit('restarted', await forgeSpawnWrapper());
}
});
}
return forgeSpawnWrapper();
};