123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- const path = require('path')
- const shellQuote = require('shell-quote')
- const childProcess = require('child_process')
- // Map from full process name to binary that starts the process
- // We can't just re-use full process name, because it will spawn a new instance
- // of the app every time
- const COMMON_EDITORS_OSX = require('./editor-info/osx')
- const COMMON_EDITORS_LINUX = require('./editor-info/linux')
- const COMMON_EDITORS_WIN = require('./editor-info/windows')
- module.exports = function guessEditor (specifiedEditor) {
- if (specifiedEditor) {
- return shellQuote.parse(specifiedEditor)
- }
- if (process.versions.webcontainer) {
- return [process.env.EDITOR || 'code']
- }
- // We can find out which editor is currently running by:
- // `ps x` on macOS and Linux
- // `Get-Process` on Windows
- try {
- if (process.platform === 'darwin') {
- const output = childProcess
- .execSync('ps x', {
- stdio: ['pipe', 'pipe', 'ignore']
- })
- .toString()
- const processNames = Object.keys(COMMON_EDITORS_OSX)
- for (let i = 0; i < processNames.length; i++) {
- const processName = processNames[i]
- if (output.indexOf(processName) !== -1) {
- return [COMMON_EDITORS_OSX[processName]]
- }
- }
- } else if (process.platform === 'win32') {
- const output = childProcess
- .execSync('powershell -Command "Get-Process | Select-Object Path"', {
- stdio: ['pipe', 'pipe', 'ignore']
- })
- .toString()
- const runningProcesses = output.split('\r\n')
- for (let i = 0; i < runningProcesses.length; i++) {
- // `Get-Process` sometimes returns empty lines
- if (!runningProcesses[i]) {
- continue
- }
- const fullProcessPath = runningProcesses[i].trim()
- const shortProcessName = path.basename(fullProcessPath)
- if (COMMON_EDITORS_WIN.indexOf(shortProcessName) !== -1) {
- return [fullProcessPath]
- }
- }
- } else if (process.platform === 'linux') {
- // --no-heading No header line
- // x List all processes owned by you
- // -o comm Need only names column
- const output = childProcess
- .execSync('ps x --no-heading -o comm --sort=comm', {
- stdio: ['pipe', 'pipe', 'ignore']
- })
- .toString()
- const processNames = Object.keys(COMMON_EDITORS_LINUX)
- for (let i = 0; i < processNames.length; i++) {
- const processName = processNames[i]
- if (output.indexOf(processName) !== -1) {
- return [COMMON_EDITORS_LINUX[processName]]
- }
- }
- }
- } catch (error) {
- // Ignore...
- }
- // Last resort, use old skool env vars
- if (process.env.VISUAL) {
- return [process.env.VISUAL]
- } else if (process.env.EDITOR) {
- return [process.env.EDITOR]
- }
- return [null]
- }
|