2021-04-11 01:09:09 +03:00
|
|
|
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-02-10 22:45:41 +03:00
|
|
|
const crossSpawn = require('cross-spawn')
|
|
|
|
const logger = require('./logger')
|
2019-12-24 15:40:03 +03:00
|
|
|
|
2019-11-16 21:51:46 +03:00
|
|
|
const log = logger('app:spawn')
|
2022-02-10 22:45:41 +03:00
|
|
|
const warn = logger('app:spawn')
|
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
|
|
|
*/
|
2022-02-10 22:45:41 +03:00
|
|
|
module.exports.spawn = (
|
|
|
|
cmd,
|
|
|
|
params,
|
|
|
|
cwd,
|
|
|
|
onClose
|
|
|
|
) => {
|
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',
|
2020-03-07 19:40:24 +03:00
|
|
|
cwd,
|
|
|
|
env: process.env
|
2019-12-24 15:40:03 +03:00
|
|
|
})
|
2019-08-20 00:09:29 +03:00
|
|
|
|
2020-08-19 05:36:46 +03:00
|
|
|
runner.on('close', (code) => {
|
2019-08-20 00:09:29 +03:00
|
|
|
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-03-09 23:57:27 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
|
2022-02-10 22:45:41 +03:00
|
|
|
onClose && onClose(code || 0, runner.pid || 0)
|
2019-08-20 00:09:29 +03:00
|
|
|
})
|
|
|
|
|
2022-02-10 22:45:41 +03:00
|
|
|
return runner.pid || 0
|
2019-08-20 00:09:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-12-24 15:40:03 +03:00
|
|
|
Returns nothing, takes onFail
|
2019-08-20 00:09:29 +03:00
|
|
|
*/
|
2022-02-10 22:45:41 +03:00
|
|
|
module.exports.spawnSync = (
|
|
|
|
cmd,
|
|
|
|
params,
|
|
|
|
cwd,
|
|
|
|
onFail
|
|
|
|
) => {
|
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-03-09 23:57:27 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
|
|
|
|
onFail && onFail()
|
2019-08-20 00:09:29 +03:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|