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.

120 lines
4.0 KiB

4 years ago
  1. 'use strict';
  2. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  3. var _crossSpawn = require('cross-spawn');
  4. var _command = require('./command');
  5. var _command2 = _interopRequireDefault(_command);
  6. var _variable = require('./variable');
  7. var _variable2 = _interopRequireDefault(_variable);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. module.exports = crossEnv;
  10. var envSetterRegex = /(\w+)=('(.*)'|"(.*)"|(.*))/;
  11. function crossEnv(args) {
  12. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  13. var _parseCommand = parseCommand(args),
  14. _parseCommand2 = _slicedToArray(_parseCommand, 3),
  15. envSetters = _parseCommand2[0],
  16. command = _parseCommand2[1],
  17. commandArgs = _parseCommand2[2];
  18. var env = getEnvVars(envSetters);
  19. if (command) {
  20. var proc = (0, _crossSpawn.spawn)(
  21. // run `path.normalize` for command(on windows)
  22. (0, _command2.default)(command, env, true),
  23. // by default normalize is `false`, so not run for cmd args
  24. commandArgs.map(function (arg) {
  25. return (0, _command2.default)(arg, env);
  26. }), {
  27. stdio: 'inherit',
  28. shell: options.shell,
  29. env
  30. });
  31. process.on('SIGTERM', function () {
  32. return proc.kill('SIGTERM');
  33. });
  34. process.on('SIGINT', function () {
  35. return proc.kill('SIGINT');
  36. });
  37. process.on('SIGBREAK', function () {
  38. return proc.kill('SIGBREAK');
  39. });
  40. process.on('SIGHUP', function () {
  41. return proc.kill('SIGHUP');
  42. });
  43. proc.on('exit', function (code, signal) {
  44. var crossEnvExitCode = code;
  45. // exit code could be null when OS kills the process(out of memory, etc) or due to node handling it
  46. // but if the signal is SIGINT the user exited the process so we want exit code 0
  47. if (crossEnvExitCode === null) {
  48. crossEnvExitCode = signal === 'SIGINT' ? 0 : 1;
  49. }
  50. process.exit(crossEnvExitCode); //eslint-disable-line no-process-exit
  51. });
  52. return proc;
  53. }
  54. return null;
  55. }
  56. function parseCommand(args) {
  57. var envSetters = {};
  58. var command = null;
  59. var commandArgs = [];
  60. for (var i = 0; i < args.length; i++) {
  61. var match = envSetterRegex.exec(args[i]);
  62. if (match) {
  63. var value = void 0;
  64. if (typeof match[3] !== 'undefined') {
  65. value = match[3];
  66. } else if (typeof match[4] === 'undefined') {
  67. value = match[5];
  68. } else {
  69. value = match[4];
  70. }
  71. envSetters[match[1]] = value;
  72. } else {
  73. // No more env setters, the rest of the line must be the command and args
  74. var cStart = [];
  75. cStart = args.slice(i)
  76. // Regex:
  77. // match "\'" or "'"
  78. // or match "\" if followed by [$"\] (lookahead)
  79. .map(function (a) {
  80. var re = /\\\\|(\\)?'|([\\])(?=[$"\\])/g;
  81. // Eliminate all matches except for "\'" => "'"
  82. return a.replace(re, function (m) {
  83. if (m === '\\\\') return '\\';
  84. if (m === "\\'") return "'";
  85. return '';
  86. });
  87. });
  88. command = cStart[0];
  89. commandArgs = cStart.slice(1);
  90. break;
  91. }
  92. }
  93. return [envSetters, command, commandArgs];
  94. }
  95. function getEnvVars(envSetters) {
  96. var envVars = Object.assign({}, process.env);
  97. if (process.env.APPDATA) {
  98. envVars.APPDATA = process.env.APPDATA;
  99. }
  100. Object.keys(envSetters).forEach(function (varName) {
  101. envVars[varName] = (0, _variable2.default)(envSetters[varName], varName);
  102. });
  103. return envVars;
  104. }