|
| 1 | +// fork from https://github.com/yyx990803/launch-editor/blob/master/packages/launch-editor/guess.js |
| 2 | + |
| 3 | +import path from 'node:path' |
| 4 | +import process from 'node:process' |
| 5 | +import child_process from 'node:child_process' |
| 6 | + |
| 7 | +// Map from full process name to binary that starts the process |
| 8 | +// We can't just re-use full process name, because it will spawn a new instance |
| 9 | +// of the app every time |
| 10 | +const COMMON_EDITORS_OSX = { |
| 11 | + '/Applications/Atom.app/Contents/MacOS/Atom': 'atom', |
| 12 | + '/Applications/Atom Beta.app/Contents/MacOS/Atom Beta': |
| 13 | + '/Applications/Atom Beta.app/Contents/MacOS/Atom Beta', |
| 14 | + '/Applications/Brackets.app/Contents/MacOS/Brackets': 'brackets', |
| 15 | + '/Applications/Sublime Text.app/Contents/MacOS/Sublime Text': |
| 16 | + '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl', |
| 17 | + '/Applications/Sublime Text.app/Contents/MacOS/sublime_text': |
| 18 | + '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl', |
| 19 | + '/Applications/Sublime Text 2.app/Contents/MacOS/Sublime Text 2': |
| 20 | + '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl', |
| 21 | + '/Applications/Sublime Text Dev.app/Contents/MacOS/Sublime Text': |
| 22 | + '/Applications/Sublime Text Dev.app/Contents/SharedSupport/bin/subl', |
| 23 | + '/Applications/Visual Studio Code.app/Contents/MacOS/Electron': 'code', |
| 24 | + '/Applications/Visual Studio Code - Insiders.app/Contents/MacOS/Electron': |
| 25 | + 'code-insiders', |
| 26 | + '/Applications/VSCodium.app/Contents/MacOS/Electron': 'codium', |
| 27 | + '/Applications/Cursor.app/Contents/MacOS/Cursor': 'cursor', |
| 28 | + '/Applications/AppCode.app/Contents/MacOS/appcode': |
| 29 | + '/Applications/AppCode.app/Contents/MacOS/appcode', |
| 30 | + '/Applications/CLion.app/Contents/MacOS/clion': |
| 31 | + '/Applications/CLion.app/Contents/MacOS/clion', |
| 32 | + '/Applications/IntelliJ IDEA.app/Contents/MacOS/idea': |
| 33 | + '/Applications/IntelliJ IDEA.app/Contents/MacOS/idea', |
| 34 | + '/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea': |
| 35 | + '/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea', |
| 36 | + '/Applications/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea': |
| 37 | + '/Applications/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea', |
| 38 | + '/Applications/PhpStorm.app/Contents/MacOS/phpstorm': |
| 39 | + '/Applications/PhpStorm.app/Contents/MacOS/phpstorm', |
| 40 | + '/Applications/PyCharm.app/Contents/MacOS/pycharm': |
| 41 | + '/Applications/PyCharm.app/Contents/MacOS/pycharm', |
| 42 | + '/Applications/PyCharm CE.app/Contents/MacOS/pycharm': |
| 43 | + '/Applications/PyCharm CE.app/Contents/MacOS/pycharm', |
| 44 | + '/Applications/RubyMine.app/Contents/MacOS/rubymine': |
| 45 | + '/Applications/RubyMine.app/Contents/MacOS/rubymine', |
| 46 | + '/Applications/WebStorm.app/Contents/MacOS/webstorm': |
| 47 | + '/Applications/WebStorm.app/Contents/MacOS/webstorm', |
| 48 | + '/Applications/MacVim.app/Contents/MacOS/MacVim': 'mvim', |
| 49 | + '/Applications/GoLand.app/Contents/MacOS/goland': |
| 50 | + '/Applications/GoLand.app/Contents/MacOS/goland', |
| 51 | + '/Applications/Rider.app/Contents/MacOS/rider': |
| 52 | + '/Applications/Rider.app/Contents/MacOS/rider', |
| 53 | + '/Applications/Zed.app/Contents/MacOS/zed': 'zed', |
| 54 | +} |
| 55 | + |
| 56 | +const COMMON_EDITORS_LINUX = { |
| 57 | + 'atom': 'atom', |
| 58 | + 'Brackets': 'brackets', |
| 59 | + 'code-insiders': 'code-insiders', |
| 60 | + 'code': 'code', |
| 61 | + 'vscodium': 'vscodium', |
| 62 | + 'codium': 'codium', |
| 63 | + 'emacs': 'emacs', |
| 64 | + 'gvim': 'gvim', |
| 65 | + 'idea.sh': 'idea', |
| 66 | + 'phpstorm.sh': 'phpstorm', |
| 67 | + 'pycharm.sh': 'pycharm', |
| 68 | + 'rubymine.sh': 'rubymine', |
| 69 | + 'sublime_text': 'subl', |
| 70 | + 'vim': 'vim', |
| 71 | + 'webstorm.sh': 'webstorm', |
| 72 | + 'goland.sh': 'goland', |
| 73 | + 'rider.sh': 'rider', |
| 74 | +} |
| 75 | + |
| 76 | +const COMMON_EDITORS_WIN = [ |
| 77 | + 'Brackets.exe', |
| 78 | + 'Code.exe', |
| 79 | + 'Code - Insiders.exe', |
| 80 | + 'VSCodium.exe', |
| 81 | + 'atom.exe', |
| 82 | + 'sublime_text.exe', |
| 83 | + 'notepad++.exe', |
| 84 | + 'clion.exe', |
| 85 | + 'clion64.exe', |
| 86 | + 'idea.exe', |
| 87 | + 'idea64.exe', |
| 88 | + 'phpstorm.exe', |
| 89 | + 'phpstorm64.exe', |
| 90 | + 'pycharm.exe', |
| 91 | + 'pycharm64.exe', |
| 92 | + 'rubymine.exe', |
| 93 | + 'rubymine64.exe', |
| 94 | + 'webstorm.exe', |
| 95 | + 'webstorm64.exe', |
| 96 | + 'goland.exe', |
| 97 | + 'goland64.exe', |
| 98 | + 'rider.exe', |
| 99 | + 'rider64.exe', |
| 100 | +] |
| 101 | + |
| 102 | +export function guessEditor(): [string | null, string?] { |
| 103 | + // We can find out which editor is currently running by: |
| 104 | + // `ps x` on macOS and Linux |
| 105 | + // `Get-Process` on Windows |
| 106 | + try { |
| 107 | + if (process.platform === 'darwin') { |
| 108 | + const output = child_process.execSync('ps x').toString() |
| 109 | + const processNames = Object.keys(COMMON_EDITORS_OSX) |
| 110 | + const processName = processNames.find(processName => output.includes(processName)) |
| 111 | + return [processName ? COMMON_EDITORS_OSX[processName] : null] |
| 112 | + } |
| 113 | + else if (process.platform === 'win32') { |
| 114 | + // Some processes need elevated rights to get its executable path. |
| 115 | + // Just filter them out upfront. This also saves 10-20ms on the command. |
| 116 | + const output = child_process |
| 117 | + .execSync( |
| 118 | + 'wmic process where "executablepath is not null" get executablepath', |
| 119 | + ) |
| 120 | + .toString() |
| 121 | + const runningProcesses = output.split('\r\n') |
| 122 | + |
| 123 | + const processName = runningProcesses |
| 124 | + .find(processName => COMMON_EDITORS_WIN |
| 125 | + .includes(path.basename(processName.trim()))) |
| 126 | + return [processName ? processName.trim() : null] |
| 127 | + } |
| 128 | + else if (process.platform === 'linux') { |
| 129 | + // --no-heading No header line |
| 130 | + // x List all processes owned by you |
| 131 | + // -o comm Need only names column |
| 132 | + const output = child_process |
| 133 | + .execSync('ps x --no-heading -o comm --sort=comm') |
| 134 | + .toString() |
| 135 | + const processNames = Object.keys(COMMON_EDITORS_LINUX) |
| 136 | + const processName = processNames.find(processName => output.includes(processName)) |
| 137 | + return [processName ? COMMON_EDITORS_LINUX[processName] : null] |
| 138 | + } |
| 139 | + } |
| 140 | + catch (error) { |
| 141 | + // Ignore... |
| 142 | + return [null] |
| 143 | + } |
| 144 | + |
| 145 | + // Last resort, use old skool env vars |
| 146 | + if (process.env.VISUAL) |
| 147 | + return [process.env.VISUAL] |
| 148 | + else if (process.env.EDITOR) |
| 149 | + return [process.env.EDITOR] |
| 150 | + |
| 151 | + return [null] |
| 152 | +} |
0 commit comments