prompt-base
Advanced tools
Comparing version 0.3.2 to 0.4.0
109
index.js
@@ -31,10 +31,19 @@ 'use strict'; | ||
function Prompt(question, answers, ui) { | ||
if (!(this instanceof Prompt)) { | ||
var proto = Object.create(Prompt.prototype); | ||
Prompt.apply(proto, arguments); | ||
return proto; | ||
} | ||
if (!question) { | ||
throw new TypeError('expected question to be a string or object'); | ||
} | ||
this.question = new Question(question); | ||
this.answers = answers || {}; | ||
this.status = 'pending'; | ||
this.session = false; | ||
this.session = true; | ||
this.called = 0; | ||
this.ui = ui; | ||
// Check to make sure prompt requirements are there | ||
if (!isString(this.question.message)) { | ||
@@ -54,9 +63,7 @@ throw new TypeError('expected message to be a string'); | ||
if (typeof this.ui === 'undefined') { | ||
this.ui = new UI(this.options); | ||
this.ui = UI.create(this.options); | ||
} | ||
this.rl = this.ui.rl; | ||
this.resume = this.rl.resume.bind(this.rl); | ||
this.close = this.ui.close.bind(this.ui); | ||
this.pause = this.ui.pause.bind(this.ui); | ||
}; | ||
@@ -135,9 +142,12 @@ | ||
Prompt.prototype.run = function(answers) { | ||
if (this.called) this.resume(); | ||
this.resume(); | ||
var name = this.question.name; | ||
var when = this.when(answers); | ||
var ask = when ? this.ask.bind(this) : this.noop; | ||
answers = answers || {}; | ||
return new Promise(function(resolve) { | ||
ask(function(value) { | ||
answers[name] = value; | ||
resolve(value); | ||
@@ -158,3 +168,8 @@ }); | ||
Prompt.prototype.render = function() { | ||
this.ui.render(this.message + this.rl.line); | ||
var message = this.message; | ||
var answer = this.status === 'answered' | ||
? log.cyan(this.answer) | ||
: this.rl.line; | ||
this.ui.render(message + answer); | ||
}; | ||
@@ -169,5 +184,3 @@ | ||
Prompt.prototype.onSubmit = function(input) { | ||
this.status = 'answered'; | ||
this.answer = input; | ||
this.submitAnswer(); | ||
this.submitAnswer(input); | ||
}; | ||
@@ -180,8 +193,7 @@ | ||
Prompt.prototype.submitAnswer = function() { | ||
this.render(); | ||
this.ui.write(); | ||
Prompt.prototype.submitAnswer = function(input) { | ||
this.status = 'answered'; | ||
this.answer = input; | ||
this.end(); | ||
this.emit('answer', this.answer); | ||
if (!this.session) this.pause(); | ||
this.called++; | ||
this.callback(this.answer); | ||
@@ -205,2 +217,37 @@ }; | ||
/** | ||
* Pause readline | ||
*/ | ||
Prompt.prototype.end = function() { | ||
this.render(); | ||
this.ui.end(); | ||
this.pause(); | ||
}; | ||
/** | ||
* Pause readline | ||
*/ | ||
Prompt.prototype.pause = function() { | ||
this.rl.pause(); | ||
}; | ||
/** | ||
* Resume readline | ||
*/ | ||
Prompt.prototype.resume = function() { | ||
this.status = 'pending'; | ||
this.rl.resume(); | ||
}; | ||
/** | ||
* Close readline | ||
*/ | ||
Prompt.prototype.close = function() { | ||
this.ui.close(); | ||
}; | ||
/** | ||
* Used if `when` returns false | ||
@@ -264,2 +311,34 @@ */ | ||
/** | ||
* Getter that gets the `readline-ui`. If a `ui` instance is not | ||
* | ||
* @name .choices | ||
* @return {Object} Choices object | ||
* @api public | ||
*/ | ||
// Object.defineProperty(Prompt.prototype, 'ui', { | ||
// set: function(ui) { | ||
// define(this, '_ui', ui); | ||
// }, | ||
// get: function() { | ||
// if (typeof this._ui === 'undefined') { | ||
// define(this, '_ui', UI.create(this.options)); | ||
// } | ||
// return this._ui; | ||
// } | ||
// }); | ||
// Object.defineProperty(Prompt.prototype, 'rl', { | ||
// set: function(rl) { | ||
// define(this, '_rl', rl); | ||
// }, | ||
// get: function() { | ||
// if (typeof this._rl === 'undefined') { | ||
// define(this, '_rl', this.ui.rl); | ||
// } | ||
// return this._rl; | ||
// } | ||
// }); | ||
/** | ||
* Utils | ||
@@ -266,0 +345,0 @@ */ |
{ | ||
"name": "prompt-base", | ||
"description": "Base prompt module used for creating custom prompt types for Enquirer.", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/enquirer/prompt-base", | ||
@@ -32,3 +32,4 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"devDependencies": { | ||
"gulp-format-md": "^0.1.10" | ||
"gulp-format-md": "^0.1.10", | ||
"mocha": "^3.0.2" | ||
}, | ||
@@ -35,0 +36,0 @@ "keywords": [ |
@@ -1,2 +0,2 @@ | ||
# prompt-base [![NPM version](https://img.shields.io/npm/v/prompt-base.svg?style=flat)](https://www.npmjs.com/package/prompt-base) [![NPM downloads](https://img.shields.io/npm/dm/prompt-base.svg?style=flat)](https://npmjs.org/package/prompt-base) | ||
# prompt-base [![NPM version](https://img.shields.io/npm/v/prompt-base.svg?style=flat)](https://www.npmjs.com/package/prompt-base) [![NPM downloads](https://img.shields.io/npm/dm/prompt-base.svg?style=flat)](https://npmjs.org/package/prompt-base) [![Build Status](https://img.shields.io/travis/enquirer/prompt-base.svg?style=flat)](https://travis-ci.org/enquirer/prompt-base) | ||
@@ -17,2 +17,11 @@ > Base prompt module used for creating custom prompt types for Enquirer. | ||
var Prompt = require('prompt-base'); | ||
var prompt = new Prompt({ | ||
name: 'color', | ||
message: 'What is your favorite color?' | ||
}); | ||
prompt.run() | ||
.then(function(answer) { | ||
console.log(answer); | ||
}) | ||
``` | ||
@@ -46,3 +55,3 @@ | ||
### [.run](index.js#L132) | ||
### [.run](index.js#L139) | ||
@@ -57,3 +66,3 @@ Initialize a prompt and resolve answers. If `question.when` returns false, | ||
### [.render](index.js#L154) | ||
### [.render](index.js#L164) | ||
@@ -68,3 +77,3 @@ Render the current prompt input. This can be replaced by custom prompts. | ||
### [.format](index.js#L190) | ||
### [.format](index.js#L202) | ||
@@ -75,3 +84,3 @@ Returns a formatted prompt message. | ||
### [.choices](index.js#L212) | ||
### [.choices](index.js#L259) | ||
@@ -82,3 +91,3 @@ Getter for getting the choices array from the question. | ||
### [.message](index.js#L229) | ||
### [.message](index.js#L276) | ||
@@ -89,3 +98,3 @@ Getter that returns `question.message` after passing it to [format](#format). | ||
### [.prefix](index.js#L247) | ||
### [.prefix](index.js#L294) | ||
@@ -97,2 +106,8 @@ Getter that returns the prefix to use before `question.message`. The | ||
### [.choices](index.js#L311) | ||
Getter that gets the `readline-ui`. If a `ui` instance is not | ||
* `returns` **{Object}**: Choices object | ||
## Examples | ||
@@ -147,2 +162,3 @@ | ||
* [enquirer-prompt-radio](https://www.npmjs.com/package/enquirer-prompt-radio): Adds `radio` prompt support to Enquirer. This prompt behaves like other radio-button interfaces, where only… [more](https://github.com/enquirer/enquirer-prompt-radio) | [homepage](https://github.com/enquirer/enquirer-prompt-radio "Adds `radio` prompt support to Enquirer. This prompt behaves like other radio-button interfaces, where only one choice is enabled whilst all others are disabled.") | ||
* [enquirer-prompt-rawlist](https://www.npmjs.com/package/enquirer-prompt-rawlist): Adds `rawlist` prompt support to [Enquirer](https://github.com/jonschlinkert/enquirer). | [homepage](https://github.com/enquirer/enquirer-prompt-rawlist "Adds `rawlist` prompt support to [Enquirer].") | ||
@@ -162,3 +178,3 @@ ## About | ||
Please read the [contributing guide](contributing.md) for avice on opening issues, pull requests, and coding standards. | ||
Please read the [contributing guide](.github/contributing.md) for avice on opening issues, pull requests, and coding standards. | ||
@@ -197,2 +213,2 @@ ### Building docs | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.30, on August 30, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.30, on September 01, 2016._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18278
293
203
2