Skip to content

Commit 5785eaf

Browse files
committed
Fixed issue with Promise-based editor detection and cleaned up code
1 parent 55dca23 commit 5785eaf

File tree

2 files changed

+66
-51
lines changed

2 files changed

+66
-51
lines changed

packages/react-dev-utils/launchEditor.js

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ class PowerShell extends EventEmitter {
3030
}
3131
);
3232

33+
this._proc.on('error', () => {
34+
// Needs to be registered... already catched by if-statement below
35+
});
36+
37+
if (!this._proc.pid) {
38+
throw new Error('Failed to start PowerShell');
39+
}
40+
3341
let output = [];
3442
this._proc.stdout.on('data', data => {
3543
if (data.indexOf(EOI) !== -1) {
@@ -39,17 +47,9 @@ class PowerShell extends EventEmitter {
3947
output.push(data);
4048
}
4149
});
42-
43-
this._proc.on('error', () => {
44-
this._proc = null;
45-
});
4650
}
4751

4852
invoke(cmd) {
49-
if (!this._proc) {
50-
return Promise.resolve('');
51-
}
52-
5353
return new Promise(resolve => {
5454
this.on('resolve', data => {
5555
resolve(data);
@@ -154,63 +154,76 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
154154
}
155155

156156
let powerShellAgent = null;
157-
function launchPowerShellAgent() {
157+
function tryLaunchPowerShellAgent() {
158158
if (!powerShellAgent) {
159-
powerShellAgent = new PowerShell();
159+
try {
160+
powerShellAgent = new PowerShell();
161+
} catch (err) {
162+
// Failed to start, ignore silent...
163+
powerShellAgent = null;
164+
}
160165
}
161166
}
162167

163168
function guessEditor() {
164-
return new Promise(resolve => {
165-
// Explicit config always wins
166-
if (process.env.REACT_EDITOR) {
167-
return resolve(shellQuote.parse(process.env.REACT_EDITOR));
168-
}
169-
169+
return Promise.resolve().then(() => {
170170
// Using `ps x` on OSX or `Get-Process` on Windows we can find out which editor is currently running.
171171
// Potentially we could use similar technique for Linux
172-
try {
173-
if (process.platform === 'darwin') {
172+
if (process.platform === 'darwin') {
173+
try {
174174
const output = child_process.execSync('ps x').toString();
175175
const processNames = Object.keys(COMMON_EDITORS_OSX);
176176
for (let i = 0; i < processNames.length; i++) {
177177
const processName = processNames[i];
178178
if (output.indexOf(processName) !== -1) {
179-
return resolve([COMMON_EDITORS_OSX[processName]]);
179+
return COMMON_EDITORS_OSX[processName];
180180
}
181181
}
182-
} else if (process.platform === 'win32' && powerShellAgent) {
183-
return powerShellAgent
184-
.invoke('Get-Process | Select-Object Path')
185-
.then(output => {
186-
const runningProcesses = output.split('\r\n');
187-
for (let i = 0; i < runningProcesses.length; i++) {
188-
// `Get-Process` sometimes returns empty lines
189-
if (!runningProcesses[i]) {
190-
continue;
191-
}
192-
193-
const fullProcessPath = runningProcesses[i].trim();
194-
const shortProcessName = path.basename(fullProcessPath);
195-
196-
if (COMMON_EDITORS_WIN.indexOf(shortProcessName) !== -1) {
197-
return resolve([fullProcessPath]);
198-
}
199-
}
200-
});
182+
} catch (error) {
183+
// Ignore...
201184
}
202-
} catch (error) {
203-
// Ignore...
204-
}
185+
} else if (process.platform === 'win32' && powerShellAgent) {
186+
return powerShellAgent
187+
.invoke('Get-Process | Select-Object Path')
188+
.then(output => {
189+
const runningProcesses = output.split('\r\n');
190+
for (let i = 0; i < runningProcesses.length; i++) {
191+
// `Get-Process` sometimes returns empty lines
192+
if (!runningProcesses[i]) {
193+
continue;
194+
}
195+
196+
const fullProcessPath = runningProcesses[i].trim();
197+
const shortProcessName = path.basename(fullProcessPath);
205198

206-
// Last resort, use old skool env vars
207-
if (process.env.VISUAL) {
208-
return resolve([process.env.VISUAL]);
209-
} else if (process.env.EDITOR) {
210-
return resolve([process.env.EDITOR]);
199+
if (COMMON_EDITORS_WIN.indexOf(shortProcessName) !== -1) {
200+
return fullProcessPath;
201+
}
202+
}
203+
});
211204
}
205+
});
206+
}
207+
208+
function tryGetEditor() {
209+
// Explicit config always wins
210+
if (process.env.REACT_EDITOR) {
211+
return Promise.resolve(shellQuote.parse(process.env.REACT_EDITOR));
212+
}
212213

213-
return resolve([null]);
214+
return guessEditor().then(editor => {
215+
if (editor) {
216+
return [editor];
217+
} else {
218+
// Last resort, use old skool env vars
219+
if (process.env.VISUAL) {
220+
return [process.env.VISUAL];
221+
} else if (process.env.EDITOR) {
222+
return [process.env.EDITOR];
223+
}
224+
225+
return [null];
226+
}
214227
});
215228
}
216229

@@ -252,7 +265,7 @@ function launchEditor(fileName, lineNumber) {
252265
return;
253266
}
254267

255-
guessEditor().then(([editor, ...args]) => {
268+
tryGetEditor().then(([editor, ...args]) => {
256269
if (!editor) {
257270
printInstructions(fileName, null);
258271
return;
@@ -315,5 +328,5 @@ function launchEditor(fileName, lineNumber) {
315328

316329
module.exports = {
317330
launchEditor,
318-
launchPowerShellAgent,
331+
tryLaunchPowerShellAgent,
319332
};

packages/react-error-overlay/middleware.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const { launchEditor } = require('react-dev-utils/launchEditor');
1212

1313
module.exports = function createLaunchEditorMiddleware() {
1414
if (process.platform === 'win32' && !process.env.REACT_EDITOR) {
15-
const { launchPowerShellAgent } = require('react-dev-utils/launchEditor');
16-
launchPowerShellAgent();
15+
const {
16+
tryLaunchPowerShellAgent,
17+
} = require('react-dev-utils/launchEditor');
18+
tryLaunchPowerShellAgent();
1719
}
1820

1921
return function launchEditorMiddleware(req, res, next) {

0 commit comments

Comments
 (0)