spawn.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. const crossSpawn = require('cross-spawn')
  5. const logger = require('./logger')
  6. const log = logger('app:spawn')
  7. const warn = logger('app:spawn')
  8. /*
  9. Returns pid, takes onClose
  10. */
  11. module.exports.spawn = (
  12. cmd,
  13. params,
  14. cwd,
  15. onClose
  16. ) => {
  17. log(`Running "${cmd} ${params.join(' ')}"`)
  18. log()
  19. // TODO: move to execa?
  20. const runner = crossSpawn(cmd, params, {
  21. stdio: 'inherit',
  22. cwd,
  23. env: process.env
  24. })
  25. runner.on('close', (code) => {
  26. log()
  27. if (code) {
  28. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
  29. log(`Command "${cmd}" failed with exit code: ${code}`)
  30. }
  31. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  32. onClose && onClose(code || 0, runner.pid || 0)
  33. })
  34. return runner.pid || 0
  35. }
  36. /*
  37. Returns nothing, takes onFail
  38. */
  39. module.exports.spawnSync = (
  40. cmd,
  41. params,
  42. cwd,
  43. onFail
  44. ) => {
  45. log(`[sync] Running "${cmd} ${params.join(' ')}"`)
  46. log()
  47. const runner = crossSpawn.sync(cmd, params, {
  48. stdio: 'inherit',
  49. cwd
  50. })
  51. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  52. if (runner.status || runner.error) {
  53. warn()
  54. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
  55. warn(`⚠️ Command "${cmd}" failed with exit code: ${runner.status}`)
  56. if (runner.status === null) {
  57. warn(`⚠️ Please globally install "${cmd}"`)
  58. }
  59. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  60. onFail && onFail()
  61. process.exit(1)
  62. }
  63. }