2019-12-24 15:40:03 +03:00
|
|
|
import crossSpawn from 'cross-spawn'
|
|
|
|
import logger from './logger'
|
|
|
|
|
2019-11-16 21:51:46 +03:00
|
|
|
const log = logger('app:spawn')
|
|
|
|
const warn = logger('app:spawn', 'red')
|
2019-08-20 00:09:29 +03:00
|
|
|
|
|
|
|
/*
|
2019-12-24 15:40:03 +03:00
|
|
|
Returns pid, takes onClose
|
2019-08-20 00:09:29 +03:00
|
|
|
*/
|
2019-12-24 15:40:03 +03:00
|
|
|
export const spawn = (
|
|
|
|
cmd: string,
|
|
|
|
params: string[],
|
|
|
|
cwd: string,
|
|
|
|
onClose: (code: number) => void
|
|
|
|
): number => {
|
2019-08-20 00:09:29 +03:00
|
|
|
log(`Running "${cmd} ${params.join(' ')}"`)
|
|
|
|
log()
|
|
|
|
|
2019-12-24 15:40:03 +03:00
|
|
|
// TODO: move to execa?
|
|
|
|
const runner = crossSpawn(cmd, params, {
|
|
|
|
stdio: 'inherit',
|
|
|
|
cwd
|
|
|
|
})
|
2019-08-20 00:09:29 +03:00
|
|
|
|
|
|
|
runner.on('close', code => {
|
|
|
|
log()
|
|
|
|
if (code) {
|
2020-02-08 18:17:27 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
2019-08-20 00:09:29 +03:00
|
|
|
log(`Command "${cmd}" failed with exit code: ${code}`)
|
|
|
|
}
|
|
|
|
|
2020-02-08 18:17:27 +03:00
|
|
|
onClose?.(code)
|
2019-08-20 00:09:29 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
return runner.pid
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-12-24 15:40:03 +03:00
|
|
|
Returns nothing, takes onFail
|
2019-08-20 00:09:29 +03:00
|
|
|
*/
|
2019-12-24 15:40:03 +03:00
|
|
|
export const spawnSync = (
|
|
|
|
cmd: string,
|
|
|
|
params: string[],
|
|
|
|
cwd: string,
|
|
|
|
onFail: () => void
|
|
|
|
): void => {
|
2019-08-20 00:09:29 +03:00
|
|
|
log(`[sync] Running "${cmd} ${params.join(' ')}"`)
|
|
|
|
log()
|
|
|
|
|
2019-12-24 15:40:03 +03:00
|
|
|
const runner = crossSpawn.sync(cmd, params, {
|
|
|
|
stdio: 'inherit',
|
|
|
|
cwd
|
|
|
|
})
|
2019-08-20 00:09:29 +03:00
|
|
|
|
2020-02-08 18:17:27 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
2019-08-20 00:09:29 +03:00
|
|
|
if (runner.status || runner.error) {
|
|
|
|
warn()
|
2020-02-08 18:17:27 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
2019-08-20 00:09:29 +03:00
|
|
|
warn(`⚠️ Command "${cmd}" failed with exit code: ${runner.status}`)
|
|
|
|
if (runner.status === null) {
|
|
|
|
warn(`⚠️ Please globally install "${cmd}"`)
|
|
|
|
}
|
2020-02-08 18:17:27 +03:00
|
|
|
onFail?.()
|
2019-08-20 00:09:29 +03:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|