-
Notifications
You must be signed in to change notification settings - Fork 302
/
util.js
456 lines (414 loc) · 14.7 KB
/
util.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import console from 'node:console';
import fs from 'node:fs';
import https from 'node:https';
import path from 'node:path';
import process from 'node:process';
import * as GlobModule from 'glob';
import semver from 'semver';
/**
* Get manifest (array of NW release metadata) from URL.
* @param {string} manifestUrl Url to manifest
* @returns {Promise<object | undefined>} - Manifest object
*/
function getManifest(manifestUrl) {
let chunks = undefined;
return new Promise((resolve) => {
const req = https.get(manifestUrl, (response) => {
response.on('data', (chunk) => {
chunks += chunk;
});
response.on('error', (e) => {
console.error(e);
resolve(undefined);
});
response.on('end', () => {
resolve(chunks);
});
});
req.on('error', (e) => {
console.error(e);
resolve(undefined);
});
});
}
/**
* Get version specific release metadata.
* @param {string} version NW version
* @param {string} platform NW platform
* @param {string} arch NW architecture
* @param {string} cacheDir Directory to store NW binaries
* @param {string} manifestUrl Url to manifest
* @returns {object} Version specific release info
*/
async function getReleaseInfo(
version,
platform,
arch,
cacheDir,
manifestUrl,
) {
let releaseData = undefined;
let manifestPath = undefined;
if (platform === 'osx' && arch === 'arm64') {
manifestPath = path.resolve(cacheDir, 'manifest.mac.arm.json');
} else {
manifestPath = path.resolve(cacheDir, 'manifest.json');
}
try {
const data = await getManifest(manifestUrl);
if (data !== undefined) {
await fs.promises.writeFile(manifestPath, data.slice(9));
}
let manifest = JSON.parse(await fs.promises.readFile(manifestPath));
if (version === 'latest' || version === 'stable' || version === 'lts') {
// Remove leading "v" from version string
version = manifest[version].slice(1);
}
releaseData = manifest.versions.find(
(release) => release.version === `v${version}`,
);
} catch {
console.error(
'The version manifest does not exist/was not downloaded. Please try again in some time.',
);
}
return releaseData;
}
const PLATFORM_KV = {
darwin: 'osx',
linux: 'linux',
win32: 'win',
};
const ARCH_KV = {
x64: 'x64',
ia32: 'ia32',
arm64: 'arm64',
};
const EXE_NAME = {
win: 'nw.exe',
osx: 'nwjs.app/Contents/MacOS/nwjs',
linux: 'nw',
};
/**
* Glob files.
* @async
* @function
* @param {object} options - glob file options
* @param {string | string[]} options.srcDir - app src dir
* @param {boolean} options.glob - glob flag
* @returns {Promise<string[]>} - Returns array of file paths
*/
async function globFiles({
srcDir,
glob,
}) {
let files;
if (glob) {
files = [];
const patterns = srcDir.split(' ');
for (const pattern of patterns) {
let filePath = await GlobModule.glob(pattern);
files.push(...filePath);
}
} else {
files = srcDir;
}
return files;
}
/**
* Get Node manifest.
* @async
* @function
* @param {object} options - node manifest options
* @param {string | string []} options.srcDir - src dir
* @param {boolean} options.glob - glob flag
* @returns {Promise.<{path: string, json: object}>} - Node manifest
*/
async function getNodeManifest({
srcDir, glob
}) {
let manifest = {
path: '',
json: undefined,
};
let files;
if (glob) {
files = await globFiles({ srcDir, glob });
for (const file of files) {
if (path.basename(file) === 'package.json' && manifest === undefined) {
manifest.path = file;
manifest.json = JSON.parse(await fs.promises.readFile(file));
}
}
} else {
manifest.path = path.resolve(srcDir, 'package.json');
manifest.json = JSON.parse(await fs.promises.readFile(path.resolve(srcDir, 'package.json')));
}
if (manifest.json === undefined) {
throw new Error('package.json not found in srcDir file glob patterns.');
}
return manifest;
}
/**
* Function to convert `'true'` and `'false'` into `true` and `false`.
* `commander` does not do the conversion automatically.
* @param {any} option - a boolean type option
* @returns {any} Usually `undefined`, `true` or `false`. if not then it is validated later on.
*/
function str2Bool (option) {
if (typeof option === 'string') {
if (option === 'true') {
return true;
} else if (option === 'false') {
return false;
}
} else {
return option;
}
}
/**
* Parse options.
* @param {import("../../index.js").Options} options Options
* @param {object} pkg Package.json as JSON
* @returns {Promise<object>} Options
*/
export const parse = async (options, pkg) => {
options = options ?? {};
options.mode = options.mode ?? 'build';
options.version = options.version ?? 'latest';
options.flavor = options.flavor ?? 'normal';
options.platform = options.platform ?? PLATFORM_KV[process.platform];
options.arch = options.arch ?? ARCH_KV[process.arch];
options.downloadUrl = options.downloadUrl ?? 'https://dl.nwjs.io';
options.manifestUrl = options.manifestUrl ?? 'https://nwjs.io/versions';
options.cacheDir = options.cacheDir ?? './cache';
options.cache = str2Bool(options.cache ?? true);
options.ffmpeg = str2Bool(options.ffmpeg ?? false);
options.logLevel = options.logLevel ?? 'info';
if (options.mode === 'get') {
return { ...options };
}
options.argv = options.argv ?? [];
options.glob = str2Bool(options.glob) ?? true;
options.srcDir = options.srcDir ?? (options.glob ? './*' : '.');
if (options.mode === 'run') {
return { ...options };
}
options.outDir = path.resolve(options.outDir ?? './out');
options.zip = str2Bool(options.zip) ?? false;
options.managedManifest = str2Bool(options.managedManifest) ?? false;
options.nativeAddon = str2Bool(options.nativeAddon) ?? false;
options.app = options.app ?? {};
options.app.name = options.app.name ?? pkg.name;
/* Since the `parse` function is called twice, the first time `pkg` is `{}` and `options.app.name` is `undefined`. */
if (options.app.name) {
/* Remove special and control characters from app.name to mitigate potential path traversal. */
options.app.name = options.app.name.replace(/[<>:"/\\|?*\u0000-\u001F]/g, '');
}
options.app.icon = options.app.icon ?? undefined;
// TODO(#737): move this out
if (options.platform === 'linux') {
// linux desktop entry file configurations options
options.app.genericName = options.app.genericName ?? undefined;
options.app.noDisplay = options.app.noDisplay ?? undefined;
options.app.comment = options.app.comment ?? undefined;
options.app.hidden = options.app.hidden ?? undefined;
options.app.onlyShowIn = options.app.onlyShowIn ?? undefined;
options.app.notShowIn = options.app.notShowIn ?? undefined;
options.app.dBusActivatable = options.app.dBusActivatable ?? undefined;
options.app.tryExec = options.app.tryExec ?? undefined;
options.app.exec = options.app.name ?? undefined;
options.app.path = options.app.path ?? undefined;
options.app.terminal = options.app.terminal ?? undefined;
options.app.actions = options.app.actions ?? undefined;
options.app.mimeType = options.app.mimeType ?? undefined;
options.app.categories = options.app.categories ?? undefined;
options.app.implements = options.app.implements ?? undefined;
options.app.keywords = options.app.keywords ?? undefined;
options.app.startupNotify = options.app.startupNotify ?? undefined;
options.app.startupWMClass = options.app.startupWMClass ?? undefined;
options.app.prefersNonDefaultGPU =
options.app.prefersNonDefaultGPU ?? undefined;
options.app.singleMainWindow = options.app.singleMainWindow ?? undefined;
}
if (options.platform === 'win') {
// windows configuration options
options.app.version = options.app.version ?? pkg.version;
options.app.comments = options.app.comments ?? undefined;
options.app.company = options.app.company ?? pkg.author;
options.app.fileDescription =
options.app.fileDescription ?? pkg.description;
options.app.fileVersion = options.app.fileVersion ?? options.app.version ?? pkg.version;
options.app.internalName = options.app.internalName ?? pkg.name;
options.app.legalCopyright = options.app.legalCopyright ?? undefined;
options.app.legalTrademark = options.app.legalTrademark ?? undefined;
options.app.originalFilename = options.app.originalFilename ?? options.app.name;
options.app.privateBuild = options.app.privateBuild ?? undefined;
options.app.productName = options.app.productName ?? pkg.name;
options.app.productVersion = options.app.productVersion ?? pkg.version;
options.app.specialBuild = options.app.specialBuild ?? undefined;
options.app.languageCode = options.app.languageCode ?? 1033;
}
if (options.platform === 'osx') {
options.app.LSApplicationCategoryType =
options.app.LSApplicationCategoryType ?? undefined;
options.app.CFBundleIdentifier =
options.app.CFBundleIdentifier ?? options.app.name;
options.app.CFBundleName = options.app.CFBundleName ?? pkg.name;
options.app.CFBundleDisplayName =
options.app.CFBundleDisplayName ?? pkg.name;
options.app.CFBundleSpokenName = options.app.CFBundleSpokenName ?? pkg.name;
options.app.CFBundleShortVersionString =
options.app.CFBundleVersion ?? pkg.version;
options.app.CFBundleVersion =
options.app.CFBundleShortVersionString ?? pkg.version;
options.app.NSHumanReadableCopyright =
options.app.NSHumanReadableCopyright ?? undefined;
options.app.NSLocalNetworkUsageDescription =
options.app.NSLocalNetworkUsageDescription ?? undefined;
}
return { ...options };
};
/**
* Validate options.
* @param {import("../index.js").Options} options Options
* @param {object} releaseInfo Version specific NW release info
* @returns {Promise<undefined>} Return undefined if options are valid
* @throws {Error} Throw error if options are invalid
*/
export const validate = async (options, releaseInfo) => {
if (!['get', 'run', 'build'].includes(options.mode)) {
throw new Error(
`Unknown mode ${options.mode}. Expected "get", "run" or "build".`,
);
}
if (typeof releaseInfo === 'undefined') {
throw new Error(
'Either the specific version info does not exist or the version manifest itself does not exist. In case of the latter, please check your internet connection and try again later.',
);
}
if (!releaseInfo.flavors.includes(options.flavor)) {
throw new Error(
`${options.flavor} flavor is not supported by this download server.`,
);
}
if (
options.platform &&
options.arch &&
!releaseInfo.files.includes(`${options.platform}-${options.arch}`)
) {
throw new Error(
`Platform ${options.platform} and architecture ${options.arch} is not supported by this download server.`,
);
}
// if (typeof options.cacheDir !== "string") {
// throw new Error("Expected options.cacheDir to be a string. Got " + typeof options.cacheDir);
// }
if (typeof options.cache !== 'boolean') {
return new Error(
'Expected options.cache to be a boolean. Got ' + typeof options.cache,
);
}
if (typeof options.ffmpeg !== 'boolean') {
return new Error(
'Expected options.ffmpeg to be a boolean. Got ' + typeof options.ffmpeg,
);
}
if (
options.logLevel !== 'error' &&
options.logLevel !== 'warn' &&
options.logLevel !== 'info' &&
options.logLevel !== 'debug'
) {
throw new Error(
'Expected options.logLevel to be \'error\', \'warn\', \'info\' or \'debug\'. Got ' +
options.logLevel,
);
}
if (options.mode === 'get') {
return undefined;
}
if (Array.isArray(options.argv)) {
return new Error(
'Expected options.argv to be an array. Got ' + typeof options.argv,
);
}
if (typeof options.glob !== 'boolean') {
return new Error(
'Expected options.glob to be a boolean. Got ' + typeof options.glob,
);
}
if (options.srcDir) {
await fs.promises.readdir(options.srcDir);
}
if (options.mode === 'run') {
return undefined;
}
if (options.outDir) {
await fs.promises.readdir(options.outDir);
}
if (
typeof options.managedManifest !== 'boolean' &&
typeof options.managedManifest !== 'object' &&
typeof options.managedManifest !== 'string'
) {
return new Error(
'Expected options.managedManifest to be a boolean, object or string. Got ' +
typeof options.managedManifest,
);
}
if (typeof options.managedManifest === 'object') {
if (options.managedManifest.name === undefined) {
return new Error('Expected NW.js Manifest to have a `name` property.');
}
if (options.managedManifest.main === undefined) {
return new Error('Expected NW.js Manifest to have a `main` property.');
}
}
if (typeof options.nativeAddon !== 'boolean') {
if (typeof options.nativeAddon !== 'boolean' && typeof options.nativeAddon !== 'string') {
return new Error('Expected options.nativeAddon to be a boolean or string type. Got ' + typeof options.nativeAddon);
}
if (semver.parse(options.version).minor >= '83' && options.nativeAddon !== false) {
return new Error('Native addons are not supported for NW.js v0.82.0 and below');
}
}
// TODO: Validate app options
return undefined;
};
/**
* Get path to various NW specific file paths.
* @async
* @function
* @param {"chromedriver"} type - NW specific file or directory
* @param {object} options - nwbuild options
* @returns {string} - Path to chromedriver
* @throws {Error}
*/
async function getPath(type, options) {
if (type === 'chromedriver') {
return path.resolve(
options.cacheDir,
`nwjs${options.flavor === 'sdk' ? '-sdk' : ''}-v${options.version}-${options.platform
}-${options.arch}`,
`chromedriver${options.platform === 'win' ? '.exe' : ''}`,
);
} else {
throw new Error('Invalid type. Expected `chromedriver` but got ', type);
}
}
/**
* Check if file exists at specified path.
* @param {string} filePath - File path to check existence of
* @returns {Promise<boolean>} `true` if exists, otherwise `false`
*/
async function fileExists(filePath) {
let exists = true;
try {
await fs.promises.stat(filePath);
} catch {
exists = false;
}
return exists;
}
export default { fileExists, getReleaseInfo, getPath, PLATFORM_KV, ARCH_KV, EXE_NAME, globFiles, getNodeManifest, parse, validate };