This commit is contained in:
@@ -4,6 +4,12 @@ const { spawnSync } = require('node:child_process');
|
|||||||
const fs = require('node:fs');
|
const fs = require('node:fs');
|
||||||
const path = require('node:path');
|
const path = require('node:path');
|
||||||
|
|
||||||
|
const ELECTRON_SKIP_ENV_KEYS = [
|
||||||
|
'ELECTRON_SKIP_BINARY_DOWNLOAD',
|
||||||
|
'npm_config_electron_skip_binary_download',
|
||||||
|
'NPM_CONFIG_ELECTRON_SKIP_BINARY_DOWNLOAD',
|
||||||
|
];
|
||||||
|
|
||||||
function resolveElectronPackageRoot() {
|
function resolveElectronPackageRoot() {
|
||||||
try {
|
try {
|
||||||
return path.dirname(require.resolve('electron/package.json'));
|
return path.dirname(require.resolve('electron/package.json'));
|
||||||
@@ -35,6 +41,17 @@ function platformBinaryCandidates(platform) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sanitizeElectronEnv(baseEnv = process.env) {
|
||||||
|
const env = { ...baseEnv };
|
||||||
|
|
||||||
|
delete env.ELECTRON_RUN_AS_NODE;
|
||||||
|
for (const key of ELECTRON_SKIP_ENV_KEYS) {
|
||||||
|
delete env[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
function electronBinaryCandidates({ packageRoot, distDir, platform }) {
|
function electronBinaryCandidates({ packageRoot, distDir, platform }) {
|
||||||
const candidatePaths = [];
|
const candidatePaths = [];
|
||||||
const pathHint = packageRoot ? readElectronPathHint(packageRoot) : null;
|
const pathHint = packageRoot ? readElectronPathHint(packageRoot) : null;
|
||||||
@@ -52,8 +69,6 @@ function electronBinaryCandidates({ packageRoot, distDir, platform }) {
|
|||||||
|
|
||||||
function runNpmRebuild({
|
function runNpmRebuild({
|
||||||
packageRoot,
|
packageRoot,
|
||||||
platform = process.platform,
|
|
||||||
arch = process.arch,
|
|
||||||
npmExecPath = process.env.npm_execpath || null,
|
npmExecPath = process.env.npm_execpath || null,
|
||||||
npmNodeExecPath = process.env.npm_node_execpath || process.execPath,
|
npmNodeExecPath = process.env.npm_node_execpath || process.execPath,
|
||||||
}) {
|
}) {
|
||||||
@@ -66,11 +81,7 @@ function runNpmRebuild({
|
|||||||
[npmExecPath, 'rebuild', 'electron', '--force', '--foreground-scripts'],
|
[npmExecPath, 'rebuild', 'electron', '--force', '--foreground-scripts'],
|
||||||
{
|
{
|
||||||
cwd: packageRoot,
|
cwd: packageRoot,
|
||||||
env: {
|
env: sanitizeElectronEnv(),
|
||||||
...process.env,
|
|
||||||
npm_config_platform: platform,
|
|
||||||
npm_config_arch: arch,
|
|
||||||
},
|
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -92,8 +103,6 @@ function runNpmRebuild({
|
|||||||
|
|
||||||
function repairElectronInstall({
|
function repairElectronInstall({
|
||||||
packageRoot,
|
packageRoot,
|
||||||
platform = process.platform,
|
|
||||||
arch = process.arch,
|
|
||||||
}) {
|
}) {
|
||||||
const installScript = path.join(packageRoot, 'install.js');
|
const installScript = path.join(packageRoot, 'install.js');
|
||||||
if (!fs.existsSync(installScript)) {
|
if (!fs.existsSync(installScript)) {
|
||||||
@@ -102,11 +111,7 @@ function repairElectronInstall({
|
|||||||
|
|
||||||
const result = spawnSync(process.execPath, [installScript], {
|
const result = spawnSync(process.execPath, [installScript], {
|
||||||
cwd: packageRoot,
|
cwd: packageRoot,
|
||||||
env: {
|
env: sanitizeElectronEnv(),
|
||||||
...process.env,
|
|
||||||
npm_config_platform: platform,
|
|
||||||
npm_config_arch: arch,
|
|
||||||
},
|
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -137,6 +142,7 @@ function buildMissingElectronError({ packageRoot, distDir, candidatePaths }) {
|
|||||||
'',
|
'',
|
||||||
' npm install',
|
' npm install',
|
||||||
' npm rebuild electron --force --foreground-scripts',
|
' npm rebuild electron --force --foreground-scripts',
|
||||||
|
' make sure ELECTRON_SKIP_BINARY_DOWNLOAD is not set',
|
||||||
'',
|
'',
|
||||||
'If that does not help, delete node_modules/electron and install again.',
|
'If that does not help, delete node_modules/electron and install again.',
|
||||||
'',
|
'',
|
||||||
@@ -149,7 +155,6 @@ function resolveElectronBinary({
|
|||||||
packageRoot = resolveElectronPackageRoot(),
|
packageRoot = resolveElectronPackageRoot(),
|
||||||
platform = process.platform,
|
platform = process.platform,
|
||||||
overrideDistPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || null,
|
overrideDistPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || null,
|
||||||
arch = process.arch,
|
|
||||||
} = {}) {
|
} = {}) {
|
||||||
if (!packageRoot && !overrideDistPath) {
|
if (!packageRoot && !overrideDistPath) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -164,7 +169,7 @@ function resolveElectronBinary({
|
|||||||
const resolved = candidatePaths.find((candidate) => fs.existsSync(candidate));
|
const resolved = candidatePaths.find((candidate) => fs.existsSync(candidate));
|
||||||
if (!resolved) {
|
if (!resolved) {
|
||||||
if (packageRoot) {
|
if (packageRoot) {
|
||||||
if (runNpmRebuild({ packageRoot, platform, arch })) {
|
if (runNpmRebuild({ packageRoot })) {
|
||||||
const rebuilt = electronBinaryCandidates({ packageRoot, distDir, platform }).find((candidate) =>
|
const rebuilt = electronBinaryCandidates({ packageRoot, distDir, platform }).find((candidate) =>
|
||||||
fs.existsSync(candidate)
|
fs.existsSync(candidate)
|
||||||
);
|
);
|
||||||
@@ -173,7 +178,7 @@ function resolveElectronBinary({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (repairElectronInstall({ packageRoot, platform, arch })) {
|
if (repairElectronInstall({ packageRoot })) {
|
||||||
const repaired = electronBinaryCandidates({ packageRoot, distDir, platform }).find((candidate) =>
|
const repaired = electronBinaryCandidates({ packageRoot, distDir, platform }).find((candidate) =>
|
||||||
fs.existsSync(candidate)
|
fs.existsSync(candidate)
|
||||||
);
|
);
|
||||||
@@ -195,6 +200,7 @@ module.exports = {
|
|||||||
readElectronPathHint,
|
readElectronPathHint,
|
||||||
repairElectronInstall,
|
repairElectronInstall,
|
||||||
runNpmRebuild,
|
runNpmRebuild,
|
||||||
|
sanitizeElectronEnv,
|
||||||
resolveElectronBinary,
|
resolveElectronBinary,
|
||||||
resolveElectronPackageRoot,
|
resolveElectronPackageRoot,
|
||||||
platformBinaryCandidates,
|
platformBinaryCandidates,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
const { spawn } = require('node:child_process');
|
const { spawn } = require('node:child_process');
|
||||||
|
|
||||||
const { resolveElectronBinary } = require('./electron-launcher');
|
const { resolveElectronBinary, sanitizeElectronEnv } = require('./electron-launcher');
|
||||||
|
|
||||||
let electronPath;
|
let electronPath;
|
||||||
try {
|
try {
|
||||||
@@ -12,8 +12,7 @@ try {
|
|||||||
console.error(error && error.message ? error.message : error);
|
console.error(error && error.message ? error.message : error);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
const env = { ...process.env };
|
const env = sanitizeElectronEnv();
|
||||||
delete env.ELECTRON_RUN_AS_NODE;
|
|
||||||
|
|
||||||
const child = spawn(electronPath, ['.'], {
|
const child = spawn(electronPath, ['.'], {
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
|
|||||||
@@ -49,14 +49,22 @@ test('repairs a broken Electron install before resolving the binary', (t) => {
|
|||||||
[
|
[
|
||||||
"const fs = require('node:fs');",
|
"const fs = require('node:fs');",
|
||||||
"const path = require('node:path');",
|
"const path = require('node:path');",
|
||||||
|
"if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) process.exit(2);",
|
||||||
"fs.mkdirSync(path.join(__dirname, 'dist'), { recursive: true });",
|
"fs.mkdirSync(path.join(__dirname, 'dist'), { recursive: true });",
|
||||||
"fs.writeFileSync(path.join(__dirname, 'dist', 'electron.exe'), 'binary');",
|
"fs.writeFileSync(path.join(__dirname, 'dist', 'electron.exe'), 'binary');",
|
||||||
"fs.writeFileSync(path.join(__dirname, 'path.txt'), 'electron.exe');",
|
"fs.writeFileSync(path.join(__dirname, 'path.txt'), 'electron.exe');",
|
||||||
].join('\n')
|
].join('\n')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const originalSkip = process.env.ELECTRON_SKIP_BINARY_DOWNLOAD;
|
||||||
|
process.env.ELECTRON_SKIP_BINARY_DOWNLOAD = '1';
|
||||||
|
t.after(() => {
|
||||||
|
if (originalSkip === undefined) delete process.env.ELECTRON_SKIP_BINARY_DOWNLOAD;
|
||||||
|
else process.env.ELECTRON_SKIP_BINARY_DOWNLOAD = originalSkip;
|
||||||
|
});
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
repairElectronInstall({ packageRoot: root, platform: 'win32' }),
|
repairElectronInstall({ packageRoot: root }),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
@@ -76,6 +84,7 @@ test('rebuilds Electron through npm when the binary is missing', (t) => {
|
|||||||
[
|
[
|
||||||
"const fs = require('node:fs');",
|
"const fs = require('node:fs');",
|
||||||
"const path = require('node:path');",
|
"const path = require('node:path');",
|
||||||
|
"if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) process.exit(2);",
|
||||||
"fs.mkdirSync(path.join(__dirname, 'dist'), { recursive: true });",
|
"fs.mkdirSync(path.join(__dirname, 'dist'), { recursive: true });",
|
||||||
"fs.writeFileSync(path.join(__dirname, 'dist', 'electron.exe'), 'binary');",
|
"fs.writeFileSync(path.join(__dirname, 'dist', 'electron.exe'), 'binary');",
|
||||||
"fs.writeFileSync(path.join(__dirname, 'path.txt'), 'electron.exe');",
|
"fs.writeFileSync(path.join(__dirname, 'path.txt'), 'electron.exe');",
|
||||||
@@ -84,13 +93,17 @@ test('rebuilds Electron through npm when the binary is missing', (t) => {
|
|||||||
|
|
||||||
const originalNpmExecPath = process.env.npm_execpath;
|
const originalNpmExecPath = process.env.npm_execpath;
|
||||||
const originalNpmNodeExecPath = process.env.npm_node_execpath;
|
const originalNpmNodeExecPath = process.env.npm_node_execpath;
|
||||||
|
const originalSkip = process.env.ELECTRON_SKIP_BINARY_DOWNLOAD;
|
||||||
process.env.npm_execpath = fakeNpmCli;
|
process.env.npm_execpath = fakeNpmCli;
|
||||||
process.env.npm_node_execpath = process.execPath;
|
process.env.npm_node_execpath = process.execPath;
|
||||||
|
process.env.ELECTRON_SKIP_BINARY_DOWNLOAD = '1';
|
||||||
t.after(() => {
|
t.after(() => {
|
||||||
if (originalNpmExecPath === undefined) delete process.env.npm_execpath;
|
if (originalNpmExecPath === undefined) delete process.env.npm_execpath;
|
||||||
else process.env.npm_execpath = originalNpmExecPath;
|
else process.env.npm_execpath = originalNpmExecPath;
|
||||||
if (originalNpmNodeExecPath === undefined) delete process.env.npm_node_execpath;
|
if (originalNpmNodeExecPath === undefined) delete process.env.npm_node_execpath;
|
||||||
else process.env.npm_node_execpath = originalNpmNodeExecPath;
|
else process.env.npm_node_execpath = originalNpmNodeExecPath;
|
||||||
|
if (originalSkip === undefined) delete process.env.ELECTRON_SKIP_BINARY_DOWNLOAD;
|
||||||
|
else process.env.ELECTRON_SKIP_BINARY_DOWNLOAD = originalSkip;
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
|
|||||||
Reference in New Issue
Block a user