|
24 | 24 |
|
25 | 25 | var resolve = require( 'path' ).resolve;
|
26 | 26 | var ReadStream = require( 'tty' ).ReadStream;
|
27 |
| -var open = require( '@stdlib/fs/open' ); // eslint-disable-line stdlib/no-redeclare |
| 27 | +var openSync = require( '@stdlib/fs/open' ).sync; |
| 28 | +var closeSync = require( '@stdlib/fs/close' ).sync; |
28 | 29 | var readFileSync = require( '@stdlib/fs/read-file' ).sync;
|
29 | 30 | var CLI = require( '@stdlib/tools/cli' );
|
30 | 31 | var stdin = require( '@stdlib/process/read-stdin' );
|
@@ -176,30 +177,35 @@ function main() {
|
176 | 177 | * @returns {void}
|
177 | 178 | */
|
178 | 179 | function onRead( error, text ) {
|
| 180 | + var repl; |
| 181 | + var pres; |
| 182 | + var fd; |
179 | 183 | if ( error ) {
|
180 | 184 | return cli.error( error );
|
181 | 185 | }
|
182 | 186 | // Because we received input over `stdin`, we need to create a new TTY `stdin` read stream for receiving interactive user input...
|
183 |
| - open( '/dev/tty', 'r', onOpen ); |
| 187 | + fd = openSync( '/dev/tty', 'r' ); |
| 188 | + if ( fd instanceof Error ) { |
| 189 | + return cli.error( error ); |
| 190 | + } |
| 191 | + opts1.input = new ReadStream( fd ); // note: instance of net.Socket |
| 192 | + |
| 193 | + // Create a new REPL instance: |
| 194 | + repl = new REPL( opts1 ); |
| 195 | + repl.once( 'exit', onExit ); |
| 196 | + |
| 197 | + // Create a new REPL presentation: |
| 198 | + pres = new Presentation( text.toString(), repl, opts2 ); |
| 199 | + pres.show(); |
184 | 200 |
|
185 | 201 | /**
|
186 |
| - * Callback invoked upon opening a file. |
| 202 | + * Callback invoked upon exiting a REPL. |
187 | 203 | *
|
188 | 204 | * @private
|
189 |
| - * @param {(Error|null)} error - error object |
190 |
| - * @param {integer} fd - file descriptor |
191 |
| - * @returns {void} |
192 | 205 | */
|
193 |
| - function onOpen( error, fd ) { |
194 |
| - var repl; |
195 |
| - var pres; |
196 |
| - if ( error ) { |
197 |
| - return cli.error( error ); |
198 |
| - } |
199 |
| - opts1.input = new ReadStream( fd ); |
200 |
| - repl = new REPL( opts1 ); |
201 |
| - pres = new Presentation( text.toString(), repl, opts2 ); |
202 |
| - pres.show(); |
| 206 | + function onExit() { |
| 207 | + opts1.input.end(); // close socket to prevent the process from hanging (see https://github.com/nodejs/node-v0.x-archive/issues/7101) |
| 208 | + closeSync( fd ); |
203 | 209 | }
|
204 | 210 | }
|
205 | 211 | }
|
|
0 commit comments