test: run npm pack via npm-cli.js

This commit is contained in:
Peter Steinberger
2026-01-12 01:31:21 +00:00
parent cd65183f24
commit 07b93e1d26

View File

@@ -14,6 +14,38 @@ function makeTempDir() {
return dir; return dir;
} }
function resolveNpmCliJs() {
const fromEnv = process.env.npm_execpath;
if (
fromEnv?.includes(`${path.sep}npm${path.sep}`) &&
fromEnv?.endsWith("npm-cli.js")
) {
return fromEnv ?? null;
}
const fromNodeDir = path.join(
path.dirname(process.execPath),
"node_modules",
"npm",
"bin",
"npm-cli.js",
);
if (fs.existsSync(fromNodeDir)) return fromNodeDir;
const fromLibNodeModules = path.resolve(
path.dirname(process.execPath),
"..",
"lib",
"node_modules",
"npm",
"bin",
"npm-cli.js",
);
if (fs.existsSync(fromLibNodeModules)) return fromLibNodeModules;
return null;
}
function packToArchive({ function packToArchive({
pkgDir, pkgDir,
outDir, outDir,
@@ -23,13 +55,13 @@ function packToArchive({
outDir: string; outDir: string;
outName: string; outName: string;
}) { }) {
const res = spawnSync( const npmCli = resolveNpmCliJs();
"npm", const cmd = npmCli ? process.execPath : "npm";
["pack", "--silent", "--pack-destination", outDir, pkgDir], const args = npmCli
{ ? [npmCli, "pack", "--silent", "--pack-destination", outDir, pkgDir]
encoding: "utf-8", : ["pack", "--silent", "--pack-destination", outDir, pkgDir];
},
); const res = spawnSync(cmd, args, { encoding: "utf-8" });
expect(res.status).toBe(0); expect(res.status).toBe(0);
if (res.status !== 0) { if (res.status !== 0) {
throw new Error( throw new Error(