Spaces:
Sleeping
Sleeping
File size: 1,991 Bytes
bbbc03f 39fe4a7 bbbc03f 39fe4a7 bbbc03f | 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 79 80 81 82 83 84 85 86 87 | import { spawn } from 'node:child_process'
import { config } from './config'
interface CommandOptions {
cwd?: string
env?: NodeJS.ProcessEnv
timeoutMs?: number
input?: string
}
interface CommandResult {
stdout: string
stderr: string
code: number | null
}
export function runCommand(
command: string,
args: string[],
options: CommandOptions = {}
): Promise<CommandResult> {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd: options.cwd,
env: options.env,
shell: false
})
let stdout = ''
let stderr = ''
let timeoutId: NodeJS.Timeout | undefined
if (options.timeoutMs) {
timeoutId = setTimeout(() => {
child.kill('SIGKILL')
}, options.timeoutMs)
}
child.stdout.on('data', (data) => {
stdout += data.toString()
})
child.stderr.on('data', (data) => {
stderr += data.toString()
})
child.on('error', (error) => {
if (timeoutId) clearTimeout(timeoutId)
reject(error)
})
child.on('close', (code) => {
if (timeoutId) clearTimeout(timeoutId)
if (code === 0) {
resolve({ stdout, stderr, code })
return
}
const error = new Error(
`Command failed (${command} ${args.join(' ')}): ${stderr || stdout}`
)
;(error as any).stdout = stdout
;(error as any).stderr = stderr
;(error as any).code = code
reject(error)
})
if (options.input) {
child.stdin.write(options.input)
child.stdin.end()
}
})
}
export function runOpenClaw(args: string[], options: CommandOptions = {}) {
return runCommand(config.openclawBin, args, {
...options,
cwd: options.cwd || config.openclawStateDir || process.cwd()
})
}
export function runClawdbot(args: string[], options: CommandOptions = {}) {
return runCommand(config.clawdbotBin, args, {
...options,
cwd: options.cwd || config.openclawStateDir || process.cwd()
})
}
|