|
1 | | -import { exec as nodeExec, execFile as nodeExecFile } from 'node:child_process' |
2 | | -import { promisify } from 'node:util' |
| 1 | +import { type SpawnOptions, spawn } from 'node:child_process' |
3 | 2 |
|
4 | | -const execAsync = promisify(nodeExec) |
5 | | -const execFileAsync = promisify(nodeExecFile) |
| 3 | +function run(command: string, args: string[], options: SpawnOptions): Promise<void> { |
| 4 | + return new Promise((resolve, reject) => { |
| 5 | + const child = spawn(command, args, { |
| 6 | + ...options, |
| 7 | + stdio: ['ignore', 'ignore', 'pipe'], |
| 8 | + }) |
| 9 | + |
| 10 | + let stderr = '' |
| 11 | + child.stderr?.on('data', (data: Buffer) => { |
| 12 | + stderr += data.toString() |
| 13 | + }) |
6 | 14 |
|
7 | | -export async function exec(command: string, options: { cwd?: string } = {}): Promise<string> { |
8 | | - try { |
9 | | - const { stdout } = await execAsync(command, { |
10 | | - cwd: options.cwd, |
| 15 | + child.on('close', (code) => { |
| 16 | + if (code === 0) { |
| 17 | + resolve() |
| 18 | + } else { |
| 19 | + const message = stderr.trim() || `Command failed with exit code ${code}` |
| 20 | + reject(new Error(message)) |
| 21 | + } |
11 | 22 | }) |
12 | 23 |
|
13 | | - return stdout.trim() |
14 | | - } catch (error: unknown) { |
15 | | - const execError = error as { stderr?: string; message?: string } |
16 | | - const message = execError.stderr?.trim() || execError.message || 'Unknown error' |
17 | | - throw new Error(message) |
18 | | - } |
| 24 | + child.on('error', reject) |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +export async function exec(command: string, options: { cwd?: string } = {}): Promise<void> { |
| 29 | + await run('/bin/sh', ['-c', command], { cwd: options.cwd }) |
19 | 30 | } |
20 | 31 |
|
21 | 32 | export async function execFile( |
22 | 33 | file: string, |
23 | 34 | args: string[], |
24 | 35 | options: { cwd?: string } = {}, |
25 | | -): Promise<string> { |
26 | | - try { |
27 | | - const { stdout } = await execFileAsync(file, args, { |
28 | | - cwd: options.cwd, |
29 | | - }) |
30 | | - |
31 | | - return stdout.trim() |
32 | | - } catch (error: unknown) { |
33 | | - const execError = error as { stderr?: string; message?: string } |
34 | | - const message = execError.stderr?.trim() || execError.message || 'Unknown error' |
35 | | - throw new Error(message) |
36 | | - } |
| 36 | +): Promise<void> { |
| 37 | + await run(file, args, { cwd: options.cwd }) |
37 | 38 | } |
0 commit comments