| 37 | * Environment variables to launch with can be passed into the constructor. |
| 38 | */ |
| 39 | export class ProcessService implements IProcessService { |
| 40 | private processesToKill = new Set<IDisposable>(); |
| 41 | private readonly decoder: IBufferDecoder; |
| 42 | constructor(private readonly env?: EnvironmentVariables) { |
| 43 | this.decoder = new BufferDecoder(); |
| 44 | } |
| 45 | public static isAlive(pid?: number): boolean { |
| 46 | try { |
| 47 | if (!pid) { |
| 48 | return false; |
| 49 | } |
| 50 | process.kill(pid, 0); |
| 51 | return true; |
| 52 | } catch { |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | public static kill(pid?: number): void { |
| 57 | try { |
| 58 | if (!pid) { |
| 59 | return; |
| 60 | } |
| 61 | if (process.platform === 'win32') { |
| 62 | // Windows doesn't support SIGTERM, so execute taskkill to kill the process |
| 63 | execSync(`taskkill /pid ${pid} /T /F`); |
| 64 | } else { |
| 65 | process.kill(pid); |
| 66 | } |
| 67 | } catch { |
| 68 | // Ignore. |
| 69 | } |
| 70 | } |
| 71 | public dispose() { |
| 72 | this.processesToKill.forEach((p) => { |
| 73 | try { |
| 74 | p.dispose(); |
| 75 | } catch { |
| 76 | // ignore. |
| 77 | } |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | public execObservable(file: string, args: string[], options: SpawnOptions = {}): ObservableExecutionResult<string> { |
| 82 | const spawnOptions = this.getDefaultOptions(options); |
| 83 | const proc = spawn(file, args, spawnOptions); |
| 84 | let procExited = false; |
| 85 | logger.ci(`Exec observable ${file}, ${args.join(' ')}`); |
| 86 | const disposables: IDisposable[] = []; |
| 87 | const disposable: IDisposable = { |
| 88 | // eslint-disable-next-line |
| 89 | dispose: function () { |
| 90 | if (proc && !proc.killed && !procExited) { |
| 91 | ProcessService.kill(proc.pid); |
| 92 | } |
| 93 | if (proc) { |
| 94 | proc.unref(); |
| 95 | } |
| 96 | dispose(disposables); |
nothing calls this directly
no test coverage detected