1+ import { type SpawnOptions } from "node:child_process" ;
12import fs from "node:fs" ;
23import { createRequire } from "node:module" ;
34import path from "node:path" ;
@@ -628,9 +629,93 @@ class WebpackCLI {
628629 } ;
629630 }
630631
631- async getDefaultPackageManager ( ) : Promise < PackageManager | undefined > {
632- const { sync } = await import ( "cross-spawn" ) ;
632+ /**
633+ * Runs a command, the same as `cross-spawn` does.
634+ * @param {string } command command
635+ * @param {string[] } args arguments
636+ * @param {SpawnOptions } options options
637+ * @returns {Promise<{ status: number | null, error?: Error }> } exit status, or the error when the command fails to start
638+ */
639+ async #spawn(
640+ command : string ,
641+ args : string [ ] ,
642+ options : SpawnOptions = { } ,
643+ ) : Promise < { status : number | null ; error ?: Error } > {
644+ const { spawn } = await import ( "node:child_process" ) ;
645+ const run = ( command : string , args : string [ ] , options : SpawnOptions ) =>
646+ new Promise < { status : number | null ; error ?: Error } > ( ( resolve ) => {
647+ spawn ( command , args , options )
648+ . once ( "error" , ( error ) => resolve ( { status : null , error } ) )
649+ . once ( "close" , ( status ) => resolve ( { status } ) ) ;
650+ } ) ;
651+
652+ if ( process . platform !== "win32" ) {
653+ return run ( command , args , options ) ;
654+ }
655+
656+ // Find the file of the command like `which`, on Windows the current directory goes first
657+ const extensions = ( process . env . PATHEXT || ".EXE;.CMD;.BAT;.COM" ) . split ( ";" ) ;
658+
659+ if ( command . includes ( "." ) && extensions [ 0 ] !== "" ) {
660+ extensions . unshift ( "" ) ;
661+ }
662+
663+ const directories = / [ / \\ ] / . test ( command )
664+ ? [ "" ]
665+ : [ process . cwd ( ) , ...( process . env . PATH || "" ) . split ( ";" ) ] ;
666+ let file : string | undefined ;
667+
668+ search: for ( const directory of directories ) {
669+ for ( const extension of extensions ) {
670+ const candidate = path . resolve (
671+ path . join ( directory . replace ( / ^ " ( .* ) " $ / , "$1" ) , command ) + extension ,
672+ ) ;
673+
674+ try {
675+ if (
676+ ( await fs . promises . stat ( candidate ) ) . isFile ( ) &&
677+ extensions . some (
678+ ( extension ) =>
679+ extension !== "" && candidate . toLowerCase ( ) . endsWith ( extension . toLowerCase ( ) ) ,
680+ )
681+ ) {
682+ file = candidate ;
683+
684+ break search;
685+ }
686+ } catch {
687+ // Not found
688+ }
689+ }
690+ }
691+
692+ // Executables run directly
693+ if ( file && / \. (?: c o m | e x e ) $ / i. test ( file ) ) {
694+ return run ( command , args , options ) ;
695+ }
696+
697+ // Other files, e.g. `npm.cmd`, only run in a shell, which doesn't escape arguments, and the
698+ // `.cmd` shims of packages pass them to another shell, so they are escaped twice
699+ const isShim = Boolean ( file && / n o d e _ m o d u l e s [ \\ / ] .b i n [ \\ / ] [ ^ \\ / ] + \. c m d $ / i. test ( file ) ) ;
700+ const metaChars = / ( [ ( ) \] [ % ! ^ " ` < > & | ; , * ? ] ) / g;
701+ const escapedArgs = args . map ( ( arg ) => {
702+ const quoted = `"${ arg
703+ // Double the backslashes before a quote and escape the quote
704+ . replaceAll ( / (? = ( \\ + ?) ? ) \1" / g, '$1$1\\"' )
705+ // Double the backslashes at the end, they precede the closing quote
706+ . replace ( / (? = ( \\ + ?) ? ) \1$ / , "$1$1" ) } "`. replaceAll ( metaChars , "^$1" ) ;
707+
708+ return isShim ? quoted . replaceAll ( metaChars , "^$1" ) : quoted ;
709+ } ) ;
633710
711+ return run (
712+ [ path . normalize ( command ) . replaceAll ( metaChars , "^$1" ) , ...escapedArgs ] . join ( " " ) ,
713+ [ ] ,
714+ { ...options , shell : true } ,
715+ ) ;
716+ }
717+
718+ async getDefaultPackageManager ( ) : Promise < PackageManager | undefined > {
634719 try {
635720 await fs . promises . access ( path . resolve ( process . cwd ( ) , "package-lock.json" ) , fs . constants . F_OK ) ;
636721 return "npm" ;
@@ -652,37 +737,20 @@ class WebpackCLI {
652737 // Nothing
653738 }
654739
655- try {
656- // the sync function below will fail if npm is not installed,
657- // an error will be thrown
658- if ( sync ( "npm" , [ "--version" ] ) ) {
659- return "npm" ;
660- }
661- } catch {
662- // Nothing
663- }
740+ // A missing package manager fails to start, or exits with an error in the shell on Windows
741+ for ( const packageManager of [ "npm" , "yarn" , "pnpm" ] as const ) {
742+ const { error, status } = await this . #spawn( packageManager , [ "--version" ] , {
743+ stdio : "ignore" ,
744+ } ) ;
664745
665- try {
666- // the sync function below will fail if yarn is not installed,
667- // an error will be thrown
668- if ( sync ( "yarn" , [ "--version" ] ) ) {
669- return "yarn" ;
746+ if ( ! error && status === 0 ) {
747+ return packageManager ;
670748 }
671- } catch {
672- // Nothing
673749 }
674750
675- try {
676- // the sync function below will fail if pnpm is not installed,
677- // an error will be thrown
678- if ( sync ( "pnpm" , [ "--version" ] ) ) {
679- return "pnpm" ;
680- }
681- } catch {
682- this . logger . error ( "No package manager found." ) ;
751+ this . logger . error ( "No package manager found." ) ;
683752
684- process . exit ( 2 ) ;
685- }
753+ process . exit ( 2 ) ;
686754 }
687755
688756 async isPackageInstalled ( packageName : string ) : Promise < boolean > {
@@ -812,10 +880,18 @@ class WebpackCLI {
812880 }
813881
814882 if ( needInstall ) {
815- const { sync } = await import ( "cross-spawn" ) ;
816-
817883 try {
818- sync ( packageManager , commandArguments , { stdio : "inherit" } ) ;
884+ const { error, status } = await this . #spawn( packageManager , commandArguments , {
885+ stdio : "inherit" ,
886+ } ) ;
887+
888+ if ( error ) {
889+ throw error ;
890+ }
891+
892+ if ( status !== 0 ) {
893+ throw new Error ( `'${ commandToBeRun } ' exited with code ${ status } ` ) ;
894+ }
819895 } catch ( error ) {
820896 this . logger . error ( error ) ;
821897
0 commit comments