Skip to content

Commit

Permalink
refactor(command-utils): make error handling nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
matyasf authored and HerrTopi committed Aug 18, 2023
1 parent a9f5d01 commit ade4563
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/command-utils/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ function info(...args) {
}

function warn(...args) {
console.warn(chalk.yellow('⚠️ ', ...args))
console.warn(chalk.yellow('⚠️ ', ...args))
}

function error(...args) {
console.error(chalk.red('⚠️ ', ...args))
console.error(chalk.red('⚠️ ', ...args))
}

class Command {
Expand Down Expand Up @@ -113,7 +113,7 @@ function runCommandSync(bin, args = [], envVars = {}, opts = {}) {
* @param args arguments in an array
* @param envVars environment vars as key-value pairs
* @param opts Options to `childProcess.spawn`
* @returns {Promise<{stdout: string, stderr: string}>}
* @returns {Promise<{stdout: string, stderr: string, code:number}>}
*/
async function runCommandAsync(bin, args = [], envVars = {}, opts = {}) {
const result = crossSpawn.spawn(bin, args, {
Expand All @@ -135,14 +135,18 @@ async function runCommandAsync(bin, args = [], envVars = {}, opts = {}) {
})
}
const promise = new Promise((resolve, reject) => {
result.on('error', reject)

result.on('error', (err) => {
error(`runCommandAsync ${bin} ${args} error:\n${err}`)
reject({ stdout: stdout, stderr: stderr, code: err.errno })
})
result.on('close', (code) => {
if (code === 0) {
resolve({ stdout: stdout, stderr: stderr })
resolve({ stdout: stdout, stderr: stderr, code: code })
} else {
error(`child process ${bin} ${args} exited with code ${code}`)
reject({ stdout: stdout, stderr: stderr })
error(
`runCommandAsync: child process ${bin} ${args} exited with code ${code}`
)
reject({ stdout: stdout, stderr: stderr, code: code })
}
})
})
Expand Down

0 comments on commit ade4563

Please sign in to comment.