spawn.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2019-2024 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 = (cmd, params, cwd, onClose) => {
  12. log(`Running "${cmd} ${params.join(' ')}"`)
  13. log()
  14. // TODO: move to execa?
  15. const runner = crossSpawn(cmd, params, {
  16. stdio: 'inherit',
  17. cwd,
  18. env: process.env
  19. })
  20. runner.on('close', (code) => {
  21. log()
  22. if (code) {
  23. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
  24. log(`Command "${cmd}" failed with exit code: ${code}`)
  25. }
  26. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  27. onClose && onClose(code || 0, runner.pid || 0)
  28. })
  29. return runner.pid || 0
  30. }
  31. /*
  32. Returns nothing, takes onFail
  33. */
  34. module.exports.spawnSync = (cmd, params, cwd, onFail) => {
  35. log(`[sync] Running "${cmd} ${params.join(' ')}"`)
  36. log()
  37. const runner = crossSpawn.sync(cmd, params, {
  38. stdio: 'inherit',
  39. cwd
  40. })
  41. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  42. if (runner.status || runner.error) {
  43. warn()
  44. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
  45. warn(`⚠️ Command "${cmd}" failed with exit code: ${runner.status}`)
  46. if (runner.status === null) {
  47. warn(`⚠️ Please globally install "${cmd}"`)
  48. }
  49. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  50. onFail && onFail()
  51. process.exit(1)
  52. }
  53. }