forked from rstackjs/rstack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
48 lines (39 loc) · 1.31 KB
/
Copy pathcli.ts
File metadata and controls
48 lines (39 loc) · 1.31 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
import { type ExecSyncOptions, execSync } from 'node:child_process';
import path from 'node:path';
import type { LogHelper } from '@rstackjs/test-utils';
export const RSTACK_BIN_PATH: string = path.join(import.meta.dirname, '../../bin/rs.js');
export type ExecCliOptions = ExecSyncOptions & {
logHelper?: LogHelper;
};
export type ExecCli = (command: string, options?: ExecCliOptions) => string;
export const normalizeHelpOutput = (output: string): string =>
output.replace(/^Rstack v.+/u, 'Rstack v<version>');
type ExecCliError = Error & {
stdout?: Buffer | string;
stderr?: Buffer | string;
};
const addLog = (logHelper: LogHelper | undefined, output: Buffer | string | undefined) => {
if (output) {
logHelper?.addLog(output.toString());
}
};
export const execCli: ExecCli = (command, options = {}) => {
const { logHelper, ...execOptions } = options;
try {
const output = execSync(`"${process.execPath}" "${RSTACK_BIN_PATH}" ${command}`, {
stdio: 'pipe',
...execOptions,
env: {
...process.env,
...execOptions.env,
},
});
addLog(logHelper, output);
return output.toString();
} catch (error) {
const execCliError = error as ExecCliError;
addLog(logHelper, execCliError.stdout);
addLog(logHelper, execCliError.stderr);
throw error;
}
};