diff options
Diffstat (limited to 'tools/node_modules/eslint/node_modules/inquirer/lib/ui')
3 files changed, 296 insertions, 0 deletions
diff --git a/tools/node_modules/eslint/node_modules/inquirer/lib/ui/baseUI.js b/tools/node_modules/eslint/node_modules/inquirer/lib/ui/baseUI.js new file mode 100644 index 0000000000..98bb9a2897 --- /dev/null +++ b/tools/node_modules/eslint/node_modules/inquirer/lib/ui/baseUI.js @@ -0,0 +1,75 @@ +'use strict'; +var _ = require('lodash'); +var MuteStream = require('mute-stream'); +var readline = require('readline'); + +/** + * Base interface class other can inherits from + */ + +var UI = module.exports = function (opt) { + // Instantiate the Readline interface + // @Note: Don't reassign if already present (allow test to override the Stream) + if (!this.rl) { + this.rl = readline.createInterface(setupReadlineOptions(opt)); + } + this.rl.resume(); + + this.onForceClose = this.onForceClose.bind(this); + + // Make sure new prompt start on a newline when closing + process.on('exit', this.onForceClose); + + // Terminate process on SIGINT (which will call process.on('exit') in return) + this.rl.on('SIGINT', this.onForceClose); +}; + +/** + * Handle the ^C exit + * @return {null} + */ + +UI.prototype.onForceClose = function () { + this.close(); + process.kill(process.pid, 'SIGINT'); + console.log(''); +}; + +/** + * Close the interface and cleanup listeners + */ + +UI.prototype.close = function () { + // Remove events listeners + this.rl.removeListener('SIGINT', this.onForceClose); + process.removeListener('exit', this.onForceClose); + + this.rl.output.unmute(); + + if (this.activePrompt && typeof this.activePrompt.close === 'function') { + this.activePrompt.close(); + } + + // Close the readline + this.rl.output.end(); + this.rl.pause(); + this.rl.close(); +}; + +function setupReadlineOptions(opt) { + opt = opt || {}; + + // Default `input` to stdin + var input = opt.input || process.stdin; + + // Add mute capabilities to the output + var ms = new MuteStream(); + ms.pipe(opt.output || process.stdout); + var output = ms; + + return _.extend({ + terminal: true, + input: input, + output: output + }, _.omit(opt, ['input', 'output'])); +} diff --git a/tools/node_modules/eslint/node_modules/inquirer/lib/ui/bottom-bar.js b/tools/node_modules/eslint/node_modules/inquirer/lib/ui/bottom-bar.js new file mode 100644 index 0000000000..90f9b60d98 --- /dev/null +++ b/tools/node_modules/eslint/node_modules/inquirer/lib/ui/bottom-bar.js @@ -0,0 +1,106 @@ +/** + * Sticky bottom bar user interface + */ + +var util = require('util'); +var through = require('through'); +var Base = require('./baseUI'); +var rlUtils = require('../utils/readline'); +var _ = require('lodash'); + +/** + * Module exports + */ + +module.exports = Prompt; + +/** + * Constructor + */ + +function Prompt(opt) { + opt || (opt = {}); + + Base.apply(this, arguments); + + this.log = through(this.writeLog.bind(this)); + this.bottomBar = opt.bottomBar || ''; + this.render(); +} +util.inherits(Prompt, Base); + +/** + * Render the prompt to screen + * @return {Prompt} self + */ + +Prompt.prototype.render = function () { + this.write(this.bottomBar); + return this; +}; + +Prompt.prototype.clean = function () { + rlUtils.clearLine(this.rl, this.bottomBar.split('\n').length); + return this; +}; + +/** + * Update the bottom bar content and rerender + * @param {String} bottomBar Bottom bar content + * @return {Prompt} self + */ + +Prompt.prototype.updateBottomBar = function (bottomBar) { + rlUtils.clearLine(this.rl, 1); + this.rl.output.unmute(); + this.clean(); + this.bottomBar = bottomBar; + this.render(); + this.rl.output.mute(); + return this; +}; + +/** + * Write out log data + * @param {String} data - The log data to be output + * @return {Prompt} self + */ + +Prompt.prototype.writeLog = function (data) { + this.rl.output.unmute(); + this.clean(); + this.rl.output.write(this.enforceLF(data.toString())); + this.render(); + this.rl.output.mute(); + return this; +}; + +/** + * Make sure line end on a line feed + * @param {String} str Input string + * @return {String} The input string with a final line feed + */ + +Prompt.prototype.enforceLF = function (str) { + return str.match(/[\r\n]$/) ? str : str + '\n'; +}; + +/** + * Helper for writing message in Prompt + * @param {Prompt} prompt - The Prompt object that extends tty + * @param {String} message - The message to be output + */ +Prompt.prototype.write = function (message) { + var msgLines = message.split(/\n/); + this.height = msgLines.length; + + // Write message to screen and setPrompt to control backspace + this.rl.setPrompt(_.last(msgLines)); + + if (this.rl.output.rows === 0 && this.rl.output.columns === 0) { + /* When it's a tty through serial port there's no terminal info and the render will malfunction, + so we need enforce the cursor to locate to the leftmost position for rendering. */ + rlUtils.left(this.rl, message.length + this.rl.line.length); + } + this.rl.output.write(message); +}; diff --git a/tools/node_modules/eslint/node_modules/inquirer/lib/ui/prompt.js b/tools/node_modules/eslint/node_modules/inquirer/lib/ui/prompt.js new file mode 100644 index 0000000000..9b56fe4dfe --- /dev/null +++ b/tools/node_modules/eslint/node_modules/inquirer/lib/ui/prompt.js @@ -0,0 +1,115 @@ +'use strict'; +var _ = require('lodash'); +var rx = require('rx-lite-aggregates'); +var util = require('util'); +var runAsync = require('run-async'); +var utils = require('../utils/utils'); +var Base = require('./baseUI'); + +/** + * Base interface class other can inherits from + */ + +var PromptUI = module.exports = function (prompts, opt) { + Base.call(this, opt); + this.prompts = prompts; +}; +util.inherits(PromptUI, Base); + +PromptUI.prototype.run = function (questions) { + // Keep global reference to the answers + this.answers = {}; + + // Make sure questions is an array. + if (_.isPlainObject(questions)) { + questions = [questions]; + } + + // Create an observable, unless we received one as parameter. + // Note: As this is a public interface, we cannot do an instanceof check as we won't + // be using the exact same object in memory. + var obs = _.isArray(questions) ? rx.Observable.from(questions) : questions; + + this.process = obs + .concatMap(this.processQuestion.bind(this)) + // `publish` creates a hot Observable. It prevents duplicating prompts. + .publish(); + + this.process.connect(); + + return this.process + .reduce(function (answers, answer) { + _.set(this.answers, answer.name, answer.answer); + return this.answers; + }.bind(this), {}) + .toPromise(Promise) + .then(this.onCompletion.bind(this)); +}; + +/** + * Once all prompt are over + */ + +PromptUI.prototype.onCompletion = function (answers) { + this.close(); + + return answers; +}; + +PromptUI.prototype.processQuestion = function (question) { + question = _.clone(question); + return rx.Observable.defer(function () { + var obs = rx.Observable.of(question); + + return obs + .concatMap(this.setDefaultType.bind(this)) + .concatMap(this.filterIfRunnable.bind(this)) + .concatMap(utils.fetchAsyncQuestionProperty.bind(null, question, 'message', this.answers)) + .concatMap(utils.fetchAsyncQuestionProperty.bind(null, question, 'default', this.answers)) + .concatMap(utils.fetchAsyncQuestionProperty.bind(null, question, 'choices', this.answers)) + .concatMap(this.fetchAnswer.bind(this)); + }.bind(this)); +}; + +PromptUI.prototype.fetchAnswer = function (question) { + var Prompt = this.prompts[question.type]; + this.activePrompt = new Prompt(question, this.rl, this.answers); + return rx.Observable.defer(function () { + return rx.Observable.fromPromise(this.activePrompt.run().then(function (answer) { + return {name: question.name, answer: answer}; + })); + }.bind(this)); +}; + +PromptUI.prototype.setDefaultType = function (question) { + // Default type to input + if (!this.prompts[question.type]) { + question.type = 'input'; + } + return rx.Observable.defer(function () { + return rx.Observable.return(question); + }); +}; + +PromptUI.prototype.filterIfRunnable = function (question) { + if (question.when === false) { + return rx.Observable.empty(); + } + + if (!_.isFunction(question.when)) { + return rx.Observable.return(question); + } + + var answers = this.answers; + return rx.Observable.defer(function () { + return rx.Observable.fromPromise( + runAsync(question.when)(answers).then(function (shouldRun) { + if (shouldRun) { + return question; + } + }) + ).filter(function (val) { + return val != null; + }); + }); +}; |