Skip to content

Commit

Permalink
Allow supplying only the name
Browse files Browse the repository at this point in the history
Allow supplying only the name instead of the entire inquirer Question.
The question is then created with the name as `name`-property.
  • Loading branch information
Myhlamaeus authored and SBoudrias committed Dec 29, 2017
1 parent b56803d commit c41240a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ askName(
).then(function(answer) {
console.log(answer.name);
});

// Equivalent to {name: 'name'}
askName('name', inquirer).then(function(answer) {
console.log(answer.name);
});
```

Inside a **Yeoman Generator** you'd call it this way:
Expand All @@ -60,7 +65,8 @@ module.exports = generators.Base.extend({

`askName` takes 2 parameters:

1. `prompt` an [Inquirer prompt configuration](https://github.com/SBoudrias/Inquirer.js#question).
1. `prompt` an [Inquirer prompt configuration](https://github.com/SBoudrias/Inquirer.js#question)
or just a string to serve as name.
2. `inquirer` or any object with a `obj.prompt()` method.

**Returns:** A `Promise` resolved with the answer object.
Expand Down
13 changes: 10 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ var defaults = {
* This function will use the given prompt config and will then check if the value is
* available as a name on npm. If the name is already picked, we'll ask the user to
* confirm or pick another name.
* @param {Object} prompt Inquirer prompt configuration
* @param {inquirer} inquirer Object with a `prompt` method. Usually `inquirer` or a
* yeoman-generator.
* @param {(Object|String)} prompt Inquirer prompt configuration or the name to be
* used in it.
* @param {inquirer} inquirer Object with a `prompt` method. Usually `inquirer`
or a yeoman-generator.
*/
module.exports = function askName(prompt, inquirer) {
if (typeof prompt === 'string') {
prompt = {
name: prompt
};
}

var prompts = [
Object.assign({}, defaults, prompt, {
validate: function (val) {
Expand Down

0 comments on commit c41240a

Please sign in to comment.