(command: string, token?: vscode.CancellationToken)
| 167 | } |
| 168 | |
| 169 | function spawnChildProcess(command: string, token?: vscode.CancellationToken): Promise<string> { |
| 170 | return new Promise<string>((resolve, reject) => { |
| 171 | const process: child_process.ChildProcess = child_process.spawn(command, { shell: true }); |
| 172 | let stdout: string = ""; |
| 173 | let stderr: string = ""; |
| 174 | |
| 175 | if (process) { |
| 176 | let cancellationTokenListener: vscode.Disposable | undefined; // eslint-disable-line prefer-const |
| 177 | // Handle timeout |
| 178 | const seconds: number = 30; |
| 179 | const processTimeout: NodeJS.Timeout = setTimeout(() => { |
| 180 | process.removeAllListeners(); |
| 181 | if (cancellationTokenListener) { |
| 182 | cancellationTokenListener.dispose(); |
| 183 | } |
| 184 | |
| 185 | try { |
| 186 | process.kill(); |
| 187 | } catch { |
| 188 | // Failed to kill process. |
| 189 | } |
| 190 | reject(new Error(localize("timeout.processList.spawn", '"{0}" timed out after {1} seconds.', command, seconds))); |
| 191 | return; |
| 192 | }, seconds * 1000); |
| 193 | |
| 194 | // Handle cancellation |
| 195 | cancellationTokenListener = token?.onCancellationRequested(() => { |
| 196 | clearTimeout(processTimeout); |
| 197 | process.removeAllListeners(); |
| 198 | |
| 199 | try { |
| 200 | process.kill(); |
| 201 | } catch { |
| 202 | // Failed to kill process. |
| 203 | } |
| 204 | reject(new Error(localize("cancel.processList.spawn", '"{0}" canceled.', command))); |
| 205 | return; |
| 206 | }); |
| 207 | |
| 208 | const cleanUpCallbacks = () => { |
| 209 | clearTimeout(processTimeout); |
| 210 | process.removeAllListeners(); |
| 211 | if (cancellationTokenListener) { |
| 212 | cancellationTokenListener.dispose(); |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | // Handle data streams |
| 217 | if (process.stdout) { |
| 218 | process.stdout.on('data', (data: string) => { |
| 219 | stdout += data.toString(); |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | if (process.stderr) { |
| 224 | process.stderr.on('data', (data: string) => { |
| 225 | stderr += data.toString(); |
| 226 | }); |
no test coverage detected