Skip to content

Commit c0d2600

Browse files
committed
Auto-detect running editor on Windows for error overlay
1 parent 67f6163 commit c0d2600

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

packages/react-dev-utils/launchEditor.js

+45-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,20 @@ 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+
];
44+
3945
function addWorkspaceToArgumentsIfExists(args, workspace) {
4046
if (workspace) {
4147
args.unshift(workspace);
@@ -44,7 +50,7 @@ function addWorkspaceToArgumentsIfExists(args, workspace) {
4450
}
4551

4652
function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
47-
var editorBasename = path.basename(editor).replace(/\.(exe|cmd|bat)$/i, '');
53+
const editorBasename = path.basename(editor).replace(/\.(exe|cmd|bat)$/i, '');
4854
switch (editorBasename) {
4955
case 'vim':
5056
case 'mvim':
@@ -54,6 +60,7 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
5460
case 'Atom Beta':
5561
case 'subl':
5662
case 'sublime':
63+
case 'sublime_text':
5764
case 'wstorm':
5865
case 'appcode':
5966
case 'charm':
@@ -68,6 +75,7 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
6875
case 'mine':
6976
return ['--line', lineNumber, fileName];
7077
case 'code':
78+
case 'Code':
7179
return addWorkspaceToArgumentsIfExists(
7280
['-g', fileName + ':' + lineNumber],
7381
workspace
@@ -92,21 +100,37 @@ function guessEditor() {
92100
return shellQuote.parse(process.env.REACT_EDITOR);
93101
}
94102

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];
103111
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];
105129
}
106130
}
107-
} catch (error) {
108-
// Ignore...
109131
}
132+
} catch (error) {
133+
// Ignore...
110134
}
111135

112136
// Last resort, use old skool env vars
@@ -144,7 +168,7 @@ function printInstructions(fileName, errorMessage) {
144168
console.log();
145169
}
146170

147-
var _childProcess = null;
171+
let _childProcess = null;
148172
function launchEditor(fileName, lineNumber) {
149173
if (!fs.existsSync(fileName)) {
150174
return;
@@ -176,7 +200,7 @@ function launchEditor(fileName, lineNumber) {
176200
fileName = path.relative('', fileName);
177201
}
178202

179-
var workspace = null;
203+
let workspace = null;
180204
if (lineNumber) {
181205
args = args.concat(
182206
getArgumentsForLineNumber(editor, fileName, lineNumber, workspace)

0 commit comments

Comments
 (0)