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