terminate.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const util = require('util')
  2. const cp = require('child_process')
  3. const path = require('path')
  4. const {
  5. isWindows,
  6. isLinux,
  7. isMacintosh
  8. } = require('@vue/cli-shared-utils')
  9. const execFile = util.promisify(cp.execFile)
  10. const spawn = util.promisify(cp.spawn)
  11. exports.terminate = async function (childProcess, cwd) {
  12. if (isWindows) {
  13. try {
  14. const options = {
  15. stdio: ['pipe', 'pipe', 'ignore']
  16. }
  17. if (cwd) {
  18. options.cwd = cwd
  19. }
  20. await execFile('taskkill', ['/T', '/F', '/PID', childProcess.pid.toString()], options)
  21. } catch (err) {
  22. return { success: false, error: err }
  23. }
  24. } else if (isLinux || isMacintosh) {
  25. try {
  26. const cmd = path.resolve(__dirname, './terminate.sh')
  27. const result = await spawn(cmd, [childProcess.pid.toString()], {
  28. cwd
  29. })
  30. if (result.error) {
  31. return { success: false, error: result.error }
  32. }
  33. } catch (err) {
  34. return { success: false, error: err }
  35. }
  36. } else {
  37. childProcess.kill('SIGKILL')
  38. }
  39. return { success: true }
  40. }