@@ -67,4 +67,49 @@ export const standalone = (
6767 } ;
6868} ;
6969
70+ const isWindows = process . platform === 'win32' ;
71+
72+ function quoteCmdArg ( arg : string ) {
73+ return `"${ arg . replace ( / \\ / g, '\\\\' ) . replace ( / " / g, '\\"' ) } "` ;
74+ }
75+
76+ function quotePwshArg ( arg : string ) {
77+ return `'${ arg . replace ( / ' / g, "''" ) } '` ;
78+ }
79+
80+ function quotePosixShArg ( arg : string ) {
81+ return `'${ arg . replace ( / ' / g, "'\\''" ) } '` ;
82+ }
83+
84+ function testWindowsExe ( cmd : string , file : string ) {
85+ return new RegExp ( `^(?:.*\\\\)?${ cmd } (?:\\.exe)?$` , 'i' ) . test ( file ) ;
86+ }
87+
88+ function parseArgs ( unparsed_args : string [ ] ) {
89+ if ( isWindows === true ) {
90+ const file = process . env [ 'comspec' ] || 'cmd.exe' ;
91+ if ( testWindowsExe ( 'cmd' , file ) === true ) {
92+ return unparsed_args . map ( ( i ) => quoteCmdArg ( i ) ) ;
93+ }
94+ if ( testWindowsExe ( '(powershell|pwsh)' , file ) || file . endsWith ( '/pwsh' ) ) {
95+ return unparsed_args . map ( ( i ) => quotePwshArg ( i ) ) ;
96+ }
97+ return unparsed_args ;
98+ }
99+ return unparsed_args . map ( ( i ) => quotePosixShArg ( i ) ) ;
100+ }
101+
102+ export function setStandaloneArgs (
103+ unparsed_args : string [ ] ,
104+ shell : boolean
105+ ) : string [ ] {
106+ let parsedArgs = unparsed_args ;
107+ if ( shell === true ) {
108+ parsedArgs = parseArgs ( unparsed_args ) ;
109+ }
110+ return parsedArgs ;
111+ }
112+
113+ export const standaloneUseShell = isWindows ;
114+
70115export default standalone ( ) ;
0 commit comments