-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.utils.ts
More file actions
50 lines (41 loc) · 1.16 KB
/
process.utils.ts
File metadata and controls
50 lines (41 loc) · 1.16 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
import { exec } from 'node:child_process';
import type {
AppUtils,
ProcessUtilsExecOptions,
ProcessUtils as ProcessUtilsInterface,
} from '@/interfaces';
export class ProcessUtils implements ProcessUtilsInterface {
constructor(private readonly appUtils: AppUtils) {}
public async exec(
command: string,
options: ProcessUtilsExecOptions = { showStdout: false },
): Promise<string> {
return new Promise((resolve, reject) => {
const childProcess = exec(command);
let stdout = '';
childProcess.stdout.on('data', (data) => {
if (options.showStdout) {
process.stdout.write(data);
}
stdout += data.toString();
});
childProcess.stderr.on('data', (data) => {
process.stderr.write(data);
});
childProcess.on('error', (error) => {
this.appUtils.logger.error(
`Error executing command '${command}'\n`,
error,
);
reject(error);
});
childProcess.on('close', (code) => {
if (code !== 0) {
reject(`Command failed with exit code ${code}`);
} else {
resolve(stdout);
}
});
});
}
}