UNPKG

2.95 kBJavaScriptView Raw
1import * as childProcess from 'child_process';
2import * as fs from 'fs-extra';
3import * as os from 'os';
4import * as path from 'path';
5async function useAndRemoveDirectory(directory, fn) {
6 let result;
7 try {
8 result = await fn(directory);
9 }
10 finally {
11 await fs.remove(directory);
12 }
13 return result;
14}
15export async function withTempDirectoryIn(parentDirectory = os.tmpdir(), fn) {
16 const tempDirectoryPrefix = 'electron-download-';
17 const tempDirectory = await fs.mkdtemp(path.resolve(parentDirectory, tempDirectoryPrefix));
18 return useAndRemoveDirectory(tempDirectory, fn);
19}
20export async function withTempDirectory(fn) {
21 return withTempDirectoryIn(undefined, fn);
22}
23export function normalizeVersion(version) {
24 if (!version.startsWith('v')) {
25 return `v${version}`;
26 }
27 return version;
28}
29/**
30 * Runs the `uname` command and returns the trimmed output.
31 */
32export function uname() {
33 return childProcess
34 .execSync('uname -m')
35 .toString()
36 .trim();
37}
38/**
39 * Generates an architecture name that would be used in an Electron or Node.js
40 * download file name.
41 */
42export function getNodeArch(arch) {
43 if (arch === 'arm') {
44 // eslint-disable-next-line @typescript-eslint/no-explicit-any
45 switch (process.config.variables.arm_version) {
46 case '6':
47 return uname();
48 case '7':
49 default:
50 return 'armv7l';
51 }
52 }
53 return arch;
54}
55/**
56 * Generates an architecture name that would be used in an Electron or Node.js
57 * download file name, from the `process` module information.
58 */
59export function getHostArch() {
60 return getNodeArch(process.arch);
61}
62export function ensureIsTruthyString(obj, key) {
63 if (!obj[key] || typeof obj[key] !== 'string') {
64 throw new Error(`Expected property "${key}" to be provided as a string but it was not`);
65 }
66}
67export function isOfficialLinuxIA32Download(platform, arch, version, mirrorOptions) {
68 return (platform === 'linux' &&
69 arch === 'ia32' &&
70 Number(version.slice(1).split('.')[0]) >= 4 &&
71 typeof mirrorOptions === 'undefined');
72}
73/**
74 * Find the value of a environment variable which may or may not have the
75 * prefix, in a case-insensitive manner.
76 */
77export function getEnv(prefix = '') {
78 const envsLowerCase = {};
79 for (const envKey in process.env) {
80 envsLowerCase[envKey.toLowerCase()] = process.env[envKey];
81 }
82 return (name) => {
83 return (envsLowerCase[`${prefix}${name}`.toLowerCase()] ||
84 envsLowerCase[name.toLowerCase()] ||
85 undefined);
86 };
87}
88export function setEnv(key, value) {
89 // The `void` operator always returns `undefined`.
90 // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
91 if (value !== void 0) {
92 process.env[key] = value;
93 }
94}
95//# sourceMappingURL=utils.js.map
\No newline at end of file