-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseCommandRunner.ts
78 lines (75 loc) · 1.95 KB
/
BaseCommandRunner.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import * as child_process from "child_process";
import { Util } from "./Util";
export interface CommandRunnerOptions {
maxBuffer: number;
encoding: string;
isWslMode: boolean;
shellPath: string | undefined;
cwd?: string;
dockerContainer?: string | undefined;
}
export class BaseCommandRunner {
public async exec(command: string, options: child_process.ExecOptions): Promise<{
stdout: string;
stderr: string;
}> {
return new Promise<{
stdout: string;
stderr: string;
}>((resolve, reject) => {
child_process.exec(command, options, (error, stdout, stderr) => {
if (stdout) {
stdout = stdout.toString();
}
if (stderr) {
stderr = stdout.toString();
}
if (error) {
console.log(command);
console.log(options);
console.error(error);
let _errorExceptionMsg = error.toString();
let errorMsg = [_errorExceptionMsg, stdout, stderr].join("\n");
reject(errorMsg);
}
resolve({ stdout, stderr });
});
});
}
}
export class SimpleCommandRunner {
constructor(private readonly options: CommandRunnerOptions) {
}
public async exec(command: string): Promise<{
stdout: string;
stderr: string;
}> {
return new Promise<{
stdout: string;
stderr: string;
}>((resolve, reject) => {
let options: child_process.ExecOptions = {
maxBuffer: this.options.maxBuffer,
shell: this.options.shellPath,
cwd: this.options.cwd || Util.homedir
};
child_process.exec(command, options, (error, stdout, stderr) => {
if (stdout) {
stdout = stdout.toString();
}
if (stderr) {
stderr = stdout.toString();
}
if (error) {
console.log(command);
console.log(options);
console.error(error);
let _errorExceptionMsg = error.toString();
let errorMsg = [_errorExceptionMsg, stdout, stderr].join("\n");
reject(errorMsg);
}
resolve({ stdout, stderr });
});
});
}
}