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,21 @@ 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
+ 'notepad++.exe' ,
44
+ ] ;
45
+
39
46
function addWorkspaceToArgumentsIfExists ( args , workspace ) {
40
47
if ( workspace ) {
41
48
args . unshift ( workspace ) ;
@@ -44,7 +51,7 @@ function addWorkspaceToArgumentsIfExists(args, workspace) {
44
51
}
45
52
46
53
function getArgumentsForLineNumber ( editor , fileName , lineNumber , workspace ) {
47
- var editorBasename = path . basename ( editor ) . replace ( / \. ( e x e | c m d | b a t ) $ / i, '' ) ;
54
+ const editorBasename = path . basename ( editor ) . replace ( / \. ( e x e | c m d | b a t ) $ / i, '' ) ;
48
55
switch ( editorBasename ) {
49
56
case 'vim' :
50
57
case 'mvim' :
@@ -54,11 +61,14 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
54
61
case 'Atom Beta' :
55
62
case 'subl' :
56
63
case 'sublime' :
64
+ case 'sublime_text' :
57
65
case 'wstorm' :
58
66
case 'appcode' :
59
67
case 'charm' :
60
68
case 'idea' :
61
69
return [ fileName + ':' + lineNumber ] ;
70
+ case 'notepad++' :
71
+ return [ '-n' + lineNumber , fileName ] ;
62
72
case 'joe' :
63
73
case 'emacs' :
64
74
case 'emacsclient' :
@@ -68,6 +78,7 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
68
78
case 'mine' :
69
79
return [ '--line' , lineNumber , fileName ] ;
70
80
case 'code' :
81
+ case 'Code' :
71
82
return addWorkspaceToArgumentsIfExists (
72
83
[ '-g' , fileName + ':' + lineNumber ] ,
73
84
workspace
@@ -94,21 +105,41 @@ function guessEditor() {
94
105
return shellQuote . parse ( process . env . REACT_EDITOR ) ;
95
106
}
96
107
97
- // Using `ps x` on OSX we can find out which editor is currently running.
98
- // Potentially we could use similar technique for Windows and Linux
99
- if ( process . platform === 'darwin' ) {
100
- try {
101
- var output = child_process . execSync ( 'ps x' ) . toString ( ) ;
102
- var processNames = Object . keys ( COMMON_EDITORS ) ;
103
- for ( var i = 0 ; i < processNames . length ; i ++ ) {
104
- var processName = processNames [ i ] ;
108
+ // Using `ps x` on OSX or `Get-Process` on Windows we can find out which editor is currently running.
109
+ // Potentially we could use similar technique for Linux
110
+ try {
111
+ if ( process . platform === 'darwin' ) {
112
+ const output = child_process . execSync ( 'ps x' ) . toString ( ) ;
113
+ const processNames = Object . keys ( COMMON_EDITORS_OSX ) ;
114
+ for ( let i = 0 ; i < processNames . length ; i ++ ) {
115
+ const processName = processNames [ i ] ;
105
116
if ( output . indexOf ( processName ) !== - 1 ) {
106
- return [ COMMON_EDITORS [ processName ] ] ;
117
+ return [ COMMON_EDITORS_OSX [ processName ] ] ;
118
+ }
119
+ }
120
+ } else if ( process . platform === 'win32' ) {
121
+ const output = child_process
122
+ . execSync ( 'powershell -Command "Get-Process | Select-Object Path"' , {
123
+ stdio : [ 'pipe' , 'pipe' , 'ignore' ] ,
124
+ } )
125
+ . toString ( ) ;
126
+ const runningProcesses = output . split ( '\r\n' ) ;
127
+ for ( let i = 0 ; i < runningProcesses . length ; i ++ ) {
128
+ // `Get-Process` sometimes returns empty lines
129
+ if ( ! runningProcesses [ i ] ) {
130
+ continue ;
131
+ }
132
+
133
+ const fullProcessPath = runningProcesses [ i ] . trim ( ) ;
134
+ const shortProcessName = path . basename ( fullProcessPath ) ;
135
+
136
+ if ( COMMON_EDITORS_WIN . indexOf ( shortProcessName ) !== - 1 ) {
137
+ return [ fullProcessPath ] ;
107
138
}
108
139
}
109
- } catch ( error ) {
110
- // Ignore...
111
140
}
141
+ } catch ( error ) {
142
+ // Ignore...
112
143
}
113
144
114
145
// Last resort, use old skool env vars
@@ -146,7 +177,7 @@ function printInstructions(fileName, errorMessage) {
146
177
console . log ( ) ;
147
178
}
148
179
149
- var _childProcess = null ;
180
+ let _childProcess = null ;
150
181
function launchEditor ( fileName , lineNumber ) {
151
182
if ( ! fs . existsSync ( fileName ) ) {
152
183
return ;
@@ -178,7 +209,7 @@ function launchEditor(fileName, lineNumber) {
178
209
fileName = path . relative ( '' , fileName ) ;
179
210
}
180
211
181
- var workspace = null ;
212
+ let workspace = null ;
182
213
if ( lineNumber ) {
183
214
args = args . concat (
184
215
getArgumentsForLineNumber ( editor , fileName , lineNumber , workspace )
0 commit comments