shell.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const { spawn, execSync } = require("child_process");
  2. const runCommand = (command, args) =>
  3. new Promise((resolve, reject) => {
  4. const child = spawn(command, args, { stdio: "inherit" });
  5. child.on("error", (error) => reject(new Error(`Error: ${error.message}`)));
  6. child.on("exit", (code) =>
  7. code === 0
  8. ? resolve()
  9. : reject(new Error(`Command failed with exit code ${code}`))
  10. // kill installer on error?
  11. // process.exit(code)
  12. );
  13. });
  14. const $ = (command) =>
  15. console.log(`>${command}`) ||
  16. runCommand(command.split(" ")[0], command.split(" ").slice(1));
  17. exports.$ = $;
  18. const $$ = (command) =>
  19. console.log(`>${command.join(" ")}`) ||
  20. runCommand(command[0], command.slice(1));
  21. exports.$$ = $$;
  22. function $sh(cmd) {
  23. try {
  24. console.log(`>${cmd}`);
  25. return execSync(cmd, { stdio: "inherit" });
  26. } catch (error) {
  27. try {
  28. console.error(error.stderr.toString().trim(), error.status);
  29. } catch (e) {
  30. console.error(error);
  31. }
  32. throw error;
  33. }
  34. }
  35. exports.$sh = $sh;