* Spawns a child process. The returned Command may be used to wait * for the process result or to send signals to the process. * * @param {string} command The executable to spawn. * @param {Options=} opt_options The command options. * @return {!Command} The launched command.
(command, opt_options)
| 121 | * @return {!Command} The launched command. |
| 122 | */ |
| 123 | function exec(command, opt_options) { |
| 124 | const options = opt_options || {} |
| 125 | |
| 126 | let proc = childProcess.spawn(command, options.args || [], { |
| 127 | env: options.env || process.env, |
| 128 | stdio: options.stdio || 'ignore', |
| 129 | }) |
| 130 | |
| 131 | // This process should not wait on the spawned child, however, we do |
| 132 | // want to ensure the child is killed when this process exits. |
| 133 | proc.unref() |
| 134 | process.once('exit', onProcessExit) |
| 135 | |
| 136 | const result = new Promise((resolve, reject) => { |
| 137 | proc.once('exit', (code, signal) => { |
| 138 | proc = null |
| 139 | process.removeListener('exit', onProcessExit) |
| 140 | resolve(new Result(code, signal)) |
| 141 | }) |
| 142 | |
| 143 | proc.once('error', (err) => { |
| 144 | reject(err) |
| 145 | }) |
| 146 | }) |
| 147 | return new Command(result, killCommand) |
| 148 | |
| 149 | function onProcessExit() { |
| 150 | killCommand('SIGTERM') |
| 151 | } |
| 152 | |
| 153 | function killCommand(signal) { |
| 154 | process.removeListener('exit', onProcessExit) |
| 155 | if (proc) { |
| 156 | proc.kill(signal) |
| 157 | proc = null |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Exported to improve generated API documentation. |
| 163 |
no test coverage detected