base-questions
Advanced tools
Comparing version 0.2.6 to 0.3.0
297
index.js
@@ -13,171 +13,146 @@ /*! | ||
module.exports = function(options) { | ||
utils.forceExit(); | ||
return function(app) { | ||
var store = (app.base && app.base.store) || app.store || {}; | ||
if (this.isRegistered('base-questions')) return; | ||
if (app.hasQuestions) return; | ||
app.define('hasQuestions', true); | ||
var opts = utils.merge({}, this.options, options); | ||
var setQuestions = false; | ||
function updateOpts() { | ||
options = utils.merge({}, app.options.questions, options); | ||
} | ||
/** | ||
* Load questions to ask. Answers are passed to templates as context. | ||
*/ | ||
updateOpts(); | ||
utils.forceExit(); | ||
var Questions = utils.questions; | ||
var questions = new Questions(options); | ||
var opts = questions.options; | ||
function lazyQuestions(app) { | ||
if (setQuestions) return; | ||
setQuestions = true; | ||
opts.questions = utils.commonQuestions(opts.questions); | ||
for (var key in opts.questions) { | ||
app.questions.visit(key, opts.questions[key]); | ||
} | ||
delete opts.questions; | ||
// force all questions to be asked when requested by the user | ||
if (opts.init === true || opts.force === true) { | ||
opts.forceAll = true; | ||
} | ||
app.questions.on('ask', function(key, question, answers) { | ||
if (isForced(key, opts)) { | ||
question.force(); | ||
return; | ||
} | ||
var answer = app.data(key) || app.store.get(key); | ||
if (typeof answer !== 'undefined') { | ||
question.answer.set(answer); | ||
} | ||
}); | ||
} | ||
/** | ||
* Pre-populate answers with data from `app.store.data` and | ||
* `app.cache.data` (`app.store.data` is persisted to the file | ||
* system, and `app.cache.data` is in-memory) | ||
*/ | ||
// decorate the `questions` instance onto `app` | ||
this.define('questions', { | ||
set: function(val) { | ||
this.define('questions', val); | ||
}, | ||
get: function fn() { | ||
if (this._questions) return this._questions; | ||
var Questions = utils.questions; | ||
var questions = new Questions(opts); | ||
this.define('_questions', questions); | ||
lazyQuestions(this); | ||
return questions; | ||
} | ||
}); | ||
// listen for `ask` event and attempt to set the default | ||
// value using stored data, before the question is asked | ||
questions.on('ask', function(key, question, answers) { | ||
updateOpts(); | ||
/** | ||
* Create a "choices" question from an array. | ||
* | ||
* ```js | ||
* app.choices('foo', ['a', 'b', 'c']); | ||
* // or | ||
* app.choices('foo', { | ||
* message: 'Favorite letter?', | ||
* choices: ['a', 'b', 'c'] | ||
* }); | ||
* // then | ||
* app.ask('foo', function(err, answer) { | ||
* console.log(answer); | ||
* }); | ||
* ``` | ||
* @name .choices | ||
* @param {String|Array} `queue` Name or array of question names. | ||
* @param {Object|Function} `options` Question options or callback function | ||
* @param {Function} `callback` callback function | ||
* @api public | ||
*/ | ||
var ctx = utils.merge({}, app.store.data, app.cache.data); | ||
if (app.env && app.env.user) { | ||
ctx = utils.merge({}, ctx, app.env.user.pkg); | ||
} | ||
this.define('choices', function() { | ||
lazyQuestions(this); | ||
var args = [].slice.call(arguments); | ||
var cb = args.pop(); | ||
var question = utils.toChoices.apply(null, args); | ||
questions.setData(ctx); | ||
// don't save answers for choice questions | ||
// unless explicitly defined by the user | ||
if (!question.hasOwnProperty('save')) { | ||
question.save = false; | ||
} | ||
var init = options.init || options.force; | ||
if (init && typeof init !== 'boolean' && isMatch(key, init)) { | ||
question.options.force = true; | ||
return; | ||
} | ||
this.questions.set(question.name, question); | ||
return this.ask(question.name, cb); | ||
}); | ||
if (init === true) { | ||
question.options.force = true; | ||
return; | ||
} | ||
var answer = utils.get(app.cache, ['expanded', key]); | ||
if (answer) { | ||
question.answer.set(answer); | ||
return; | ||
} | ||
answer = utils.get(ctx, key); | ||
if (answer) { | ||
question.answer.set(answer); | ||
return; | ||
} | ||
if (!question.isAnswered(options.locale)) { | ||
questions.options.force = true; | ||
} | ||
}); | ||
// decorate the `questions` instance onto `app` | ||
app.define('questions', questions); | ||
/** | ||
* Create a "choices" question from an array. | ||
* | ||
* ```js | ||
* app.choices('foo', ['a', 'b', 'c']); | ||
* // or | ||
* app.choices('foo', { | ||
* message: 'Favorite letter?', | ||
* choices: ['a', 'b', 'c'] | ||
* }); | ||
* // then | ||
* app.ask('foo', function(err, answer) { | ||
* console.log(answer); | ||
* }); | ||
* ``` | ||
* @name .choices | ||
* @param {String|Array} `queue` Name or array of question names. | ||
* @param {Object|Function} `options` Question options or callback function | ||
* @param {Function} `callback` callback function | ||
* @api public | ||
*/ | ||
app.define('choices', function() { | ||
var args = [].slice.call(arguments); | ||
var cb = args.pop(); | ||
var question = utils.toChoices.apply(null, args); | ||
// don't save answers for choice questions unless | ||
// explicitly defined by the user | ||
if (!question.hasOwnProperty('save')) { | ||
question.save = false; | ||
} | ||
app.questions.set(question.name, question); | ||
return this.ask(question.name, cb); | ||
}); | ||
/** | ||
* Add a question to be asked at a later point. | ||
* | ||
* ```js | ||
* app.question('beverage', 'What is your favorite beverage?'); | ||
* // or | ||
* app.question('beverage', { | ||
* type: 'input', | ||
* message: 'What is your favorite beverage?' | ||
* }); | ||
* // or | ||
* app.question({ | ||
* name: 'beverage' | ||
* type: 'input', | ||
* message: 'What is your favorite beverage?' | ||
* }); | ||
* ``` | ||
* @name .question | ||
* @param {Object|String} `value` Question object, message (string), or options object. | ||
/** | ||
* Add a question to be asked at a later point. | ||
* | ||
* ```js | ||
* app.question('beverage', 'What is your favorite beverage?'); | ||
* // or | ||
* app.question('beverage', { | ||
* type: 'input', | ||
* message: 'What is your favorite beverage?' | ||
* }); | ||
* // or | ||
* app.question({ | ||
* name: 'beverage' | ||
* type: 'input', | ||
* message: 'What is your favorite beverage?' | ||
* }); | ||
* ``` | ||
* @name .question | ||
* @param {Object|String} `value` Question object, message (string), or options object. | ||
* @param {String} `locale` Optionally pass the locale to use, otherwise the default locale is used. | ||
* @return {Object} Returns the `app.questions` object, for chaining | ||
* @api public | ||
*/ | ||
* @return {Object} Returns the `this.questions` object, for chaining | ||
* @api public | ||
*/ | ||
app.define('question', questions.set.bind(questions)); | ||
this.define('question', function() { | ||
lazyQuestions(this); | ||
return this.questions.set.apply(this.questions, arguments); | ||
}); | ||
/** | ||
* Load questions to ask. Answers are passed to templates as context. | ||
*/ | ||
/** | ||
* Ask one or more questions, with the given `options` and callback. | ||
* | ||
* ```js | ||
* // ask all questions | ||
* app.ask(function(err, answers) { | ||
* console.log(answers); | ||
* }); | ||
* | ||
* // ask the specified questions | ||
* app.ask(['name', 'description'], function(err, answers) { | ||
* console.log(answers); | ||
* }); | ||
* ``` | ||
* @name .ask | ||
* @param {String|Array} `queue` Name or array of question names. | ||
* @param {Object|Function} `options` Question options or callback function | ||
* @param {Function} `callback` callback function | ||
* @api public | ||
*/ | ||
opts.questions = utils.commonQuestions(opts.questions); | ||
for (var key in opts.questions) { | ||
app.questions.visit(key, opts.questions[key]); | ||
} | ||
delete opts.questions; | ||
/** | ||
* Ask one or more questions, with the given `options` and callback. | ||
* | ||
* ```js | ||
* // ask all questions | ||
* app.ask(function(err, answers) { | ||
* console.log(answers); | ||
* }); | ||
* | ||
* // ask the specified questions | ||
* app.ask(['name', 'description'], function(err, answers) { | ||
* console.log(answers); | ||
* }); | ||
* ``` | ||
* @name .ask | ||
* @param {String|Array} `queue` Name or array of question names. | ||
* @param {Object|Function} `options` Question options or callback function | ||
* @param {Function} `callback` callback function | ||
* @api public | ||
*/ | ||
app.define('ask', function(queue, opts, cb) { | ||
if (typeof queue === 'string' && !questions.has(queue)) { | ||
questions.set(queue, {force: true}, queue); | ||
} | ||
questions.ask(queue, opts, cb); | ||
}); | ||
this.define('ask', function(queue, opts, cb) { | ||
lazyQuestions(this); | ||
if (typeof queue === 'string' && !this.questions.has(queue)) { | ||
this.questions.set(queue, {force: true}, queue); | ||
} | ||
this.questions.ask(queue, opts, cb); | ||
}); | ||
}; | ||
@@ -197,1 +172,17 @@ }; | ||
} | ||
function isForced(key, options) { | ||
var opts = utils.merge({}, options); | ||
if (utils.isValidGlob(opts.force)) { | ||
return isMatch(key, opts.force); | ||
} | ||
if (utils.isValidGlob(opts.init)) { | ||
return isMatch(key, opts.init); | ||
} | ||
if (opts.init === true) { | ||
return true; | ||
} | ||
if (opts.force === true) { | ||
return true; | ||
} | ||
} |
{ | ||
"name": "base-questions", | ||
"description": "Plugin for base-methods that adds methods for prompting the user and storing the answers on a project-by-project basis.", | ||
"version": "0.2.6", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/jonschlinkert/base-questions", | ||
@@ -24,10 +24,11 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"dependencies": { | ||
"common-questions": "^0.1.1", | ||
"common-questions": "^0.1.2", | ||
"for-own": "^0.1.3", | ||
"get-value": "^2.0.2", | ||
"get-value": "^2.0.3", | ||
"is-valid-glob": "^0.3.0", | ||
"lazy-cache": "^1.0.3", | ||
"micromatch": "^2.3.7", | ||
"mixin-deep": "^1.1.3", | ||
"question-store": "^0.4.2", | ||
"set-value": "^0.3.2", | ||
"question-store": "^0.6.0", | ||
"set-value": "^0.3.3", | ||
"to-choices": "^0.1.1" | ||
@@ -37,9 +38,9 @@ }, | ||
"arr-union": "^3.0.0", | ||
"assemble-core": "^0.8.1", | ||
"base": "^0.6.3", | ||
"assemble-core": "^0.10.0", | ||
"base": "^0.6.7", | ||
"base-argv": "^0.3.0", | ||
"base-config": "^0.3.3", | ||
"base-data": "^0.3.5", | ||
"base-options": "^0.5.4", | ||
"base-store": "^0.3.2", | ||
"base-config": "^0.3.6", | ||
"base-data": "^0.3.6", | ||
"base-options": "^0.5.5", | ||
"base-store": "^0.3.4", | ||
"gulp": "^3.9.0", | ||
@@ -52,3 +53,3 @@ "gulp-eslint": "^1.1.1", | ||
"minimist": "^1.2.0", | ||
"mocha": "*" | ||
"mocha": "^2.4.4" | ||
}, | ||
@@ -55,0 +56,0 @@ "keywords": [ |
@@ -80,3 +80,3 @@ # base-questions [![NPM version](https://img.shields.io/npm/v/base-questions.svg)](https://www.npmjs.com/package/base-questions) [![Build Status](https://img.shields.io/travis/jonschlinkert/base-questions.svg)](https://travis-ci.org/jonschlinkert/base-questions) | ||
### [.choices](index.js#L104) | ||
### [.choices](index.js#L105) | ||
@@ -106,3 +106,3 @@ Create a "choices" question from an array. | ||
### [.question](index.js#L141) | ||
### [.question](index.js#L142) | ||
@@ -134,3 +134,3 @@ Add a question to be asked at a later point. | ||
### [.ask](index.js#L174) | ||
### [.ask](index.js#L175) | ||
@@ -137,0 +137,0 @@ Ask one or more questions, with the given `options` and callback. |
10
utils.js
@@ -15,10 +15,11 @@ 'use strict'; | ||
require('question-store', 'questions'); | ||
require('mixin-deep', 'merge'); | ||
require('common-questions'); | ||
require('for-own'); | ||
require('get-value', 'get'); | ||
require('is-valid-glob'); | ||
require('micromatch', 'mm'); | ||
require('mixin-deep', 'merge'); | ||
require('question-store', 'questions'); | ||
require('set-value', 'set'); | ||
require('get-value', 'get'); | ||
require('to-choices'); | ||
require('for-own'); | ||
require = fn; | ||
@@ -35,2 +36,3 @@ | ||
stdin.setEncoding('utf8'); | ||
stdin.setMaxListeners(0); | ||
stdin.on('data', function(key) { | ||
@@ -37,0 +39,0 @@ if (key === '\u0003') { |
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
14103
10
+ Addedis-valid-glob@^0.3.0
+ Addedanswer-store@0.3.1(transitive)
+ Addedis-valid-glob@0.3.0(transitive)
+ Addedquestion-store@0.6.5(transitive)
- Removedanswer-store@0.2.1(transitive)
- Removedquestion-store@0.4.2(transitive)
Updatedcommon-questions@^0.1.2
Updatedget-value@^2.0.3
Updatedquestion-store@^0.6.0
Updatedset-value@^0.3.3