Skip to content

Instantly share code, notes, and snippets.

@s-shin
Created December 22, 2015 11:17
Show Gist options
  • Save s-shin/5371669c2339cefefa47 to your computer and use it in GitHub Desktop.
Save s-shin/5371669c2339cefefa47 to your computer and use it in GitHub Desktop.
手抜きspawn wrapper
{spawn} = require "child_process"
runCommand = (cmd, args, opts) -> new Promise (resolve, reject) ->
p = spawn cmd, args, opts
buf = []
p.stdout.on "data", (data) -> buf.push data
p.stderr.on "data", (data) -> buf.push data
p.on "error", reject
p.on "close", (code) ->
result = buf.join("").trim()
if code then reject result else resolve result
# パイプとか使いたい場合こちらが便利
runCommandViaShell = (cmdStr, opts) -> runCommand "sh", ["-c", cmdStr], opts
module.exports = {
runCommand
runCommandViaShell
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment