spawn.js 1017 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * Created by nuintun on 2016/1/14.
  3. */
  4. 'use strict';
  5. var spawn = require('child_process').spawn;
  6. /**
  7. * normalize exec args
  8. * @param command
  9. * @param options
  10. * @returns {{cmd: *, shell: *, args: *, options: *}}
  11. */
  12. function normalizeExecArgs(command, options){
  13. var shell, args;
  14. // Make a shallow copy before patching so we don't clobber the user's
  15. // options object.
  16. options = options || {};
  17. if (process.platform === 'win32') {
  18. shell = process.env.comspec || 'cmd.exe';
  19. args = ['/s', '/c', '"' + command + '"'];
  20. options.windowsVerbatimArguments = true;
  21. } else {
  22. shell = '/bin/sh';
  23. args = ['-c', command];
  24. }
  25. if (options.shell) {
  26. shell = options.shell;
  27. }
  28. return {
  29. shell: shell,
  30. args: args,
  31. options: options
  32. };
  33. }
  34. /**
  35. * exec thread
  36. */
  37. module.exports = function (){
  38. var parsed = normalizeExecArgs.apply(null, arguments);
  39. return spawn(parsed.shell, parsed.args, parsed.options);
  40. };