11import assert from "node:assert/strict" ;
2- import { execFile } from "node:child_process" ;
2+ import { exec , execFile } from "node:child_process" ;
33import {
44 cp ,
55 mkdtemp ,
@@ -15,11 +15,15 @@ import { promisify } from "node:util";
1515import { fileURLToPath } from "node:url" ;
1616
1717const execute = promisify ( execFile ) ;
18+ const executeShell = promisify ( exec ) ;
1819const testDirectory = dirname ( fileURLToPath ( import . meta. url ) ) ;
1920const packageRoot = resolve ( testDirectory , ".." ) ;
2021const workspaceRoot = resolve ( packageRoot , "../.." ) ;
2122const pnpmCli = process . env . npm_execpath ;
22- const npmCli = resolve ( dirname ( process . execPath ) , "node_modules/npm/bin/npm-cli.js" ) ;
23+ const npmExecutable = join (
24+ dirname ( process . execPath ) ,
25+ process . platform === "win32" ? "npm.cmd" : "npm"
26+ ) ;
2327
2428if ( ! pnpmCli ) {
2529 throw new Error ( "Run this test through pnpm so npm_execpath is available." ) ;
6468 private : true
6569 } , null , 2 ) , "utf8" ) ;
6670
67- await runNodeCli ( npmCli , [
71+ await runNpm ( [
6872 "install" ,
6973 "--no-package-lock" ,
7074 "--ignore-scripts" ,
@@ -106,11 +110,30 @@ try {
106110}
107111
108112async function runDevmap ( cwd , args ) {
109- return runNodeCli ( npmCli , [ "exec" , "--" , "devmap" , ...args ] , cwd ) ;
113+ return runNpm ( [ "exec" , "--" , "devmap" , ...args ] , cwd ) ;
110114}
111115
112116async function runNodeCli ( cliPath , args , cwd ) {
113- return execute ( process . execPath , [ cliPath , ...args ] , {
117+ return runExecutable ( process . execPath , [ cliPath , ...args ] , cwd ) ;
118+ }
119+
120+ async function runNpm ( args , cwd ) {
121+ if ( process . platform !== "win32" ) {
122+ return runExecutable ( npmExecutable , args , cwd ) ;
123+ }
124+
125+ const command = [ npmExecutable , ...args ]
126+ . map ( quoteWindowsArgument )
127+ . join ( " " ) ;
128+ return executeShell ( command , commandOptions ( cwd ) ) ;
129+ }
130+
131+ async function runExecutable ( executable , args , cwd ) {
132+ return execute ( executable , args , commandOptions ( cwd ) ) ;
133+ }
134+
135+ function commandOptions ( cwd ) {
136+ return {
114137 cwd,
115138 env : {
116139 ...process . env ,
@@ -120,7 +143,11 @@ async function runNodeCli(cliPath, args, cwd) {
120143 } ,
121144 maxBuffer : 10 * 1024 * 1024 ,
122145 windowsHide : true
123- } ) ;
146+ } ;
147+ }
148+
149+ function quoteWindowsArgument ( value ) {
150+ return `"${ value . replaceAll ( '"' , '""' ) } "` ;
124151}
125152
126153function stripAnsi ( value ) {
0 commit comments