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.

40 lines
876 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. childProgress.stdout.on('data' data => {
  10. options.onSuccess && options.onSuccess(data)
  11. })
  12. childProgress.stderr.on('data', data => {
  13. options.onError && options.onError(data)
  14. })
  15. childProgress.on('close', code => {
  16. options.onClose && options.onClose(code)
  17. })
  18. return childProgress.pid
  19. }
  20. function kill(pid) {
  21. const childProgress = pidMap[pid]
  22. let status = false
  23. if (childProgress && !childProgress.killed) {
  24. status = childProgress.kill()
  25. }
  26. childProgress = null
  27. delete pidMap[pid]
  28. return status
  29. }
  30. module.exports = {
  31. start,
  32. kill,
  33. }