-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace subcommandante with execa
Thanks to the great work from @JGAntunes
- Loading branch information
1 parent
8d43043
commit 7b09529
Showing
5 changed files
with
235 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict' | ||
|
||
const execa = require('execa') | ||
const once = require('once') | ||
|
||
function exec (cmd, args, opts, handlers) { | ||
opts = opts || {} | ||
let err = '' | ||
let result = '' | ||
let callback | ||
|
||
// Handy method if we just want the result and err returned in a callback | ||
if (typeof handlers === 'function') { | ||
callback = once(handlers) | ||
handlers = { | ||
error: callback, | ||
data (data) { | ||
result += data | ||
}, | ||
done () { | ||
if (err) { | ||
return callback(new Error(err)) | ||
} | ||
callback(null, result.trim()) | ||
} | ||
} | ||
} | ||
|
||
// The listeners that will actually be set on the process | ||
const listeners = { | ||
data: handlers.data, | ||
error (data) { | ||
err += data | ||
}, | ||
done (code) { | ||
if (code !== 0) { | ||
return handlers.error( | ||
new Error(`non-zero exit code ${code}\n | ||
while running: ${cmd} ${args.join(' ')}\n\n | ||
${err}`) | ||
) | ||
} | ||
if (handlers.done) { | ||
handlers.done() | ||
} | ||
} | ||
} | ||
|
||
const command = execa(cmd, args, opts) | ||
|
||
if (listeners.data) { | ||
command.stdout.on('data', listeners.data) | ||
} | ||
|
||
command.stderr.on('data', listeners.error) | ||
|
||
// If command fails to execute return directly to the handler | ||
command.on('error', handlers.error) | ||
|
||
command.on('close', listeners.done) | ||
|
||
command.catch(handlers.error) | ||
|
||
return command | ||
} | ||
|
||
module.exports = exec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.