11/* eslint camelcase: "off" */
22const isWindows = require ( './is-windows.js' )
33const setPATH = require ( './set-path.js' )
4- const { unlinkSync : unlink , writeFileSync : writeFile } = require ( 'fs' )
5- const { tmpdir } = require ( 'os' )
64const { resolve } = require ( 'path' )
75const which = require ( 'which' )
86const npm_config_node_gyp = require . resolve ( 'node-gyp/bin/node-gyp.js' )
97const escape = require ( './escape.js' )
10- const { randomBytes } = require ( 'crypto' )
11-
12- const translateWinPathToPosix = ( path ) => {
13- return path
14- . replace ( / ^ ( [ A - z ] ) : / , '/$1' )
15- . replace ( / \\ / g, '/' )
16- }
178
189const makeSpawnArgs = options => {
1910 const {
@@ -38,10 +29,7 @@ const makeSpawnArgs = options => {
3829 npm_config_node_gyp,
3930 } )
4031
41- const fileName = escape . filename ( `${ event } -${ randomBytes ( 4 ) . toString ( 'hex' ) } ` )
42- let scriptFile
43- let script = ''
44-
32+ let doubleEscape = false
4533 const isCmd = / (?: ^ | \\ ) c m d (?: \. e x e ) ? $ / i. test ( scriptShell )
4634 if ( isCmd ) {
4735 let initialCmd = ''
@@ -68,26 +56,18 @@ const makeSpawnArgs = options => {
6856 pathToInitial = initialCmd . toLowerCase ( )
6957 }
7058
71- const doubleEscape = pathToInitial . endsWith ( '.cmd' ) || pathToInitial . endsWith ( '.bat' )
72-
73- scriptFile = resolve ( tmpdir ( ) , `${ fileName } .cmd` )
74- script += '@echo off\n'
75- script += cmd
76- if ( args . length ) {
77- script += ` ${ args . map ( ( arg ) => escape . cmd ( arg , doubleEscape ) ) . join ( ' ' ) } `
78- }
79- } else {
80- scriptFile = resolve ( tmpdir ( ) , `${ fileName } .sh` )
81- script = cmd
82- if ( args . length ) {
83- script += ` ${ args . map ( ( arg ) => escape . sh ( arg ) ) . join ( ' ' ) } `
84- }
59+ doubleEscape = pathToInitial . endsWith ( '.cmd' ) || pathToInitial . endsWith ( '.bat' )
8560 }
8661
87- writeFile ( scriptFile , script )
62+ let script = cmd
63+ for ( const arg of args ) {
64+ script += isCmd
65+ ? ` ${ escape . cmd ( arg , doubleEscape ) } `
66+ : ` ${ escape . sh ( arg ) } `
67+ }
8868 const spawnArgs = isCmd
89- ? [ '/d' , '/s' , '/c' , escape . cmd ( scriptFile ) ]
90- : [ isWindows ? translateWinPathToPosix ( scriptFile ) : scriptFile ]
69+ ? [ '/d' , '/s' , '/c' , script ]
70+ : [ '-c' , '--' , script ]
9171
9272 const spawnOpts = {
9373 env : spawnEnv ,
@@ -97,16 +77,7 @@ const makeSpawnArgs = options => {
9777 ...( isCmd ? { windowsVerbatimArguments : true } : { } ) ,
9878 }
9979
100- const cleanup = ( ) => {
101- // delete the script, this is just a best effort
102- try {
103- unlink ( scriptFile )
104- } catch ( err ) {
105- // ignore errors
106- }
107- }
108-
109- return [ scriptShell , spawnArgs , spawnOpts , cleanup ]
80+ return [ scriptShell , spawnArgs , spawnOpts ]
11081}
11182
11283module . exports = makeSpawnArgs
0 commit comments