Skip to content

Commit

Permalink
refactor(command-utils): fix runCommandAsync
Browse files Browse the repository at this point in the history
We treated it as something that returns a Promise when it does not. This fixes it by wrapping it in
a Promise
TEST PLAN:
Change some command temporarly (e.g. ui-scripts tag) so it uses RunCommandAsync and output its
return value
  • Loading branch information
matyasf authored and HerrTopi committed Aug 18, 2023
1 parent 7e16212 commit 29f0771
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
36 changes: 35 additions & 1 deletion packages/command-utils/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,48 @@ function runCommandSync(bin, args = [], envVars = {}, opts = {}) {
return result
}

/**
* Runs the given command asynchronously, returns a Promise that contains its
* stdout and stderr as strings
* @param bin path to the binary
* @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}>}
*/
async function runCommandAsync(bin, args = [], envVars = {}, opts = {}) {
const result = crossSpawn.spawn(bin, args, {
env: { ...process.env, ...envVars },
stdio: 'inherit',
windowsHide: true,
...opts
})
return result
let stdout = ''
let stderr = ''
if (result.stdout) {
result.stdout.on('data', (data) => {
stdout += data.toString()
})
}
if (result.stderr) {
result.stderr.on('data', (data) => {
stderr += data.toString()
})
}
const promise = new Promise((resolve, reject) => {
result.on('error', reject)

result.on('close', (code) => {
if (code === 0) {
resolve({ stdout: stdout, stderr: stderr })
} else {
error(`child process ${bin} ${args} exited with code ${code}`)
reject({ stdout: stdout, stderr: stderr })
}
})
})
promise.child = result
return promise
}

function resolveBin(
Expand Down
3 changes: 2 additions & 1 deletion packages/command-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"bugs": "https:/instructure/instructure-ui/issues",
"scripts": {
"lint": "run -T ui-scripts lint",
"lint:fix": "run -T ui-scripts lint --fix"
"lint:fix": "run -T ui-scripts lint --fix",
"build:types": "run -T tsc -p tsconfig.build.json"
},
"license": "MIT",
"dependencies": {
Expand Down

0 comments on commit 29f0771

Please sign in to comment.