Skip to content

Commit 10c734b

Browse files
authored
Print error messages for editor integration (#2150)
1 parent 82687dd commit 10c734b

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

packages/react-dev-utils/launchEditor.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
var fs = require('fs');
1212
var path = require('path');
1313
var child_process = require('child_process');
14-
const shellQuote = require('shell-quote');
14+
var chalk = require('chalk');
15+
var shellQuote = require('shell-quote');
1516

1617
function isTerminalEditor(editor) {
1718
switch (editor) {
@@ -110,6 +111,31 @@ function guessEditor() {
110111
return [null];
111112
}
112113

114+
function printInstructions(fileName, errorMessage) {
115+
console.log();
116+
console.log(
117+
chalk.red('Could not open ' + path.basename(fileName) + ' in the editor.')
118+
);
119+
if (errorMessage) {
120+
if (errorMessage[errorMessage.length - 1] !== '.') {
121+
errorMessage += '.';
122+
}
123+
console.log(
124+
chalk.red('The editor process exited with an error: ' + errorMessage)
125+
);
126+
}
127+
console.log();
128+
console.log(
129+
'To set up the editor integration, add something like ' +
130+
chalk.cyan('REACT_EDITOR=atom') +
131+
' to the ' +
132+
chalk.green('.env.local') +
133+
' file in your project folder ' +
134+
'and restart the development server.'
135+
);
136+
console.log();
137+
}
138+
113139
var _childProcess = null;
114140
function launchEditor(fileName, lineNumber) {
115141
if (!fs.existsSync(fileName)) {
@@ -124,6 +150,7 @@ function launchEditor(fileName, lineNumber) {
124150

125151
let [editor, ...args] = guessEditor();
126152
if (!editor) {
153+
printInstructions(fileName, null);
127154
return;
128155
}
129156

@@ -154,8 +181,16 @@ function launchEditor(fileName, lineNumber) {
154181
} else {
155182
_childProcess = child_process.spawn(editor, args, { stdio: 'inherit' });
156183
}
157-
_childProcess.on('exit', function() {
184+
_childProcess.on('exit', function(errorCode) {
158185
_childProcess = null;
186+
187+
if (errorCode) {
188+
printInstructions(fileName, '(code ' + errorCode + ')');
189+
}
190+
});
191+
192+
_childProcess.on('error', function(error) {
193+
printInstructions(fileName, error.message);
159194
});
160195
}
161196

0 commit comments

Comments
 (0)