3 extend: require('lodash/extend'),
4 omit: require('lodash/omit'),
6 var MuteStream = require('mute-stream');
7 var readline = require('readline');
10 * Base interface class other can inherits from
15 // Instantiate the Readline interface
16 // @Note: Don't reassign if already present (allow test to override the Stream)
18 this.rl = readline.createInterface(setupReadlineOptions(opt));
23 this.onForceClose = this.onForceClose.bind(this);
25 // Make sure new prompt start on a newline when closing
26 process.on('exit', this.onForceClose);
28 // Terminate process on SIGINT (which will call process.on('exit') in return)
29 this.rl.on('SIGINT', this.onForceClose);
39 process.kill(process.pid, 'SIGINT');
44 * Close the interface and cleanup listeners
48 // Remove events listeners
49 this.rl.removeListener('SIGINT', this.onForceClose);
50 process.removeListener('exit', this.onForceClose);
52 this.rl.output.unmute();
54 if (this.activePrompt && typeof this.activePrompt.close === 'function') {
55 this.activePrompt.close();
65 function setupReadlineOptions(opt) {
68 // opt.skipTTYChecks = opt.skipTTYChecks === undefined ? opt.input !== undefined : opt.skipTTYChecks;
69 opt.skipTTYChecks = opt.skipTTYChecks === undefined ? true : opt.skipTTYChecks;
71 // Default `input` to stdin
72 var input = opt.input || process.stdin;
74 // Check if prompt is being called in TTY environment
75 // If it isn't return a failed promise
76 if (!opt.skipTTYChecks && !input.isTTY) {
77 const nonTtyError = new Error(
78 'Prompts can not be meaningfully rendered in non-TTY environments'
80 nonTtyError.isTtyError = true;
84 // Add mute capabilities to the output
85 var ms = new MuteStream();
86 ms.pipe(opt.output || process.stdout);
95 _.omit(opt, ['input', 'output'])