forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc.ts
More file actions
63 lines (54 loc) · 1.97 KB
/
proc.ts
File metadata and controls
63 lines (54 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { EventEmitter } from 'events';
import { IDisposable } from '../types';
import { EnvironmentVariables } from '../variables/types';
import { execObservable, killPid, plainExec, shellExec } from './rawProcessApis';
import {
ExecutionResult,
IBufferDecoder,
IProcessService,
ObservableExecutionResult,
ShellOptions,
SpawnOptions,
} from './types';
export class ProcessService extends EventEmitter implements IProcessService {
private processesToKill = new Set<IDisposable>();
constructor(private readonly decoder: IBufferDecoder, private readonly env?: EnvironmentVariables) {
super();
}
public static isAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
public static kill(pid: number): void {
killPid(pid);
}
public dispose(): void {
this.removeAllListeners();
this.processesToKill.forEach((p) => {
try {
p.dispose();
} catch {
// ignore.
}
});
}
public execObservable(file: string, args: string[], options: SpawnOptions = {}): ObservableExecutionResult<string> {
const result = execObservable(file, args, options, this.decoder, this.env, this.processesToKill);
this.emit('exec', file, args, options);
return result;
}
public exec(file: string, args: string[], options: SpawnOptions = {}): Promise<ExecutionResult<string>> {
const promise = plainExec(file, args, options, this.decoder, this.env, this.processesToKill);
this.emit('exec', file, args, options);
return promise;
}
public shellExec(command: string, options: ShellOptions = {}): Promise<ExecutionResult<string>> {
return shellExec(command, options, this.env, this.processesToKill);
}
}