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

@@ -24,8 +24,20 @@ import {
import { tmpdir } from "node:os";
import { basename, dirname, isAbsolute, join, posix, relative } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import {
createBundledRuntimeDependencyInstallArgs,
createBundledRuntimeDependencyInstallEnv,
createNestedNpmInstallEnv,
runBundledRuntimeDependencyNpmInstall,
} from "./lib/bundled-runtime-deps-install.mjs";
import { resolveNpmRunner } from "./npm-runner.mjs";
export {
createBundledRuntimeDependencyInstallArgs,
createBundledRuntimeDependencyInstallEnv,
createNestedNpmInstallEnv,
};
export const BUNDLED_PLUGIN_INSTALL_TARGETS = [];
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -582,32 +594,6 @@ export function discoverBundledPluginRuntimeDeps(params = {}) {
.toSorted((a, b) => a.name.localeCompare(b.name));
}
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) {
return {
...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",
};
}
export function createBundledRuntimeDependencyInstallArgs(missingSpecs) {
return ["install", "--ignore-scripts", ...missingSpecs];
}
function shouldEagerInstallBundledPluginDeps(env = process.env) {
return env?.[EAGER_BUNDLED_PLUGIN_DEPS_ENV]?.trim() === "1";
}
@@ -1003,19 +989,12 @@ export function runBundledPluginPostinstall(params = {}) {
comSpec: params.comSpec,
npmArgs: createBundledRuntimeDependencyInstallArgs(missingSpecs),
});
const result = spawn(npmRunner.command, npmRunner.args, {
runBundledRuntimeDependencyNpmInstall({
cwd: packageRoot,
encoding: "utf8",
npmRunner,
env: npmRunner.env ?? installEnv,
stdio: "pipe",
windowsHide: true,
shell: npmRunner.shell,
windowsVerbatimArguments: npmRunner.windowsVerbatimArguments,
spawnSyncImpl: spawn,
});
if (result.status !== 0) {
const output = [result.stderr, result.stdout].filter(Boolean).join("\n").trim();
throw new Error(output || "npm install failed");
}
log.log(`[postinstall] installed bundled plugin deps: ${missingSpecs.join(", ")}`);
} catch (e) {
// Non-fatal: gateway will surface the missing dep via doctor.