refactor(plugins): share bundled runtime deps install script helpers

This commit is contained in:
Peter Steinberger
2026-04-29 17:34:56 +01:00
parent f4af0777a7
commit 0519107bd3
3 changed files with 98 additions and 67 deletions

View File

@@ -0,0 +1,66 @@
import { spawnSync } from "node:child_process";
export function createNestedNpmInstallEnv(env = process.env) {
const nextEnv = { ...env };
delete nextEnv.npm_config_global;
delete nextEnv.npm_config_location;
delete nextEnv.npm_config_prefix;
return nextEnv;
}
export function createBundledRuntimeDependencyInstallEnv(env = process.env, options = {}) {
const nextEnv = {
...createNestedNpmInstallEnv(env),
npm_config_dry_run: "false",
npm_config_fetch_retries: env.npm_config_fetch_retries ?? "5",
npm_config_fetch_retry_maxtimeout: env.npm_config_fetch_retry_maxtimeout ?? "120000",
npm_config_fetch_retry_mintimeout: env.npm_config_fetch_retry_mintimeout ?? "10000",
npm_config_fetch_timeout: env.npm_config_fetch_timeout ?? "300000",
npm_config_legacy_peer_deps: "true",
npm_config_package_lock: "false",
npm_config_save: "false",
};
if (options.ci) {
nextEnv.CI = "1";
}
if (options.quiet) {
Object.assign(nextEnv, {
npm_config_audit: "false",
npm_config_fund: "false",
npm_config_loglevel: "error",
npm_config_progress: "false",
npm_config_yes: "true",
});
}
return nextEnv;
}
export function createBundledRuntimeDependencyInstallArgs(specs = [], options = {}) {
return [
"install",
...(options.noAudit ? ["--no-audit"] : []),
...(options.noFund ? ["--no-fund"] : []),
"--ignore-scripts",
...(options.silent ? ["--silent"] : []),
...specs,
];
}
export function runBundledRuntimeDependencyNpmInstall(params) {
const runSpawnSync = params.spawnSyncImpl ?? spawnSync;
const result = runSpawnSync(params.npmRunner.command, params.npmRunner.args, {
cwd: params.cwd,
encoding: "utf8",
env: params.env ?? params.npmRunner.env ?? process.env,
shell: params.npmRunner.shell,
stdio: params.stdio ?? "pipe",
...(params.timeoutMs ? { timeout: params.timeoutMs } : {}),
windowsHide: true,
windowsVerbatimArguments: params.npmRunner.windowsVerbatimArguments,
});
if (result.status === 0) {
return;
}
const output = [result.stderr, result.stdout].filter(Boolean).join("\n").trim();
throw new Error(output || "npm install failed");
}