@@ -9,7 +9,60 @@ const COMMON_EDITORS_MACOS = require('./editor-info/macos')
99const COMMON_EDITORS_LINUX = require ( './editor-info/linux' )
1010const COMMON_EDITORS_WIN = require ( './editor-info/windows' )
1111
12- module . exports = function guessEditor ( specifiedEditor ) {
12+ function getEditorFromMacProcesses ( output ) {
13+ const processNames = Object . keys ( COMMON_EDITORS_MACOS )
14+ const processList = output . split ( '\n' )
15+ for ( let i = 0 ; i < processNames . length ; i ++ ) {
16+ const processName = processNames [ i ]
17+ // Find editor by exact match.
18+ if ( processList . includes ( processName ) ) {
19+ return COMMON_EDITORS_MACOS [ processName ]
20+ }
21+ const processNameWithoutApplications = processName . replace ( '/Applications' , '' )
22+ // Find editor installation not in /Applications.
23+ if ( output . indexOf ( processNameWithoutApplications ) !== - 1 ) {
24+ // Use the CLI command if one is specified
25+ if ( processName !== COMMON_EDITORS_MACOS [ processName ] ) {
26+ return COMMON_EDITORS_MACOS [ processName ]
27+ }
28+ // Use a partial match to find the running process path. If one is found, use the
29+ // existing path since it can be running from anywhere.
30+ const runningProcess = processList . find ( ( procName ) =>
31+ procName . endsWith ( processNameWithoutApplications ) ,
32+ )
33+ if ( runningProcess !== undefined ) {
34+ return runningProcess
35+ }
36+ }
37+ }
38+ return undefined
39+ }
40+
41+ function getEditorFromWindowsProcesses ( output ) {
42+ const runningProcesses = output . split ( '\r\n' )
43+ for ( let i = 0 ; i < runningProcesses . length ; i ++ ) {
44+ const fullProcessPath = runningProcesses [ i ] . trim ( )
45+ const shortProcessName = path . win32 . basename ( fullProcessPath )
46+
47+ if ( COMMON_EDITORS_WIN . indexOf ( shortProcessName ) !== - 1 ) {
48+ return fullProcessPath
49+ }
50+ }
51+ return undefined
52+ }
53+
54+ function getEditorFromLinuxProcesses ( output ) {
55+ const processNames = Object . keys ( COMMON_EDITORS_LINUX )
56+ for ( let i = 0 ; i < processNames . length ; i ++ ) {
57+ const processName = processNames [ i ]
58+ if ( output . indexOf ( processName ) !== - 1 ) {
59+ return COMMON_EDITORS_LINUX [ processName ]
60+ }
61+ }
62+ return undefined
63+ }
64+
65+ function guessEditor ( specifiedEditor ) {
1366 if ( specifiedEditor ) {
1467 return shellQuote . parse ( specifiedEditor )
1568 }
@@ -32,30 +85,9 @@ module.exports = function guessEditor(specifiedEditor) {
3285 stdio : [ 'pipe' , 'pipe' , 'ignore' ] ,
3386 } )
3487 . 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 ) =>
53- procName . endsWith ( processNameWithoutApplications ) ,
54- )
55- if ( runningProcess !== undefined ) {
56- return [ runningProcess ]
57- }
58- }
88+ const editor = getEditorFromMacProcesses ( output )
89+ if ( editor !== undefined ) {
90+ return [ editor ]
5991 }
6092 } else if ( process . platform === 'win32' ) {
6193 const output = childProcess
@@ -69,14 +101,9 @@ module.exports = function guessEditor(specifiedEditor) {
69101 } ,
70102 )
71103 . toString ( )
72- const runningProcesses = output . split ( '\r\n' )
73- for ( let i = 0 ; i < runningProcesses . length ; i ++ ) {
74- const fullProcessPath = runningProcesses [ i ] . trim ( )
75- const shortProcessName = path . basename ( fullProcessPath )
76-
77- if ( COMMON_EDITORS_WIN . indexOf ( shortProcessName ) !== - 1 ) {
78- return [ fullProcessPath ]
79- }
104+ const editor = getEditorFromWindowsProcesses ( output )
105+ if ( editor !== undefined ) {
106+ return [ editor ]
80107 }
81108 } else if ( process . platform === 'linux' ) {
82109 // --no-heading No header line
@@ -87,12 +114,9 @@ module.exports = function guessEditor(specifiedEditor) {
87114 stdio : [ 'pipe' , 'pipe' , 'ignore' ] ,
88115 } )
89116 . toString ( )
90- const processNames = Object . keys ( COMMON_EDITORS_LINUX )
91- for ( let i = 0 ; i < processNames . length ; i ++ ) {
92- const processName = processNames [ i ]
93- if ( output . indexOf ( processName ) !== - 1 ) {
94- return [ COMMON_EDITORS_LINUX [ processName ] ]
95- }
117+ const editor = getEditorFromLinuxProcesses ( output )
118+ if ( editor !== undefined ) {
119+ return [ editor ]
96120 }
97121 }
98122 } catch ( ignoreError ) {
@@ -108,3 +132,8 @@ module.exports = function guessEditor(specifiedEditor) {
108132
109133 return [ null ]
110134}
135+
136+ module . exports = guessEditor
137+ module . exports . getEditorFromMacProcesses = getEditorFromMacProcesses
138+ module . exports . getEditorFromWindowsProcesses = getEditorFromWindowsProcesses
139+ module . exports . getEditorFromLinuxProcesses = getEditorFromLinuxProcesses
0 commit comments