Skip to content

Instantly share code, notes, and snippets.

@yukin01
Last active December 14, 2019 16:22
Show Gist options
  • Save yukin01/9fed805f499503e0c2042b0122872fce to your computer and use it in GitHub Desktop.
Save yukin01/9fed805f499503e0c2042b0122872fce to your computer and use it in GitHub Desktop.
Promisify child_process.spawn
const childProcess = require("child_process");
const spawn = (command, args) => {
return new Promise((resolve, reject) => {
const p = Array.isArray(args)
? childProcess.spawn(command, args, { stdio: "inherit" })
: childProcess.spawn(command, { stdio: "inherit" });
p.on("close", code => resolve(code));
p.on("error", err => reject(err));
});
};
(async () => {
await spawn("echo", ["Hello world!"]);
await spawn("ls");
})();
@yukin01
Copy link
Author

yukin01 commented Dec 14, 2019

{ stdio: "inherit" } をつけると親プロセスの stdio を引き継ぐので $ node spawn.js としたときにわざわざ console.logconsole.error を書かなくても stdout と stderr に出力してくれる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment