8
8
*/
9
9
'use strict' ;
10
10
11
- var fs = require ( 'fs' ) ;
12
- var path = require ( 'path' ) ;
13
- var child_process = require ( 'child_process' ) ;
14
- var os = require ( 'os' ) ;
15
- var chalk = require ( 'chalk' ) ;
16
- var shellQuote = require ( 'shell-quote' ) ;
11
+ const fs = require ( 'fs' ) ;
12
+ const path = require ( 'path' ) ;
13
+ const child_process = require ( 'child_process' ) ;
14
+ const os = require ( 'os' ) ;
15
+ const chalk = require ( 'chalk' ) ;
16
+ const shellQuote = require ( 'shell-quote' ) ;
17
17
18
18
function isTerminalEditor ( editor ) {
19
19
switch ( editor ) {
@@ -28,14 +28,20 @@ function isTerminalEditor(editor) {
28
28
// Map from full process name to binary that starts the process
29
29
// We can't just re-use full process name, because it will spawn a new instance
30
30
// of the app every time
31
- var COMMON_EDITORS = {
31
+ const COMMON_EDITORS_OSX = {
32
32
'/Applications/Atom.app/Contents/MacOS/Atom' : 'atom' ,
33
33
'/Applications/Atom Beta.app/Contents/MacOS/Atom Beta' : '/Applications/Atom Beta.app/Contents/MacOS/Atom Beta' ,
34
34
'/Applications/Sublime Text.app/Contents/MacOS/Sublime Text' : '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl' ,
35
35
'/Applications/Sublime Text 2.app/Contents/MacOS/Sublime Text 2' : '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl' ,
36
36
'/Applications/Visual Studio Code.app/Contents/MacOS/Electron' : 'code' ,
37
37
} ;
38
38
39
+ const COMMON_EDITORS_WIN = [
40
+ 'Code.exe' ,
41
+ 'atom.exe' ,
42
+ 'sublime_text.exe'
43
+ ] ;
44
+
39
45
function addWorkspaceToArgumentsIfExists ( args , workspace ) {
40
46
if ( workspace ) {
41
47
args . unshift ( workspace ) ;
@@ -44,7 +50,7 @@ function addWorkspaceToArgumentsIfExists(args, workspace) {
44
50
}
45
51
46
52
function getArgumentsForLineNumber ( editor , fileName , lineNumber , workspace ) {
47
- var editorBasename = path . basename ( editor ) . replace ( / \. ( e x e | c m d | b a t ) $ / i, '' ) ;
53
+ const editorBasename = path . basename ( editor ) . replace ( / \. ( e x e | c m d | b a t ) $ / i, '' ) ;
48
54
switch ( editorBasename ) {
49
55
case 'vim' :
50
56
case 'mvim' :
@@ -54,6 +60,7 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
54
60
case 'Atom Beta' :
55
61
case 'subl' :
56
62
case 'sublime' :
63
+ case 'sublime_text' :
57
64
case 'wstorm' :
58
65
case 'appcode' :
59
66
case 'charm' :
@@ -68,6 +75,7 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
68
75
case 'mine' :
69
76
return [ '--line' , lineNumber , fileName ] ;
70
77
case 'code' :
78
+ case 'Code' :
71
79
return addWorkspaceToArgumentsIfExists (
72
80
[ '-g' , fileName + ':' + lineNumber ] ,
73
81
workspace
@@ -92,21 +100,37 @@ function guessEditor() {
92
100
return shellQuote . parse ( process . env . REACT_EDITOR ) ;
93
101
}
94
102
95
- // Using `ps x` on OSX we can find out which editor is currently running.
96
- // Potentially we could use similar technique for Windows and Linux
97
- if ( process . platform === 'darwin' ) {
98
- try {
99
- var output = child_process . execSync ( 'ps x' ) . toString ( ) ;
100
- var processNames = Object . keys ( COMMON_EDITORS ) ;
101
- for ( var i = 0 ; i < processNames . length ; i ++ ) {
102
- var processName = processNames [ i ] ;
103
+ // Using `ps x` on OSX or `Get-Process` on Windows we can find out which editor is currently running.
104
+ // Potentially we could use similar technique for Linux
105
+ try {
106
+ if ( process . platform === 'darwin' ) {
107
+ const output = child_process . execSync ( 'ps x' ) . toString ( ) ;
108
+ const processNames = Object . keys ( COMMON_EDITORS_OSX ) ;
109
+ for ( let i = 0 ; i < processNames . length ; i ++ ) {
110
+ const processName = processNames [ i ] ;
103
111
if ( output . indexOf ( processName ) !== - 1 ) {
104
- return [ COMMON_EDITORS [ processName ] ] ;
112
+ return [ COMMON_EDITORS_OSX [ processName ] ] ;
113
+ }
114
+ }
115
+ } else if ( process . platform === 'win32' ) {
116
+ const output = child_process . execSync ( 'powershell -Command "Get-Process | Select-Object Path"' ) . toString ( ) ;
117
+ const runningProcesses = output . split ( '\r\n' ) ;
118
+ for ( let i = 0 ; i < runningProcesses . length ; i ++ ) {
119
+ // `Get-Process` sometimes returns empty lines
120
+ if ( ! runningProcesses [ i ] ) {
121
+ continue ;
122
+ }
123
+
124
+ const fullProcessPath = runningProcesses [ i ] . trim ( ) ;
125
+ const shortProcessName = path . basename ( fullProcessPath ) ;
126
+
127
+ if ( COMMON_EDITORS_WIN . indexOf ( shortProcessName ) !== - 1 ) {
128
+ return [ fullProcessPath ] ;
105
129
}
106
130
}
107
- } catch ( error ) {
108
- // Ignore...
109
131
}
132
+ } catch ( error ) {
133
+ // Ignore...
110
134
}
111
135
112
136
// Last resort, use old skool env vars
@@ -144,7 +168,7 @@ function printInstructions(fileName, errorMessage) {
144
168
console . log ( ) ;
145
169
}
146
170
147
- var _childProcess = null ;
171
+ let _childProcess = null ;
148
172
function launchEditor ( fileName , lineNumber ) {
149
173
if ( ! fs . existsSync ( fileName ) ) {
150
174
return ;
@@ -176,7 +200,7 @@ function launchEditor(fileName, lineNumber) {
176
200
fileName = path . relative ( '' , fileName ) ;
177
201
}
178
202
179
- var workspace = null ;
203
+ let workspace = null ;
180
204
if ( lineNumber ) {
181
205
args = args . concat (
182
206
getArgumentsForLineNumber ( editor , fileName , lineNumber , workspace )
0 commit comments