index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. var cp = require('child_process');
  3. var parse = require('./lib/parse');
  4. var enoent = require('./lib/enoent');
  5. var cpSpawnSync = cp.spawnSync;
  6. function spawn(command, args, options) {
  7. var parsed;
  8. var spawned;
  9. // Parse the arguments
  10. parsed = parse(command, args, options);
  11. // Spawn the child process
  12. spawned = cp.spawn(parsed.command, parsed.args, parsed.options);
  13. // Hook into child process "exit" event to emit an error if the command
  14. // does not exists, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
  15. enoent.hookChildProcess(spawned, parsed);
  16. return spawned;
  17. }
  18. function spawnSync(command, args, options) {
  19. var parsed;
  20. var result;
  21. if (!cpSpawnSync) {
  22. try {
  23. cpSpawnSync = require('spawn-sync'); // eslint-disable-line global-require
  24. } catch (ex) {
  25. throw new Error(
  26. 'In order to use spawnSync on node 0.10 or older, you must ' +
  27. 'install spawn-sync:\n\n' +
  28. ' npm install spawn-sync --save'
  29. );
  30. }
  31. }
  32. // Parse the arguments
  33. parsed = parse(command, args, options);
  34. // Spawn the child process
  35. result = cpSpawnSync(parsed.command, parsed.args, parsed.options);
  36. // Analyze if the command does not exists, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
  37. result.error = result.error || enoent.verifyENOENTSync(result.status, parsed);
  38. return result;
  39. }
  40. module.exports = spawn;
  41. module.exports.spawn = spawn;
  42. module.exports.sync = spawnSync;
  43. module.exports._parse = parse;
  44. module.exports._enoent = enoent;