-
Notifications
You must be signed in to change notification settings - Fork 175
/
rebuild.ts
339 lines (302 loc) · 10.4 KB
/
rebuild.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
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
import debug from 'debug';
import { EventEmitter } from 'events';
import * as fs from 'fs-extra';
import * as nodeAbi from 'node-abi';
import * as os from 'os';
import * as path from 'path';
import { generateCacheKey, lookupModuleState } from './cache';
import { BuildType, IRebuilder, RebuildMode } from './types';
import { ModuleRebuilder } from './module-rebuilder';
import { ModuleType, ModuleWalker } from './module-walker';
export interface RebuildOptions {
/**
* The path to the `node_modules` directory to rebuild.
*/
buildPath: string;
/**
* The version of Electron to build against.
*/
electronVersion: string;
/**
* Override the target platform to something other than the host system platform.
* Note: This only applies to downloading prebuilt binaries. **It is not possible to cross-compile native modules.**
*
* @defaultValue The system {@link https://nodejs.org/api/process.html#processplatform | `process.platform`} value
*/
platform?: NodeJS.Platform;
/**
* Override the target rebuild architecture to something other than the host system architecture.
*
* @defaultValue The system {@link https://nodejs.org/api/process.html#processarch | `process.arch`} value
*/
arch?: string;
/**
* An array of module names to rebuild in addition to detected modules
* @default []
*/
extraModules?: string[];
/**
* An array of module names to rebuild. **Only** these modules will be rebuilt.
*/
onlyModules?: string[] | null;
/**
* Force a rebuild of modules regardless of their current build state.
*/
force?: boolean;
/**
* URL to download Electron header files from.
* @defaultValue `https://www.electronjs.org/headers`
*/
headerURL?: string;
/**
* Array of types of dependencies to rebuild. Possible values are `prod`, `dev`, and `optional`.
*
* @defaultValue `['prod', 'optional']`
*/
types?: ModuleType[];
/**
* Whether to rebuild modules sequentially or in parallel.
*
* @defaultValue `sequential`
*/
mode?: RebuildMode;
/**
* Rebuilds a Debug build of target modules. If this is `false`, a Release build will be generated instead.
*
* @defaultValue false
*/
debug?: boolean;
/**
* Enables hash-based caching to speed up local rebuilds.
*
* @experimental
* @defaultValue false
*/
useCache?: boolean;
/**
* Whether to use the `clang` executable that Electron uses when building.
* This will guarantee compiler compatibility.
*
* @defaultValue false
*/
useElectronClang?: boolean;
/**
* Sets a custom cache path for the {@link useCache} option.
* @experimental
* @defaultValue a `.electron-rebuild-cache` folder in the `os.homedir()` directory
*/
cachePath?: string;
/**
* GitHub tag prefix passed to {@link https://www.npmjs.com/package/prebuild-install | `prebuild-install`}.
* @defaultValue `v`
*/
prebuildTagPrefix?: string;
/**
* Path to the root of the project if using npm or yarn workspaces.
*/
projectRootPath?: string;
/**
* Override the Application Binary Interface (ABI) version for the version of Electron you are targeting.
* Only use when targeting nightly releases.
*
* @see the {@link https://github.com/electron/node-abi | electron/node-abi} repository for a list of Electron and Node.js ABIs
*/
forceABI?: number;
/**
* Disables the copying of `.node` files if not needed.
* @defaultValue false
*/
disablePreGypCopy?: boolean;
/**
* Skip prebuild download and rebuild module from source.
*
* @defaultValue false
*/
buildFromSource?: boolean;
/**
* Array of module names to ignore during the rebuild process.
*/
ignoreModules?: string[];
}
export interface RebuilderOptions extends RebuildOptions {
lifecycle: EventEmitter;
}
const d = debug('electron-rebuild');
const defaultMode: RebuildMode = 'sequential';
const defaultTypes: ModuleType[] = ['prod', 'optional'];
export class Rebuilder implements IRebuilder {
private ABIVersion: string | undefined;
private moduleWalker: ModuleWalker;
nodeGypPath: string;
rebuilds: (() => Promise<void>)[];
public lifecycle: EventEmitter;
public buildPath: string;
public electronVersion: string;
public platform: NodeJS.Platform;
public arch: string;
public force: boolean;
public headerURL: string;
public mode: RebuildMode;
public debug: boolean;
public useCache: boolean;
public cachePath: string;
public prebuildTagPrefix: string;
public msvsVersion?: string;
public useElectronClang: boolean;
public disablePreGypCopy: boolean;
public buildFromSource: boolean;
public ignoreModules: string[];
constructor(options: RebuilderOptions) {
this.lifecycle = options.lifecycle;
this.buildPath = options.buildPath;
this.electronVersion = options.electronVersion;
this.platform = options.platform || process.platform;
this.arch = options.arch || process.arch;
this.force = options.force || false;
this.headerURL = options.headerURL || 'https://www.electronjs.org/headers';
this.mode = options.mode || defaultMode;
this.debug = options.debug || false;
this.useCache = options.useCache || false;
this.useElectronClang = options.useElectronClang || false;
this.cachePath = options.cachePath || path.resolve(os.homedir(), '.electron-rebuild-cache');
this.prebuildTagPrefix = options.prebuildTagPrefix || 'v';
this.msvsVersion = process.env.GYP_MSVS_VERSION;
this.disablePreGypCopy = options.disablePreGypCopy || false;
this.buildFromSource = options.buildFromSource || false;
this.ignoreModules = options.ignoreModules || [];
d('ignoreModules', this.ignoreModules);
if (this.useCache && this.force) {
console.warn('[WARNING]: Electron Rebuild has force enabled and cache enabled, force take precedence and the cache will not be used.');
this.useCache = false;
}
if (typeof this.electronVersion === 'number') {
if (`${this.electronVersion}`.split('.').length === 1) {
this.electronVersion = `${this.electronVersion}.0.0`;
} else {
this.electronVersion = `${this.electronVersion}.0`;
}
}
if (typeof this.electronVersion !== 'string') {
throw new Error(`Expected a string version for electron version, got a "${typeof this.electronVersion}"`);
}
this.ABIVersion = options.forceABI?.toString();
const onlyModules = options.onlyModules || null;
const extraModules = new Set(options.extraModules);
const types = options.types || defaultTypes;
this.moduleWalker = new ModuleWalker(
this.buildPath,
options.projectRootPath,
types,
extraModules,
onlyModules,
);
this.rebuilds = [];
d(
'rebuilding with args:',
this.buildPath,
this.electronVersion,
this.platform,
this.arch,
extraModules,
this.force,
this.headerURL,
types,
this.debug
);
}
get ABI(): string {
if (this.ABIVersion === undefined) {
this.ABIVersion = nodeAbi.getAbi(this.electronVersion, 'electron');
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.ABIVersion!;
}
get buildType(): BuildType {
return this.debug ? BuildType.Debug : BuildType.Release;
}
async rebuild(): Promise<void> {
if (!path.isAbsolute(this.buildPath)) {
throw new Error('Expected buildPath to be an absolute path');
}
this.lifecycle.emit('start');
for (const modulePath of await this.modulesToRebuild()) {
this.rebuilds.push(() => this.rebuildModuleAt(modulePath));
}
this.rebuilds.push(() => this.rebuildModuleAt(this.buildPath));
if (this.mode !== 'sequential') {
await Promise.all(this.rebuilds.map(fn => fn()));
} else {
for (const rebuildFn of this.rebuilds) {
await rebuildFn();
}
}
}
async modulesToRebuild(): Promise<string[]> {
await this.moduleWalker.walkModules();
for (const nodeModulesPath of await this.moduleWalker.nodeModulesPaths) {
await this.moduleWalker.findAllModulesIn(nodeModulesPath);
}
return this.moduleWalker.modulesToRebuild;
}
async rebuildModuleAt(modulePath: string): Promise<void> {
if (!(await fs.pathExists(path.resolve(modulePath, 'binding.gyp')))) {
return;
}
const moduleRebuilder = new ModuleRebuilder(this, modulePath);
let moduleName = path.basename(modulePath);
const parentName = path.basename(path.dirname(modulePath));
if (parentName !== 'node_modules') {
moduleName = `${parentName}/${moduleName}`;
}
this.lifecycle.emit('module-found', moduleName);
if (!this.force && await moduleRebuilder.alreadyBuiltByRebuild()) {
d(`skipping: ${moduleName} as it is already built`);
this.lifecycle.emit('module-done', moduleName);
this.lifecycle.emit('module-skip', moduleName);
return;
}
d('checking', moduleName, 'against', this.ignoreModules);
if (this.ignoreModules.includes(moduleName)) {
d(`skipping: ${moduleName} as it is in the ignoreModules array`);
this.lifecycle.emit('module-done', moduleName);
this.lifecycle.emit('module-skip', moduleName);
return;
}
if (await moduleRebuilder.prebuildInstallNativeModuleExists()) {
d(`skipping: ${moduleName} as it was prebuilt`);
return;
}
let cacheKey!: string;
if (this.useCache) {
cacheKey = await generateCacheKey({
ABI: this.ABI,
arch: this.arch,
platform: this.platform,
debug: this.debug,
electronVersion: this.electronVersion,
headerURL: this.headerURL,
modulePath,
});
const applyDiffFn = await lookupModuleState(this.cachePath, cacheKey);
if (typeof applyDiffFn === 'function') {
await applyDiffFn(modulePath);
this.lifecycle.emit('module-done', moduleName);
return;
}
}
if (await moduleRebuilder.rebuild(cacheKey)) {
this.lifecycle.emit('module-done', moduleName);
}
}
}
export type RebuildResult = Promise<void> & { lifecycle: EventEmitter };
export function rebuild(options: RebuildOptions): RebuildResult {
// eslint-disable-next-line prefer-rest-params
d('rebuilding with args:', arguments);
const lifecycle = new EventEmitter();
const rebuilderOptions: RebuilderOptions = { ...options, lifecycle };
const rebuilder = new Rebuilder(rebuilderOptions);
const ret = rebuilder.rebuild() as RebuildResult;
ret.lifecycle = lifecycle;
return ret;
}