index.js 2.8 KB

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