Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print error messages for editor integration #2150

Merged
merged 1 commit into from
May 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions packages/react-dev-utils/launchEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
const shellQuote = require('shell-quote');
var chalk = require('chalk');
var shellQuote = require('shell-quote');

function isTerminalEditor(editor) {
switch (editor) {
Expand Down Expand Up @@ -110,6 +111,31 @@ function guessEditor() {
return [null];
}

function printInstructions(fileName, errorMessage) {
console.log();
console.log(
chalk.red('Could not open ' + path.basename(fileName) + ' in the editor.')
);
if (errorMessage) {
if (errorMessage[errorMessage.length - 1] !== '.') {
errorMessage += '.';
}
console.log(
chalk.red('The editor process exited with an error: ' + errorMessage)
);
}
console.log();
console.log(
'To set up the editor integration, add something like ' +
chalk.cyan('REACT_EDITOR=atom') +
' to the ' +
chalk.green('.env.local') +
' file in your project folder ' +
'and restart the development server.'
);
console.log();
}

var _childProcess = null;
function launchEditor(fileName, lineNumber) {
if (!fs.existsSync(fileName)) {
Expand All @@ -124,6 +150,7 @@ function launchEditor(fileName, lineNumber) {

let [editor, ...args] = guessEditor();
if (!editor) {
printInstructions(fileName, null);
return;
}

Expand Down Expand Up @@ -154,8 +181,16 @@ function launchEditor(fileName, lineNumber) {
} else {
_childProcess = child_process.spawn(editor, args, { stdio: 'inherit' });
}
_childProcess.on('exit', function() {
_childProcess.on('exit', function(errorCode) {
_childProcess = null;

if (errorCode) {
printInstructions(fileName, '(code ' + errorCode + ')');
}
});

_childProcess.on('error', function(error) {
printInstructions(fileName, error.message);
});
}

Expand Down