Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Commit b9cbf5c

Browse files
levrikwmonk
authored andcommitted
Auto-detect running editor on Windows for error overlay (#2552)
* Auto-detect running editor on Windows for error overlay * Ignore process output if powershell call fails * Support Notepad++
1 parent 88e03ce commit b9cbf5c

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

packages/react-dev-utils/launchEditor.js

+52-21
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
*/
99
'use strict';
1010

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');
1717

1818
function isTerminalEditor(editor) {
1919
switch (editor) {
@@ -28,14 +28,21 @@ function isTerminalEditor(editor) {
2828
// Map from full process name to binary that starts the process
2929
// We can't just re-use full process name, because it will spawn a new instance
3030
// of the app every time
31-
var COMMON_EDITORS = {
31+
const COMMON_EDITORS_OSX = {
3232
'/Applications/Atom.app/Contents/MacOS/Atom': 'atom',
3333
'/Applications/Atom Beta.app/Contents/MacOS/Atom Beta': '/Applications/Atom Beta.app/Contents/MacOS/Atom Beta',
3434
'/Applications/Sublime Text.app/Contents/MacOS/Sublime Text': '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl',
3535
'/Applications/Sublime Text 2.app/Contents/MacOS/Sublime Text 2': '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl',
3636
'/Applications/Visual Studio Code.app/Contents/MacOS/Electron': 'code',
3737
};
3838

39+
const COMMON_EDITORS_WIN = [
40+
'Code.exe',
41+
'atom.exe',
42+
'sublime_text.exe',
43+
'notepad++.exe',
44+
];
45+
3946
function addWorkspaceToArgumentsIfExists(args, workspace) {
4047
if (workspace) {
4148
args.unshift(workspace);
@@ -44,7 +51,7 @@ function addWorkspaceToArgumentsIfExists(args, workspace) {
4451
}
4552

4653
function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
47-
var editorBasename = path.basename(editor).replace(/\.(exe|cmd|bat)$/i, '');
54+
const editorBasename = path.basename(editor).replace(/\.(exe|cmd|bat)$/i, '');
4855
switch (editorBasename) {
4956
case 'vim':
5057
case 'mvim':
@@ -54,11 +61,14 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
5461
case 'Atom Beta':
5562
case 'subl':
5663
case 'sublime':
64+
case 'sublime_text':
5765
case 'wstorm':
5866
case 'appcode':
5967
case 'charm':
6068
case 'idea':
6169
return [fileName + ':' + lineNumber];
70+
case 'notepad++':
71+
return ['-n' + lineNumber, fileName];
6272
case 'joe':
6373
case 'emacs':
6474
case 'emacsclient':
@@ -68,6 +78,7 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
6878
case 'mine':
6979
return ['--line', lineNumber, fileName];
7080
case 'code':
81+
case 'Code':
7182
return addWorkspaceToArgumentsIfExists(
7283
['-g', fileName + ':' + lineNumber],
7384
workspace
@@ -94,21 +105,41 @@ function guessEditor() {
94105
return shellQuote.parse(process.env.REACT_EDITOR);
95106
}
96107

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];
105116
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];
107138
}
108139
}
109-
} catch (error) {
110-
// Ignore...
111140
}
141+
} catch (error) {
142+
// Ignore...
112143
}
113144

114145
// Last resort, use old skool env vars
@@ -146,7 +177,7 @@ function printInstructions(fileName, errorMessage) {
146177
console.log();
147178
}
148179

149-
var _childProcess = null;
180+
let _childProcess = null;
150181
function launchEditor(fileName, lineNumber) {
151182
if (!fs.existsSync(fileName)) {
152183
return;
@@ -178,7 +209,7 @@ function launchEditor(fileName, lineNumber) {
178209
fileName = path.relative('', fileName);
179210
}
180211

181-
var workspace = null;
212+
let workspace = null;
182213
if (lineNumber) {
183214
args = args.concat(
184215
getArgumentsForLineNumber(editor, fileName, lineNumber, workspace)

0 commit comments

Comments
 (0)