UNPKG

5.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.doesCallerOwnTemporaryOutput = exports.shouldWriteCache = exports.shouldTryReadCache = exports.effectiveCacheMode = exports.setEnv = exports.getEnv = exports.isOfficialLinuxIA32Download = exports.ensureIsTruthyString = exports.getHostArch = exports.getNodeArch = exports.uname = exports.normalizeVersion = exports.withTempDirectory = exports.withTempDirectoryIn = exports.TempDirCleanUpMode = exports.mkdtemp = void 0;
4const childProcess = require("child_process");
5const fs = require("fs-extra");
6const os = require("os");
7const path = require("path");
8const types_1 = require("./types");
9async function useAndRemoveDirectory(directory, fn) {
10 let result;
11 try {
12 result = await fn(directory);
13 }
14 finally {
15 await fs.remove(directory);
16 }
17 return result;
18}
19async function mkdtemp(parentDirectory = os.tmpdir()) {
20 const tempDirectoryPrefix = 'electron-download-';
21 return await fs.mkdtemp(path.resolve(parentDirectory, tempDirectoryPrefix));
22}
23exports.mkdtemp = mkdtemp;
24var TempDirCleanUpMode;
25(function (TempDirCleanUpMode) {
26 TempDirCleanUpMode[TempDirCleanUpMode["CLEAN"] = 0] = "CLEAN";
27 TempDirCleanUpMode[TempDirCleanUpMode["ORPHAN"] = 1] = "ORPHAN";
28})(TempDirCleanUpMode = exports.TempDirCleanUpMode || (exports.TempDirCleanUpMode = {}));
29async function withTempDirectoryIn(parentDirectory = os.tmpdir(), fn, cleanUp) {
30 const tempDirectory = await mkdtemp(parentDirectory);
31 if (cleanUp === TempDirCleanUpMode.CLEAN) {
32 return useAndRemoveDirectory(tempDirectory, fn);
33 }
34 else {
35 return fn(tempDirectory);
36 }
37}
38exports.withTempDirectoryIn = withTempDirectoryIn;
39async function withTempDirectory(fn, cleanUp) {
40 return withTempDirectoryIn(undefined, fn, cleanUp);
41}
42exports.withTempDirectory = withTempDirectory;
43function normalizeVersion(version) {
44 if (!version.startsWith('v')) {
45 return `v${version}`;
46 }
47 return version;
48}
49exports.normalizeVersion = normalizeVersion;
50/**
51 * Runs the `uname` command and returns the trimmed output.
52 */
53function uname() {
54 return childProcess
55 .execSync('uname -m')
56 .toString()
57 .trim();
58}
59exports.uname = uname;
60/**
61 * Generates an architecture name that would be used in an Electron or Node.js
62 * download file name.
63 */
64function getNodeArch(arch) {
65 if (arch === 'arm') {
66 // eslint-disable-next-line @typescript-eslint/no-explicit-any
67 switch (process.config.variables.arm_version) {
68 case '6':
69 return uname();
70 case '7':
71 default:
72 return 'armv7l';
73 }
74 }
75 return arch;
76}
77exports.getNodeArch = getNodeArch;
78/**
79 * Generates an architecture name that would be used in an Electron or Node.js
80 * download file name from the `process` module information.
81 *
82 * @category Utility
83 */
84function getHostArch() {
85 return getNodeArch(process.arch);
86}
87exports.getHostArch = getHostArch;
88function ensureIsTruthyString(obj, key) {
89 if (!obj[key] || typeof obj[key] !== 'string') {
90 throw new Error(`Expected property "${String(key)}" to be provided as a string but it was not`);
91 }
92}
93exports.ensureIsTruthyString = ensureIsTruthyString;
94function isOfficialLinuxIA32Download(platform, arch, version, mirrorOptions) {
95 return (platform === 'linux' &&
96 arch === 'ia32' &&
97 Number(version.slice(1).split('.')[0]) >= 4 &&
98 typeof mirrorOptions === 'undefined');
99}
100exports.isOfficialLinuxIA32Download = isOfficialLinuxIA32Download;
101/**
102 * Find the value of a environment variable which may or may not have the
103 * prefix, in a case-insensitive manner.
104 */
105function getEnv(prefix = '') {
106 const envsLowerCase = {};
107 for (const envKey in process.env) {
108 envsLowerCase[envKey.toLowerCase()] = process.env[envKey];
109 }
110 return (name) => {
111 return (envsLowerCase[`${prefix}${name}`.toLowerCase()] ||
112 envsLowerCase[name.toLowerCase()] ||
113 undefined);
114 };
115}
116exports.getEnv = getEnv;
117function setEnv(key, value) {
118 // The `void` operator always returns `undefined`.
119 // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
120 if (value !== void 0) {
121 process.env[key] = value;
122 }
123}
124exports.setEnv = setEnv;
125function effectiveCacheMode(artifactDetails) {
126 if (artifactDetails.force) {
127 if (artifactDetails.cacheMode) {
128 throw new Error('Setting both "force" and "cacheMode" is not supported, please exclusively use "cacheMode"');
129 }
130 return types_1.ElectronDownloadCacheMode.WriteOnly;
131 }
132 return artifactDetails.cacheMode || types_1.ElectronDownloadCacheMode.ReadWrite;
133}
134exports.effectiveCacheMode = effectiveCacheMode;
135function shouldTryReadCache(cacheMode) {
136 return (cacheMode === types_1.ElectronDownloadCacheMode.ReadOnly ||
137 cacheMode === types_1.ElectronDownloadCacheMode.ReadWrite);
138}
139exports.shouldTryReadCache = shouldTryReadCache;
140function shouldWriteCache(cacheMode) {
141 return (cacheMode === types_1.ElectronDownloadCacheMode.WriteOnly ||
142 cacheMode === types_1.ElectronDownloadCacheMode.ReadWrite);
143}
144exports.shouldWriteCache = shouldWriteCache;
145function doesCallerOwnTemporaryOutput(cacheMode) {
146 return (cacheMode === types_1.ElectronDownloadCacheMode.Bypass ||
147 cacheMode === types_1.ElectronDownloadCacheMode.ReadOnly);
148}
149exports.doesCallerOwnTemporaryOutput = doesCallerOwnTemporaryOutput;
150//# sourceMappingURL=utils.js.map
\No newline at end of file