PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/front/node_modules/bower/lib/util/cmd.js

https://gitlab.com/boxnia/NFU_MOVIL
JavaScript | 120 lines | 77 code | 22 blank | 21 comment | 6 complexity | 757840d60a67af4099cfc1b514d74e66 MD5 | raw file
  1. var cp = require('child_process');
  2. var path = require('path');
  3. var Q = require('q');
  4. var mout = require('mout');
  5. var which = require('which');
  6. var PThrottler = require('p-throttler');
  7. var createError = require('./createError');
  8. // The concurrency limit here is kind of magic. You don't really gain a lot from
  9. // having a large number of commands spawned at once, so it isn't super
  10. // important for this number to be large. Reports have shown that much more than 5
  11. // or 10 cause issues for corporate networks, private repos or situations where
  12. // internet bandwidth is limited. We're running with a concurrency of 5 until
  13. // 1.4.X is released, at which time we'll move to what was discussed in #1262
  14. // https://github.com/bower/bower/pull/1262
  15. var throttler = new PThrottler(5);
  16. var winBatchExtensions;
  17. var winWhichCache;
  18. var isWin = process.platform === 'win32';
  19. if (isWin) {
  20. winBatchExtensions = ['.bat', '.cmd'];
  21. winWhichCache = {};
  22. }
  23. function getWindowsCommand(command) {
  24. var fullCommand;
  25. var extension;
  26. // Do we got the value converted in the cache?
  27. if (mout.object.hasOwn(winWhichCache, command)) {
  28. return winWhichCache[command];
  29. }
  30. // Use which to retrieve the full command, which puts the extension in the end
  31. try {
  32. fullCommand = which.sync(command);
  33. } catch (err) {
  34. return winWhichCache[command] = command;
  35. }
  36. extension = path.extname(fullCommand).toLowerCase();
  37. // Does it need to be converted?
  38. if (winBatchExtensions.indexOf(extension) === -1) {
  39. return winWhichCache[command] = command;
  40. }
  41. return winWhichCache[command] = fullCommand;
  42. }
  43. // Executes a shell command, buffering the stdout and stderr
  44. // If an error occurs, a meaningful error is generated
  45. // Returns a promise that gets fulfilled if the command succeeds
  46. // or rejected if it fails
  47. function executeCmd(command, args, options) {
  48. var process;
  49. var stderr = '';
  50. var stdout = '';
  51. var deferred = Q.defer();
  52. // Windows workaround for .bat and .cmd files, see #626
  53. if (isWin) {
  54. command = getWindowsCommand(command);
  55. }
  56. // Buffer output, reporting progress
  57. process = cp.spawn(command, args, options);
  58. process.stdout.on('data', function (data) {
  59. data = data.toString();
  60. deferred.notify(data);
  61. stdout += data;
  62. });
  63. process.stderr.on('data', function (data) {
  64. data = data.toString();
  65. deferred.notify(data);
  66. stderr += data;
  67. });
  68. // If there is an error spawning the command, reject the promise
  69. process.on('error', function (error) {
  70. return deferred.reject(error);
  71. });
  72. // Listen to the close event instead of exit
  73. // They are similar but close ensures that streams are flushed
  74. process.on('close', function (code) {
  75. var fullCommand;
  76. var error;
  77. if (code) {
  78. // Generate the full command to be presented in the error message
  79. if (!Array.isArray(args)) {
  80. args = [];
  81. }
  82. fullCommand = command;
  83. fullCommand += args.length ? ' ' + args.join(' ') : '';
  84. // Build the error instance
  85. error = createError('Failed to execute "' + fullCommand + '", exit code of #' + code + '\n' + stderr, 'ECMDERR', {
  86. details: stderr,
  87. exitCode: code
  88. });
  89. return deferred.reject(error);
  90. }
  91. return deferred.resolve([stdout, stderr]);
  92. });
  93. return deferred.promise;
  94. }
  95. function cmd(command, args, options) {
  96. return throttler.enqueue(executeCmd.bind(null, command, args, options));
  97. }
  98. module.exports = cmd;