-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpackager.ts
324 lines (287 loc) · 9.01 KB
/
packager.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
import {
baseTempDir,
debug,
generateFinalPath,
hostInfo,
info,
isPlatformMac,
} from './common';
import { populateIgnoredPaths } from './copy-filter';
import { createDownloadCombos, downloadElectronZip } from './download';
import fs from 'fs-extra';
import { getMetadataFromPackageJSON } from './infer';
import { promisifyHooks } from './hooks';
import path from 'path';
import {
createPlatformArchPairs,
osModules,
validateListFromOptions,
} from './targets';
import { extractElectronZip } from './unzip';
import { packageUniversalMac } from './universal';
import {
ComboOptions,
DownloadOptions,
OfficialPlatform,
Options,
SupportedArch,
SupportedPlatform,
} from './types';
import { App } from './platform';
function debugHostInfo() {
debug(hostInfo());
}
export class Packager {
canCreateSymlinks: boolean | undefined = undefined;
opts: Options;
tempBase: string;
useTempDir: boolean;
constructor(opts: Options) {
this.opts = opts;
this.tempBase = baseTempDir(opts);
this.useTempDir = opts.tmpdir !== false;
}
async ensureTempDir() {
if (this.useTempDir) {
await fs.remove(this.tempBase);
} else {
return Promise.resolve();
}
}
async testSymlink(comboOpts: ComboOptions, zipPath: string) {
await fs.mkdirp(this.tempBase);
const testPath = await fs.mkdtemp(
path.join(
this.tempBase,
`symlink-test-${comboOpts.platform}-${comboOpts.arch}-`,
),
);
const testFile = path.join(testPath, 'test');
const testLink = path.join(testPath, 'testlink');
try {
await fs.outputFile(testFile, '');
await fs.symlink(testFile, testLink);
this.canCreateSymlinks = true;
} catch (e) {
/* istanbul ignore next */
this.canCreateSymlinks = false;
} finally {
await fs.remove(testPath);
}
if (this.canCreateSymlinks) {
return this.checkOverwrite(comboOpts, zipPath);
}
/* istanbul ignore next */
return this.skipHostPlatformSansSymlinkSupport(comboOpts);
}
/* istanbul ignore next */
skipHostPlatformSansSymlinkSupport(comboOpts: ComboOptions) {
info(
`Cannot create symlinks (on Windows hosts, it requires admin privileges); skipping ${comboOpts.platform} platform`,
this.opts.quiet,
);
return Promise.resolve();
}
async overwriteAndCreateApp(
outDir: string,
comboOpts: ComboOptions,
zipPath: string,
) {
debug(`Removing ${outDir} due to setting overwrite: true`);
await fs.remove(outDir);
return this.createApp(comboOpts, zipPath);
}
async extractElectronZip(
comboOpts: ComboOptions,
zipPath: string,
buildDir: string,
) {
debug(`Extracting ${zipPath} to ${buildDir}`);
await extractElectronZip(zipPath, buildDir);
await promisifyHooks(this.opts.afterExtract, [
buildDir,
comboOpts.electronVersion,
comboOpts.platform,
comboOpts.arch,
]);
}
async buildDir(
platform: ComboOptions['platform'],
arch: ComboOptions['arch'],
) {
let buildParentDir;
if (this.useTempDir) {
buildParentDir = this.tempBase;
} else {
buildParentDir = this.opts.out || process.cwd();
}
await fs.mkdirp(buildParentDir);
return await fs.mkdtemp(
path.resolve(buildParentDir, `${platform}-${arch}-template-`),
);
}
async createApp(comboOpts: ComboOptions, zipPath: string) {
const buildDir = await this.buildDir(comboOpts.platform, comboOpts.arch);
info(
`Packaging app for platform ${comboOpts.platform} ${comboOpts.arch} using electron v${comboOpts.electronVersion}`,
this.opts.quiet,
);
debug(`Creating ${buildDir}`);
await fs.ensureDir(buildDir);
await this.extractElectronZip(comboOpts, zipPath, buildDir);
const os = await import(osModules[comboOpts.platform as OfficialPlatform]);
const app = new os.App(comboOpts, buildDir) as App;
return app.create();
}
async checkOverwrite(comboOpts: ComboOptions, zipPath: string) {
const finalPath = generateFinalPath(comboOpts);
if (await fs.pathExists(finalPath)) {
if (this.opts.overwrite) {
return this.overwriteAndCreateApp(finalPath, comboOpts, zipPath);
} else {
info(
`Skipping ${comboOpts.platform} ${comboOpts.arch} (output dir already exists, use --overwrite to force)`,
this.opts.quiet,
);
return true;
}
} else {
return this.createApp(comboOpts, zipPath);
}
}
async getElectronZipPath(downloadOpts: DownloadOptions) {
if (this.opts.electronZipDir) {
if (await fs.pathExists(this.opts.electronZipDir)) {
const zipPath = path.resolve(
this.opts.electronZipDir,
`electron-v${downloadOpts.version}-${downloadOpts.platform}-${downloadOpts.arch}.zip`,
);
if (!(await fs.pathExists(zipPath))) {
throw new Error(
`The specified Electron ZIP file does not exist: ${zipPath}`,
);
}
return zipPath;
}
throw new Error(
`The specified Electron ZIP directory does not exist: ${this.opts.electronZipDir}`,
);
} else {
return downloadElectronZip(downloadOpts);
}
}
async packageForPlatformAndArchWithOpts(
comboOpts: ComboOptions,
downloadOpts: DownloadOptions,
) {
const zipPath = await this.getElectronZipPath(downloadOpts);
if (!this.useTempDir) {
return this.createApp(comboOpts, zipPath);
}
if (isPlatformMac(comboOpts.platform)) {
/* istanbul ignore else */
if (this.canCreateSymlinks === undefined) {
return this.testSymlink(comboOpts, zipPath);
} else if (!this.canCreateSymlinks) {
return this.skipHostPlatformSansSymlinkSupport(comboOpts);
}
}
return this.checkOverwrite(comboOpts, zipPath);
}
async packageForPlatformAndArch(downloadOpts: DownloadOptions) {
// Create delegated options object with specific platform and arch, for output directory naming
const comboOpts: ComboOptions = {
...this.opts,
arch: downloadOpts.arch,
platform: downloadOpts.platform,
electronVersion: downloadOpts.version,
};
if (isPlatformMac(comboOpts.platform) && comboOpts.arch === 'universal') {
return packageUniversalMac(
this.packageForPlatformAndArchWithOpts.bind(this),
await this.buildDir(comboOpts.platform, comboOpts.arch),
comboOpts,
downloadOpts,
this.tempBase,
);
}
return this.packageForPlatformAndArchWithOpts(comboOpts, downloadOpts);
}
}
async function packageAllSpecifiedCombos(
opts: Options,
archs: SupportedArch[],
platforms: SupportedPlatform[],
) {
const packager = new Packager(opts);
await packager.ensureTempDir();
return Promise.all(
createDownloadCombos(opts, platforms, archs).map((downloadOpts) =>
packager.packageForPlatformAndArch(downloadOpts),
),
);
}
/**
* Bundles Electron-based application source code with a renamed/customized Electron executable and
* its supporting files into folders ready for distribution.
*
* Briefly, this function:
* - finds or downloads the correct release of Electron
* - uses that version of Electron to create a app in `<out>/<appname>-<platform>-<arch>`
*
* @example
*
* ```javascript
* const packager = require('@electron/packager')
*
* async function bundleElectronApp(options) {
* const appPaths = await packager(options)
* console.log(`Electron app bundles created:\n${appPaths.join("\n")}`)
* }
* ```
*
* @param opts - Options to configure packaging.
*
* @returns A Promise containing the paths to the newly created application bundles.
*/
export async function packager(opts: Options): Promise<string[]> {
debugHostInfo();
if (debug.enabled) {
debug(`Packager Options: ${JSON.stringify(opts)}`);
}
const archs = validateListFromOptions(opts, 'arch') as
| SupportedArch[]
| Error;
const platforms = validateListFromOptions(opts, 'platform') as
| SupportedPlatform[]
| Error;
if (!Array.isArray(archs)) {
return Promise.reject(archs);
}
if (!Array.isArray(platforms)) {
return Promise.reject(platforms);
}
debug(`Target Platforms: ${platforms.join(', ')}`);
debug(`Target Architectures: ${archs.join(', ')}`);
const packageJSONDir = path.resolve(process.cwd(), opts.dir) || process.cwd();
await getMetadataFromPackageJSON(platforms, opts, packageJSONDir);
if (opts.name?.endsWith(' Helper')) {
throw new Error(
'Application names cannot end in " Helper" due to limitations on macOS',
);
}
debug(`Application name: ${opts.name}`);
debug(`Target Electron version: ${opts.electronVersion}`);
populateIgnoredPaths(opts);
await promisifyHooks(opts.afterFinalizePackageTargets, [
createPlatformArchPairs(opts, platforms, archs).map(([platform, arch]) => ({
platform,
arch,
})),
]);
const appPaths = await packageAllSpecifiedCombos(opts, archs, platforms);
// Remove falsy entries (e.g. skipped platforms)
return appPaths.filter(
(appPath) => appPath && typeof appPath === 'string',
) as string[];
}