guess.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const path = require('path')
  2. const shellQuote = require('shell-quote')
  3. const childProcess = require('child_process')
  4. // Map from full process name to binary that starts the process
  5. // We can't just re-use full process name, because it will spawn a new instance
  6. // of the app every time
  7. const COMMON_EDITORS_MACOS = require('./editor-info/macos')
  8. const COMMON_EDITORS_LINUX = require('./editor-info/linux')
  9. const COMMON_EDITORS_WIN = require('./editor-info/windows')
  10. module.exports = function guessEditor (specifiedEditor) {
  11. if (specifiedEditor) {
  12. return shellQuote.parse(specifiedEditor)
  13. }
  14. if (process.env.LAUNCH_EDITOR) {
  15. return [process.env.LAUNCH_EDITOR]
  16. }
  17. if (process.versions.webcontainer) {
  18. return [process.env.EDITOR || 'code']
  19. }
  20. // We can find out which editor is currently running by:
  21. // `ps x` on macOS and Linux
  22. // `Get-Process` on Windows
  23. try {
  24. if (process.platform === 'darwin') {
  25. const output = childProcess
  26. .execSync('ps x -o comm=', {
  27. stdio: ['pipe', 'pipe', 'ignore']
  28. })
  29. .toString()
  30. const processNames = Object.keys(COMMON_EDITORS_MACOS)
  31. const processList = output.split('\n')
  32. for (let i = 0; i < processNames.length; i++) {
  33. const processName = processNames[i]
  34. // Find editor by exact match.
  35. if (processList.includes(processName)) {
  36. return [COMMON_EDITORS_MACOS[processName]]
  37. }
  38. const processNameWithoutApplications = processName.replace('/Applications', '')
  39. // Find editor installation not in /Applications.
  40. if (output.indexOf(processNameWithoutApplications) !== -1) {
  41. // Use the CLI command if one is specified
  42. if (processName !== COMMON_EDITORS_MACOS[processName]) {
  43. return [COMMON_EDITORS_MACOS[processName]]
  44. }
  45. // Use a partial match to find the running process path. If one is found, use the
  46. // existing path since it can be running from anywhere.
  47. const runningProcess = processList.find((procName) => procName.endsWith(processNameWithoutApplications))
  48. if (runningProcess !== undefined) {
  49. return [runningProcess]
  50. }
  51. }
  52. }
  53. } else if (process.platform === 'win32') {
  54. const output = childProcess
  55. .execSync(
  56. 'powershell -NoProfile -Command "Get-CimInstance -Query \\"select executablepath from win32_process where executablepath is not null\\" | % { $_.ExecutablePath }"',
  57. {
  58. stdio: ['pipe', 'pipe', 'ignore']
  59. }
  60. )
  61. .toString()
  62. const runningProcesses = output.split('\r\n')
  63. for (let i = 0; i < runningProcesses.length; i++) {
  64. const fullProcessPath = runningProcesses[i].trim()
  65. const shortProcessName = path.basename(fullProcessPath)
  66. if (COMMON_EDITORS_WIN.indexOf(shortProcessName) !== -1) {
  67. return [fullProcessPath]
  68. }
  69. }
  70. } else if (process.platform === 'linux') {
  71. // --no-heading No header line
  72. // x List all processes owned by you
  73. // -o comm Need only names column
  74. const output = childProcess
  75. .execSync('ps x --no-heading -o comm --sort=comm', {
  76. stdio: ['pipe', 'pipe', 'ignore']
  77. })
  78. .toString()
  79. const processNames = Object.keys(COMMON_EDITORS_LINUX)
  80. for (let i = 0; i < processNames.length; i++) {
  81. const processName = processNames[i]
  82. if (output.indexOf(processName) !== -1) {
  83. return [COMMON_EDITORS_LINUX[processName]]
  84. }
  85. }
  86. }
  87. } catch (ignoreError) {
  88. // Ignore...
  89. }
  90. // Last resort, use old skool env vars
  91. if (process.env.VISUAL) {
  92. return [process.env.VISUAL]
  93. } else if (process.env.EDITOR) {
  94. return [process.env.EDITOR]
  95. }
  96. return [null]
  97. }