mirror of
https://github.com/hanzoai/gui.git
synced 2026-10-25 02:31:41 +00:00
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import * as proc from 'node:child_process'
|
|
|
|
export const spawn = proc.spawn
|
|
|
|
// could add only if changed checks: git diff --quiet HEAD HEAD~3 -- ./packages/core
|
|
// but at that point would be nicer to get a whole setup for this.. lerna or whatever
|
|
export const spawnify = async (
|
|
cmd: string,
|
|
opts?: proc.SpawnOptionsWithoutStdio & {
|
|
avoidLog?: boolean
|
|
interactive?: boolean
|
|
cwd?: string
|
|
}
|
|
): Promise<string> => {
|
|
console.info('>', cmd)
|
|
const [head, ...rest] = cmd.split(' ')
|
|
return new Promise((res, rej) => {
|
|
const { avoidLog, interactive, cwd, ...spawnOpts } = opts || {}
|
|
const child = spawn(head, rest, {
|
|
stdio: interactive ? 'inherit' : avoidLog ? undefined : ['inherit', 'pipe', 'pipe'],
|
|
cwd,
|
|
shell: true,
|
|
...spawnOpts,
|
|
})
|
|
const outStr = []
|
|
const errStr = []
|
|
if (!avoidLog && !interactive) {
|
|
child.stdout?.pipe(process.stdout)
|
|
child.stderr?.pipe(process.stderr)
|
|
}
|
|
child.stdout?.on('data', (out) => {
|
|
// @ts-ignore
|
|
outStr.push(`${out}`)
|
|
})
|
|
child.stderr?.on('data', (out) => {
|
|
// @ts-ignore
|
|
errStr.push(`${out}`)
|
|
})
|
|
child.on('error', (err) => {
|
|
rej(err)
|
|
})
|
|
child.on('close', (code) => {
|
|
if (code === 0) {
|
|
res(outStr.join('\n'))
|
|
} else {
|
|
rej(errStr.join('\n'))
|
|
}
|
|
})
|
|
})
|
|
}
|