Skip to content

Commit 0519107

Browse files
committed
refactor(plugins): share bundled runtime deps install script helpers
1 parent f4af077 commit 0519107

3 files changed

Lines changed: 98 additions & 67 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { spawnSync } from "node:child_process";
2+
3+
export function createNestedNpmInstallEnv(env = process.env) {
4+
const nextEnv = { ...env };
5+
delete nextEnv.npm_config_global;
6+
delete nextEnv.npm_config_location;
7+
delete nextEnv.npm_config_prefix;
8+
return nextEnv;
9+
}
10+
11+
export function createBundledRuntimeDependencyInstallEnv(env = process.env, options = {}) {
12+
const nextEnv = {
13+
...createNestedNpmInstallEnv(env),
14+
npm_config_dry_run: "false",
15+
npm_config_fetch_retries: env.npm_config_fetch_retries ?? "5",
16+
npm_config_fetch_retry_maxtimeout: env.npm_config_fetch_retry_maxtimeout ?? "120000",
17+
npm_config_fetch_retry_mintimeout: env.npm_config_fetch_retry_mintimeout ?? "10000",
18+
npm_config_fetch_timeout: env.npm_config_fetch_timeout ?? "300000",
19+
npm_config_legacy_peer_deps: "true",
20+
npm_config_package_lock: "false",
21+
npm_config_save: "false",
22+
};
23+
if (options.ci) {
24+
nextEnv.CI = "1";
25+
}
26+
if (options.quiet) {
27+
Object.assign(nextEnv, {
28+
npm_config_audit: "false",
29+
npm_config_fund: "false",
30+
npm_config_loglevel: "error",
31+
npm_config_progress: "false",
32+
npm_config_yes: "true",
33+
});
34+
}
35+
return nextEnv;
36+
}
37+
38+
export function createBundledRuntimeDependencyInstallArgs(specs = [], options = {}) {
39+
return [
40+
"install",
41+
...(options.noAudit ? ["--no-audit"] : []),
42+
...(options.noFund ? ["--no-fund"] : []),
43+
"--ignore-scripts",
44+
...(options.silent ? ["--silent"] : []),
45+
...specs,
46+
];
47+
}
48+
49+
export function runBundledRuntimeDependencyNpmInstall(params) {
50+
const runSpawnSync = params.spawnSyncImpl ?? spawnSync;
51+
const result = runSpawnSync(params.npmRunner.command, params.npmRunner.args, {
52+
cwd: params.cwd,
53+
encoding: "utf8",
54+
env: params.env ?? params.npmRunner.env ?? process.env,
55+
shell: params.npmRunner.shell,
56+
stdio: params.stdio ?? "pipe",
57+
...(params.timeoutMs ? { timeout: params.timeoutMs } : {}),
58+
windowsHide: true,
59+
windowsVerbatimArguments: params.npmRunner.windowsVerbatimArguments,
60+
});
61+
if (result.status === 0) {
62+
return;
63+
}
64+
const output = [result.stderr, result.stdout].filter(Boolean).join("\n").trim();
65+
throw new Error(output || "npm install failed");
66+
}

scripts/postinstall-bundled-plugins.mjs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,20 @@ import {
2424
import { tmpdir } from "node:os";
2525
import { basename, dirname, isAbsolute, join, posix, relative } from "node:path";
2626
import { fileURLToPath, pathToFileURL } from "node:url";
27+
import {
28+
createBundledRuntimeDependencyInstallArgs,
29+
createBundledRuntimeDependencyInstallEnv,
30+
createNestedNpmInstallEnv,
31+
runBundledRuntimeDependencyNpmInstall,
32+
} from "./lib/bundled-runtime-deps-install.mjs";
2733
import { resolveNpmRunner } from "./npm-runner.mjs";
2834

35+
export {
36+
createBundledRuntimeDependencyInstallArgs,
37+
createBundledRuntimeDependencyInstallEnv,
38+
createNestedNpmInstallEnv,
39+
};
40+
2941
export const BUNDLED_PLUGIN_INSTALL_TARGETS = [];
3042

3143
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -582,32 +594,6 @@ export function discoverBundledPluginRuntimeDeps(params = {}) {
582594
.toSorted((a, b) => a.name.localeCompare(b.name));
583595
}
584596

585-
export function createNestedNpmInstallEnv(env = process.env) {
586-
const nextEnv = { ...env };
587-
delete nextEnv.npm_config_global;
588-
delete nextEnv.npm_config_location;
589-
delete nextEnv.npm_config_prefix;
590-
return nextEnv;
591-
}
592-
593-
export function createBundledRuntimeDependencyInstallEnv(env = process.env) {
594-
return {
595-
...createNestedNpmInstallEnv(env),
596-
npm_config_dry_run: "false",
597-
npm_config_fetch_retries: env.npm_config_fetch_retries ?? "5",
598-
npm_config_fetch_retry_maxtimeout: env.npm_config_fetch_retry_maxtimeout ?? "120000",
599-
npm_config_fetch_retry_mintimeout: env.npm_config_fetch_retry_mintimeout ?? "10000",
600-
npm_config_fetch_timeout: env.npm_config_fetch_timeout ?? "300000",
601-
npm_config_legacy_peer_deps: "true",
602-
npm_config_package_lock: "false",
603-
npm_config_save: "false",
604-
};
605-
}
606-
607-
export function createBundledRuntimeDependencyInstallArgs(missingSpecs) {
608-
return ["install", "--ignore-scripts", ...missingSpecs];
609-
}
610-
611597
function shouldEagerInstallBundledPluginDeps(env = process.env) {
612598
return env?.[EAGER_BUNDLED_PLUGIN_DEPS_ENV]?.trim() === "1";
613599
}
@@ -1003,19 +989,12 @@ export function runBundledPluginPostinstall(params = {}) {
1003989
comSpec: params.comSpec,
1004990
npmArgs: createBundledRuntimeDependencyInstallArgs(missingSpecs),
1005991
});
1006-
const result = spawn(npmRunner.command, npmRunner.args, {
992+
runBundledRuntimeDependencyNpmInstall({
1007993
cwd: packageRoot,
1008-
encoding: "utf8",
994+
npmRunner,
1009995
env: npmRunner.env ?? installEnv,
1010-
stdio: "pipe",
1011-
windowsHide: true,
1012-
shell: npmRunner.shell,
1013-
windowsVerbatimArguments: npmRunner.windowsVerbatimArguments,
996+
spawnSyncImpl: spawn,
1014997
});
1015-
if (result.status !== 0) {
1016-
const output = [result.stderr, result.stdout].filter(Boolean).join("\n").trim();
1017-
throw new Error(output || "npm install failed");
1018-
}
1019998
log.log(`[postinstall] installed bundled plugin deps: ${missingSpecs.join(", ")}`);
1020999
} catch (e) {
10211000
// Non-fatal: gateway will surface the missing dep via doctor.

scripts/stage-bundled-plugin-runtime-deps.mjs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { spawnSync } from "node:child_process";
21
import { createHash } from "node:crypto";
32
import fs from "node:fs";
43
import path from "node:path";
54
import { performance } from "node:perf_hooks";
65
import { pathToFileURL } from "node:url";
76
import semverSatisfies from "semver/functions/satisfies.js";
7+
import {
8+
createBundledRuntimeDependencyInstallArgs,
9+
createBundledRuntimeDependencyInstallEnv,
10+
runBundledRuntimeDependencyNpmInstall,
11+
} from "./lib/bundled-runtime-deps-install.mjs";
812
import { resolveNpmRunner } from "./npm-runner.mjs";
913

1014
const TRANSIENT_TEMP_REMOVE_ERROR_CODES = new Set(["EBUSY", "ENOTEMPTY", "EPERM"]);
@@ -905,39 +909,17 @@ function createRuntimeInstallManifest(pluginId, pinnedGroups) {
905909
}
906910

907911
function runNpmInstall(params) {
908-
const npmEnv = {
909-
...(params.npmRunner.env ?? process.env),
910-
CI: "1",
911-
npm_config_audit: "false",
912-
npm_config_dry_run: "false",
913-
npm_config_fetch_retries: process.env.npm_config_fetch_retries ?? "5",
914-
npm_config_fetch_retry_maxtimeout: process.env.npm_config_fetch_retry_maxtimeout ?? "120000",
915-
npm_config_fetch_retry_mintimeout: process.env.npm_config_fetch_retry_mintimeout ?? "10000",
916-
npm_config_fetch_timeout: process.env.npm_config_fetch_timeout ?? "300000",
917-
npm_config_fund: "false",
918-
npm_config_legacy_peer_deps: "true",
919-
npm_config_loglevel: "error",
920-
npm_config_package_lock: "false",
921-
npm_config_progress: "false",
922-
npm_config_save: "false",
923-
npm_config_yes: "true",
924-
};
925-
const runSpawnSync = params.spawnSyncImpl ?? spawnSync;
926-
const result = runSpawnSync(params.npmRunner.command, params.npmRunner.args, {
912+
return runBundledRuntimeDependencyNpmInstall({
927913
cwd: params.cwd,
928-
encoding: "utf8",
929-
env: npmEnv,
930-
shell: params.npmRunner.shell,
914+
npmRunner: params.npmRunner,
915+
env: createBundledRuntimeDependencyInstallEnv(params.npmRunner.env ?? process.env, {
916+
ci: true,
917+
quiet: true,
918+
}),
919+
spawnSyncImpl: params.spawnSyncImpl,
931920
stdio: ["ignore", "pipe", "pipe"],
932921
timeout: params.timeoutMs ?? 5 * 60 * 1000,
933-
windowsHide: true,
934-
windowsVerbatimArguments: params.npmRunner.windowsVerbatimArguments,
935922
});
936-
if (result.status === 0) {
937-
return;
938-
}
939-
const output = [result.stderr, result.stdout].filter(Boolean).join("\n").trim();
940-
throw new Error(output || "npm install failed");
941923
}
942924

943925
function resolveLegacyRuntimeDepsStampPath(pluginDir) {
@@ -1203,7 +1185,11 @@ function installPluginRuntimeDeps(params) {
12031185
runNpmInstall({
12041186
cwd: tempInstallDir,
12051187
npmRunner: resolveNpmRunner({
1206-
npmArgs: ["install", "--no-audit", "--no-fund", "--ignore-scripts", "--silent"],
1188+
npmArgs: createBundledRuntimeDependencyInstallArgs([], {
1189+
noAudit: true,
1190+
noFund: true,
1191+
silent: true,
1192+
}),
12071193
}),
12081194
});
12091195
}

0 commit comments

Comments
 (0)