electron launcher
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
939 B

  1. const { spawn } = require('child_process')
  2. let pidMap = {}
  3. /**
  4. * options: { onSuccess: null | function, onError: null | function, onClose: null | function }
  5. */
  6. function start(command, args, options = {}) {
  7. const childProgress = spawn(command, args)
  8. pidMap[childProgress.pid] = childProgress
  9. const promise = new Promise((resolve, reject) => {
  10. childProgress.stdout.on('data', data => {
  11. resolve(data.toString())
  12. })
  13. childProgress.stderr.on('data', data => {
  14. reject(data.toString())
  15. })
  16. })
  17. /**
  18. childProgress.on('close', code => {
  19. options.onClose && options.onClose(code)
  20. })
  21. */
  22. return { pid: childProgress.pid, promise }
  23. }
  24. function kill(pid) {
  25. const childProgress = pidMap[pid]
  26. let status = false
  27. if (childProgress && !childProgress.killed) {
  28. status = childProgress.kill()
  29. }
  30. childProgress = null
  31. delete pidMap[pid]
  32. return status
  33. }
  34. module.exports = {
  35. start,
  36. kill,
  37. }