index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const child_process_1 = require("child_process");
  7. const path_1 = __importDefault(require("path"));
  8. const common_1 = require("./common");
  9. const loggerAlias = common_1.logger;
  10. function runSync(command, options) {
  11. try {
  12. const nextOptions = {
  13. cwd: options.cwd,
  14. env: options.env,
  15. stdio: options.stdio,
  16. timeout: options.timeout
  17. };
  18. const buffer = child_process_1.execSync(command, nextOptions);
  19. if (buffer) {
  20. return buffer.toString();
  21. }
  22. return null;
  23. }
  24. catch (error) {
  25. throw new common_1.RunJSError(error.message);
  26. }
  27. }
  28. function runAsync(command, options) {
  29. return new Promise((resolve, reject) => {
  30. const nextOptions = {
  31. cwd: options.cwd,
  32. env: options.env,
  33. shell: true,
  34. stdio: options.stdio
  35. };
  36. const asyncProcess = child_process_1.spawn(command, nextOptions);
  37. let output = null;
  38. asyncProcess.on("error", (error) => {
  39. reject(new Error(`Failed to start command: ${command}; ${error.toString()}`));
  40. });
  41. asyncProcess.on("close", (exitCode) => {
  42. if (exitCode === 0) {
  43. resolve(output);
  44. }
  45. else {
  46. reject(new Error(`Command failed: ${command} with exit code ${exitCode}`));
  47. }
  48. });
  49. if (options.stdio === "pipe") {
  50. asyncProcess.stdout.on("data", (buffer) => {
  51. output = buffer.toString();
  52. });
  53. }
  54. if (options.timeout) {
  55. setTimeout(() => {
  56. asyncProcess.kill();
  57. reject(new Error(`Command timeout: ${command}`));
  58. }, options.timeout);
  59. }
  60. });
  61. }
  62. function run(command, options = {}, logger = loggerAlias) {
  63. const binPath = path_1.default.resolve("./node_modules/.bin");
  64. // Pick relevant option keys and set default values
  65. const nextOptions = {
  66. async: !!options.async,
  67. cwd: options.cwd,
  68. env: options.env || process.env,
  69. stdio: options.stdio || "inherit",
  70. timeout: options.timeout
  71. };
  72. const env = nextOptions.env;
  73. // Include in PATH node_modules bin path
  74. if (env) {
  75. env.PATH = [binPath, env.PATH || process.env.PATH].join(path_1.default.delimiter);
  76. }
  77. logger.title(command);
  78. // Handle async call
  79. if (options.async) {
  80. return runAsync(command, nextOptions);
  81. }
  82. // Handle sync call by default
  83. return runSync(command, nextOptions);
  84. }
  85. exports.run = run;
  86. /**
  87. * @deprecated
  88. */
  89. function option(thisObj, name = "") {
  90. return (thisObj && thisObj.options && thisObj.options[name]) || null;
  91. }
  92. exports.option = option;
  93. function options(thisObj) {
  94. return (thisObj && thisObj.options) || {};
  95. }
  96. exports.options = options;
  97. function help(func, annotation) {
  98. // Because the validation above currently gets compiled out,
  99. // Explictly validate the function input
  100. if (typeof func === "function") {
  101. func.help = annotation;
  102. }
  103. else {
  104. throw new Error("first help() argument must be a function");
  105. }
  106. }
  107. exports.help = help;
  108. //# sourceMappingURL=index.js.map