bin.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. var spawn = require('child_process').spawn;
  3. function stripStderr(stderr) {
  4. if (!stderr) return;
  5. stderr = stderr.trim();
  6. // Strip bogus screen size error.
  7. // See https://github.com/microsoft/vscode/issues/98590
  8. var regex = /your \d+x\d+ screen size is bogus\. expect trouble/gi;
  9. stderr = stderr.replace(regex, '');
  10. return stderr.trim();
  11. }
  12. /**
  13. * Spawn a binary and read its stdout.
  14. * @param {String} cmd The name of the binary to spawn.
  15. * @param {String[]} args The arguments for the binary.
  16. * @param {Object} [options] Optional option for the spawn function.
  17. * @param {Function} done(err, stdout)
  18. */
  19. function run(cmd, args, options, done) {
  20. if (typeof options === 'function') {
  21. done = options;
  22. options = undefined;
  23. }
  24. var executed = false;
  25. var ch = spawn(cmd, args, options);
  26. var stdout = '';
  27. var stderr = '';
  28. ch.stdout.on('data', function(d) {
  29. stdout += d.toString();
  30. });
  31. ch.stderr.on('data', function(d) {
  32. stderr += d.toString();
  33. });
  34. ch.on('error', function(err) {
  35. if (executed) return;
  36. executed = true;
  37. done(new Error(err));
  38. });
  39. ch.on('close', function(code) {
  40. if (executed) return;
  41. executed = true;
  42. stderr = stripStderr(stderr);
  43. if (stderr) {
  44. return done(new Error(stderr));
  45. }
  46. done(null, stdout, code);
  47. });
  48. }
  49. module.exports = run;