You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shell.js 800B

1234567891011121314151617181920212223242526272829303132
  1. var spawn = require("child_process").spawn;
  2. module.exports = shell;
  3. module.exports.safe = safe;
  4. function shell(cmd, ...args) {
  5. console.log("running shell '"+cmd+" "+args.join(" ")+"'");
  6. return new Promise((resolve, reject) => {
  7. var child = spawn(cmd, args);
  8. function print(out, name) {
  9. return function(d) {
  10. var s = d.toString();
  11. if (s.trim() === "")
  12. return;
  13. out.write(cmd+": "+name+": "+s);
  14. }
  15. }
  16. child.stderr.on("data", print(process.stderr, "err"));
  17. child.stdout.on("data", print(process.stdout, "out"));
  18. child.on("error", reject);
  19. child.on("exit", resolve);
  20. });
  21. }
  22. async function safe(...args) {
  23. var code = await shell.apply(null, args);
  24. if (code !== 0 && code !== null)
  25. throw new Error("Command "+args[0]+" failed, exit code "+code);
  26. return code;
  27. }