* Spawns a given command with the specified arguments inside a shell synchronously. * * @returns The command's stdout and stderr.
(command: string, args: string[], options: SpawnSyncOptions = {})
| 85 | reject(sanitizedErr); |
| 86 | }); |
| 87 | // The `close` event is used because the process is guaranteed to have completed writing to |
| 88 | // stdout and stderr, using the `exit` event can cause inconsistent information in stdout and |
| 89 | // stderr due to a race condition around exiting. |
| 90 | childProcess.on('close', (status) => (status === 0 ? resolve() : reject(status))); |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Spawns a given command with the specified arguments inside a shell synchronously. |
| 96 | * |
| 97 | * @returns The command's stdout and stderr. |
| 98 | */ |
| 99 | static spawnSync(command: string, args: string[], options: SpawnSyncOptions = {}): SpawnResult { |
| 100 | // Default shell to false to prevent OS command injection: with shell: true, Node.js |
| 101 | // internally joins command + args into a single string evaluated by /bin/sh, making |
| 102 | // shell metacharacters in args exploitable. Callers that genuinely require shell |
| 103 | // features (e.g. sourcing shell scripts) may explicitly pass shell: true. |
| 104 | const commandText = `${command} ${args.join(' ')}`; |
| 105 | const env = getEnvironmentForNonInteractiveCommand(options.env, options.cwd); |
| 106 | |
| 107 | Log.debug(`Executing command: ${sanitize(commandText)}`); |
| 108 | |
| 109 | const { |
| 110 | status: exitCode, |
| 111 | signal, |
| 112 | stdout, |
| 113 | stderr, |
| 114 | } = _spawnSync(command, args, {...options, env, encoding: 'utf8', stdio: 'pipe'}); |
| 115 |
no test coverage detected