-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathexec.ts
50 lines (41 loc) · 1.67 KB
/
exec.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
import { Result } from '@sapphire/result';
import { execa, type ExecaError, type Options } from 'execa';
import { normalizeExecutablePath } from './hooks/runtimes/utils.js';
import { error, run } from './outputs.js';
import { cliDebugPrint } from './utils/cliDebugPrint.js';
const spawnPromised = async (cmd: string, args: string[], opts: Options) => {
const escapedCommand = normalizeExecutablePath(cmd);
cliDebugPrint('spawnPromised2', { escapedCommand, args, opts });
const childProcess = execa(escapedCommand, args, {
shell: true,
windowsHide: true,
env: opts.env,
cwd: opts.cwd,
// Pipe means it gets collected by the parent process, inherit means it gets collected by the parent process and printed out to the console
stdout: process.env.APIFY_NO_LOGS_IN_TESTS ? ['pipe'] : ['pipe', 'inherit'],
stderr: process.env.APIFY_NO_LOGS_IN_TESTS ? ['pipe'] : ['pipe', 'inherit'],
verbose: process.env.APIFY_CLI_DEBUG ? 'full' : undefined,
});
return Result.fromAsync(
childProcess.catch((execaError: ExecaError) => {
throw new Error(`${cmd} exited with code ${execaError.exitCode}`, { cause: execaError });
}),
) as Promise<Result<Awaited<typeof childProcess>, Error & { cause: ExecaError }>>;
};
export interface ExecWithLogOptions {
cmd: string;
args?: string[];
opts?: Options;
overrideCommand?: string;
}
export async function execWithLog({ cmd, args = [], opts = {}, overrideCommand }: ExecWithLogOptions) {
run({ message: `${overrideCommand || cmd} ${args.join(' ')}` });
const result = await spawnPromised(cmd, args, opts);
if (result.isErr()) {
const err = result.unwrapErr();
error({ message: err.message });
if (err.cause) {
throw err.cause;
}
}
}