From 07b93e1d26b87cabdaf6367f01a571655cc1b230 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 12 Jan 2026 01:31:21 +0000 Subject: [PATCH] test: run npm pack via npm-cli.js --- src/plugins/install.test.ts | 46 +++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/plugins/install.test.ts b/src/plugins/install.test.ts index c29030e07..c2abdbffb 100644 --- a/src/plugins/install.test.ts +++ b/src/plugins/install.test.ts @@ -14,6 +14,38 @@ function makeTempDir() { 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({ pkgDir, outDir, @@ -23,13 +55,13 @@ function packToArchive({ outDir: string; outName: string; }) { - const res = spawnSync( - "npm", - ["pack", "--silent", "--pack-destination", outDir, pkgDir], - { - encoding: "utf-8", - }, - ); + const npmCli = resolveNpmCliJs(); + const cmd = npmCli ? process.execPath : "npm"; + const args = npmCli + ? [npmCli, "pack", "--silent", "--pack-destination", outDir, pkgDir] + : ["pack", "--silent", "--pack-destination", outDir, pkgDir]; + + const res = spawnSync(cmd, args, { encoding: "utf-8" }); expect(res.status).toBe(0); if (res.status !== 0) { throw new Error(