11
11
var fs = require ( 'fs' ) ;
12
12
var path = require ( 'path' ) ;
13
13
var child_process = require ( 'child_process' ) ;
14
- const shellQuote = require ( 'shell-quote' ) ;
14
+ var chalk = require ( 'chalk' ) ;
15
+ var shellQuote = require ( 'shell-quote' ) ;
15
16
16
17
function isTerminalEditor ( editor ) {
17
18
switch ( editor ) {
@@ -110,6 +111,31 @@ function guessEditor() {
110
111
return [ null ] ;
111
112
}
112
113
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
+
113
139
var _childProcess = null ;
114
140
function launchEditor ( fileName , lineNumber ) {
115
141
if ( ! fs . existsSync ( fileName ) ) {
@@ -124,6 +150,7 @@ function launchEditor(fileName, lineNumber) {
124
150
125
151
let [ editor , ...args ] = guessEditor ( ) ;
126
152
if ( ! editor ) {
153
+ printInstructions ( fileName , null ) ;
127
154
return ;
128
155
}
129
156
@@ -154,8 +181,16 @@ function launchEditor(fileName, lineNumber) {
154
181
} else {
155
182
_childProcess = child_process . spawn ( editor , args , { stdio : 'inherit' } ) ;
156
183
}
157
- _childProcess . on ( 'exit' , function ( ) {
184
+ _childProcess . on ( 'exit' , function ( errorCode ) {
158
185
_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 ) ;
159
194
} ) ;
160
195
}
161
196
0 commit comments