(workingDirectory: string | undefined, binPath: string, args: string[], env: Record<string, string | undefined> | undefined)
| 9 | const simpleCommandRegex = new RegExp("^[\\w\\-.]+$"); |
| 10 | |
| 11 | export function safeSpawn(workingDirectory: string | undefined, binPath: string, args: string[], env: Record<string, string | undefined> | undefined): SpawnedProcess { |
| 12 | const customEnv = Object.assign({}, process.env, env); |
| 13 | |
| 14 | // On Windows we need to use shell-execute for running `.bat` files. |
| 15 | // Try to limit when we use this, because terminating a shell might not terminate |
| 16 | // the spawned process, so not using shell-execute may improve reliability of |
| 17 | // terminating processes. |
| 18 | if (isWin && binPath.endsWith(".bat")) { |
| 19 | const quotedArgs = args.map(quoteAndEscapeArg); |
| 20 | // Putting quotes around something like "git" will cause it to fail, so don't do it if binPath is just a single identifier. |
| 21 | binPath = simpleCommandRegex.test(binPath) ? binPath : `"${binPath}"`; |
| 22 | return child_process.spawn(binPath, quotedArgs, { cwd: workingDirectory, env: customEnv, shell: true }) satisfies SpawnedProcess; |
| 23 | } |
| 24 | |
| 25 | return child_process.spawn(binPath, args, { cwd: workingDirectory, env: customEnv }) satisfies SpawnedProcess; |
| 26 | } |
| 27 | |
| 28 | function quoteAndEscapeArg(arg: string) { |
| 29 | // Spawning processes on Windows with funny symbols in the path requires quoting. However if you quote an |
no test coverage detected