-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathplatform.ts
365 lines (311 loc) · 9.47 KB
/
platform.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
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
import fs from 'fs-extra';
import path from 'path';
import asar, { FileRecord } from '@electron/asar';
import {
baseTempDir,
createAsarOpts,
debug,
ensureArray,
generateFinalPath,
validateElectronApp,
warning,
} from './common';
import { userPathFilter } from './copy-filter';
import { promisifyHooks } from './hooks';
import crypto from 'crypto';
import { ComboOptions } from './types';
export class App {
asarIntegrity:
| Record<string, Pick<FileRecord['integrity'], 'algorithm' | 'hash'>>
| undefined = undefined;
asarOptions: ReturnType<typeof createAsarOpts>;
cachedStagingPath: string | undefined = undefined;
opts: ComboOptions;
templatePath: string;
constructor(opts: ComboOptions, templatePath: string) {
this.opts = opts;
this.templatePath = templatePath;
this.asarOptions = createAsarOpts(opts);
if (this.opts.prune === undefined) {
this.opts.prune = true;
}
}
async create(): Promise<string> {
/* istanbul ignore next */
throw new Error('Child classes must implement this');
}
/**
* Resource directory path before renaming.
*/
get originalResourcesDir(): string {
return this.resourcesDir;
}
/**
* Resource directory path after renaming.
*/
get resourcesDir(): string {
return path.join(this.stagingPath, 'resources');
}
get originalResourcesAppDir(): string {
return path.join(this.originalResourcesDir, 'app');
}
get electronBinaryDir(): string {
return this.stagingPath;
}
get originalElectronName(): string {
/* istanbul ignore next */
throw new Error('Child classes must implement this');
}
get newElectronName(): string {
/* istanbul ignore next */
throw new Error('Child classes must implement this');
}
get executableName() {
return this.opts.executableName || this.opts.name;
}
get stagingPath() {
if (this.opts.tmpdir === false) {
return generateFinalPath(this.opts);
} else {
if (!this.cachedStagingPath) {
const tempDir = baseTempDir(this.opts);
fs.mkdirpSync(tempDir);
this.cachedStagingPath = fs.mkdtempSync(path.resolve(tempDir, 'tmp-'));
}
return this.cachedStagingPath;
}
}
get appAsarPath() {
return path.join(this.originalResourcesDir, 'app.asar');
}
get commonHookArgs() {
return [this.opts.electronVersion, this.opts.platform, this.opts.arch];
}
get hookArgsWithOriginalResourcesAppDir() {
return [this.originalResourcesAppDir, ...this.commonHookArgs];
}
async relativeRename(basePath: string, oldName: string, newName: string) {
debug(`Renaming ${oldName} to ${newName} in ${basePath}`);
await fs.rename(path.join(basePath, oldName), path.join(basePath, newName));
}
async renameElectron() {
return this.relativeRename(
this.electronBinaryDir,
this.originalElectronName,
this.newElectronName,
);
}
/**
* Performs the following initial operations for an app:
* * Creates temporary directory
* * Remove default_app (which is either a folder or an asar file)
* * If a prebuilt asar is specified:
* * Copies asar into temporary directory as app.asar
* * Otherwise:
* * Copies template into temporary directory
* * Copies user's app into temporary directory
* * Prunes non-production node_modules (if opts.prune is either truthy or undefined)
* * Creates an asar (if opts.asar is set)
*
* Prune and asar are performed before platform-specific logic, primarily so that
* this.originalResourcesAppDir is predictable (e.g. before .app is renamed for Mac)
*/
async initialize() {
debug(
`Initializing app in ${this.stagingPath} from ${this.templatePath} template`,
);
await fs.move(this.templatePath, this.stagingPath, { overwrite: true });
await this.removeDefaultApp();
if (this.opts.prebuiltAsar) {
await this.copyPrebuiltAsar();
this.asarIntegrity = {
[this.appRelativePlatformPath(this.appAsarPath)]: this.getAsarIntegrity(
this.appAsarPath,
),
};
} else {
await this.buildApp();
}
await promisifyHooks(
this.opts.afterInitialize,
this.hookArgsWithOriginalResourcesAppDir,
);
}
async buildApp() {
await this.copyTemplate();
await validateElectronApp(this.opts.dir, this.originalResourcesAppDir);
await this.asarApp();
}
async copyTemplate() {
await promisifyHooks(
this.opts.beforeCopy,
this.hookArgsWithOriginalResourcesAppDir,
);
await fs.copy(this.opts.dir, this.originalResourcesAppDir, {
filter: userPathFilter(this.opts),
dereference: this.opts.derefSymlinks,
});
await promisifyHooks(
this.opts.afterCopy,
this.hookArgsWithOriginalResourcesAppDir,
);
if (this.opts.prune) {
await promisifyHooks(
this.opts.afterPrune,
this.hookArgsWithOriginalResourcesAppDir,
);
}
}
async removeDefaultApp() {
await Promise.all(
['default_app', 'default_app.asar'].map(async (basename) =>
fs.remove(path.join(this.originalResourcesDir, basename)),
),
);
}
/**
* Forces an icon filename to a given extension and returns the normalized filename,
* if it exists. Otherwise, returns null.
*
* This error path is used by win32 if no icon is specified.
*/
async normalizeIconExtension(targetExt: string) {
if (!this.opts.icon) {
throw new Error('No filename specified to normalizeIconExtension');
}
let iconFilename = this.opts.icon;
const ext = path.extname(iconFilename);
if (ext !== targetExt) {
iconFilename = path.join(
path.dirname(iconFilename),
path.basename(iconFilename, ext) + targetExt,
);
}
if (await fs.pathExists(iconFilename)) {
return iconFilename;
} else {
/* istanbul ignore next */
warning(
`Could not find icon "${iconFilename}", not updating app icon`,
this.opts.quiet,
);
}
}
prebuiltAsarWarning(option: keyof ComboOptions, triggerWarning: unknown) {
if (triggerWarning) {
warning(
`prebuiltAsar and ${option} are incompatible, ignoring the ${option} option`,
this.opts.quiet,
);
}
}
async copyPrebuiltAsar() {
if (this.asarOptions) {
warning(
'prebuiltAsar has been specified, all asar options will be ignored',
this.opts.quiet,
);
}
for (const hookName of ['beforeCopy', 'afterCopy', 'afterPrune'] as const) {
if (this.opts[hookName]) {
throw new Error(`${hookName} is incompatible with prebuiltAsar`);
}
}
this.prebuiltAsarWarning(
'ignore',
(
this.opts as ComboOptions & {
originalIgnore: ComboOptions['ignore'];
}
).originalIgnore,
);
this.prebuiltAsarWarning('prune', !this.opts.prune);
this.prebuiltAsarWarning(
'derefSymlinks',
this.opts.derefSymlinks !== undefined,
);
const src = path.resolve(this.opts.prebuiltAsar!);
const stat = await fs.stat(src);
if (!stat.isFile()) {
throw new Error(`${src} specified in prebuiltAsar must be an asar file.`);
}
debug(`Copying asar: ${src} to ${this.appAsarPath}`);
await fs.copy(src, this.appAsarPath, {
overwrite: false,
errorOnExist: true,
});
}
appRelativePlatformPath(p: string) {
if (this.opts.platform === 'win32') {
return path.win32.relative(this.stagingPath, p);
}
return path.posix.relative(this.stagingPath, p);
}
async asarApp() {
if (!this.asarOptions) {
return Promise.resolve();
}
debug(`Running asar with the options ${JSON.stringify(this.asarOptions)}`);
await promisifyHooks(
this.opts.beforeAsar,
this.hookArgsWithOriginalResourcesAppDir,
);
await asar.createPackageWithOptions(
this.originalResourcesAppDir,
this.appAsarPath,
this.asarOptions,
);
this.asarIntegrity = {
[this.appRelativePlatformPath(this.appAsarPath)]: this.getAsarIntegrity(
this.appAsarPath,
),
};
await fs.remove(this.originalResourcesAppDir);
await promisifyHooks(
this.opts.afterAsar,
this.hookArgsWithOriginalResourcesAppDir,
);
}
getAsarIntegrity(
path: string,
): Pick<FileRecord['integrity'], 'algorithm' | 'hash'> {
const { headerString } = asar.getRawHeader(path);
return {
algorithm: 'SHA256',
hash: crypto.createHash('SHA256').update(headerString).digest('hex'),
};
}
async copyExtraResources() {
if (!this.opts.extraResource) {
return Promise.resolve();
}
const extraResources = ensureArray(this.opts.extraResource);
const hookArgs = [this.stagingPath, ...this.commonHookArgs];
await promisifyHooks(this.opts.beforeCopyExtraResources, hookArgs);
await Promise.all(
extraResources.map((resource) =>
fs.copy(
resource,
path.resolve(
this.stagingPath,
this.resourcesDir,
path.basename(resource),
),
),
),
);
await promisifyHooks(this.opts.afterCopyExtraResources, hookArgs);
}
async move() {
const finalPath = generateFinalPath(this.opts);
if (this.opts.tmpdir !== false) {
debug(`Moving ${this.stagingPath} to ${finalPath}`);
await fs.move(this.stagingPath, finalPath);
}
if (this.opts.afterComplete) {
const hookArgs = [finalPath, ...this.commonHookArgs];
await promisifyHooks(this.opts.afterComplete, hookArgs);
}
return finalPath;
}
}