2020-09-04 17:22:15 +09:00
|
|
|
import { spawn as _spawn, SpawnOptionsWithoutStdio } from 'child_process'
|
|
|
|
|
|
|
|
import { debugFactory } from './debug'
|
|
|
|
|
|
|
|
const debug = debugFactory('spawn')
|
|
|
|
|
|
|
|
export function spawn(
|
|
|
|
command: string,
|
|
|
|
options: SpawnOptionsWithoutStdio = {},
|
|
|
|
): Promise<Buffer> {
|
|
|
|
const [cmd, ...args] = command.split(' ').map((s) => s.trim())
|
|
|
|
debug(`execute ${cmd} ${args.join(' ')}`)
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-01-03 16:23:08 +09:00
|
|
|
const spawnStream = _spawn(cmd, args, { ...options, shell: true })
|
2020-09-04 17:22:15 +09:00
|
|
|
const chunks: Buffer[] = []
|
|
|
|
process.stdin.pipe(spawnStream.stdin)
|
|
|
|
spawnStream.stdout?.on('data', (chunk) => {
|
|
|
|
chunks.push(chunk)
|
|
|
|
})
|
|
|
|
spawnStream.stdout.pipe(process.stdout)
|
|
|
|
spawnStream.stderr.pipe(process.stderr)
|
|
|
|
spawnStream.on('close', (code) => {
|
|
|
|
if (code !== 0) {
|
|
|
|
reject()
|
|
|
|
} else {
|
|
|
|
resolve(Buffer.concat(chunks))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|