make-spawn-args.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* eslint camelcase: "off" */
  2. const isWindows = require('./is-windows.js')
  3. const setPATH = require('./set-path.js')
  4. const {resolve} = require('path')
  5. const npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
  6. const makeSpawnArgs = options => {
  7. const {
  8. event,
  9. path,
  10. scriptShell = isWindows ? process.env.ComSpec || 'cmd' : 'sh',
  11. env = {},
  12. stdio,
  13. cmd,
  14. stdioString = false,
  15. } = options
  16. const isCmd = /(?:^|\\)cmd(?:\.exe)?$/i.test(scriptShell)
  17. const args = isCmd ? ['/d', '/s', '/c', cmd] : ['-c', cmd]
  18. const spawnOpts = {
  19. env: setPATH(path, {
  20. // we need to at least save the PATH environment var
  21. ...process.env,
  22. ...env,
  23. npm_package_json: resolve(path, 'package.json'),
  24. npm_lifecycle_event: event,
  25. npm_lifecycle_script: cmd,
  26. npm_config_node_gyp,
  27. }),
  28. stdioString,
  29. stdio,
  30. cwd: path,
  31. ...(isCmd ? { windowsVerbatimArguments: true } : {}),
  32. }
  33. return [scriptShell, args, spawnOpts]
  34. }
  35. module.exports = makeSpawnArgs