-
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced 'read' with high-level 'prompt' lib
/ref #199 This changes the prompt API slightly and adds the following features: - `grunt-init` style coloring - `default` can be a function (in preperation of "magic defaults") - if `default` is a boolean, it will display a 'y/n' prompt with proper validation - validations occurs after a value is entered and not after the whole wizard completed
- Loading branch information
Showing
3 changed files
with
55 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,73 @@ | ||
var read = require('read'); | ||
'use strict'; | ||
|
||
var prompt_ = require('prompt'); | ||
var _ = require('lodash'); | ||
var exports = module.exports; | ||
|
||
|
||
function evaluatePrompts(prompt) { | ||
if (_.isFunction(prompt.default)) { | ||
prompt.default = prompt.default(); | ||
} else if (typeof prompt.default === 'boolean') { | ||
// Handle boolean defaults as confirmation prompts. | ||
var defaultMsg = prompt.default ? 'Y/n' : 'y/N'; | ||
prompt.default = defaultMsg; | ||
|
||
prompt.validator = function (value) { | ||
return value.match(/^([yYnN]|(y\/N)|(Y\/n))$/); | ||
}; | ||
prompt.required = true; | ||
|
||
prompt.before = function (val) { | ||
if (val === 'Y/n' || val.toLowerCase() === 'y') { | ||
return true; | ||
} else if (val === 'y/N' || val.toLowerCase() === 'n') { | ||
return false; | ||
} | ||
|
||
return val; | ||
}; | ||
} | ||
|
||
return prompt; | ||
} | ||
|
||
// Prompt for user input based on the given Array of `prompts` to perform in | ||
// series, and call `done` callback on completion. `prompts` can be a single | ||
// Hash of options in which case a single prompt is performed. | ||
// | ||
// Options can be any read's option: https://github.com/isaacs/read#options | ||
// Options can be any prompt's option: https://npmjs.org/package/prompt | ||
// | ||
// - prompts - A single or an Array of Hash options. | ||
// - done - Callback to call on error or on completion. | ||
// | ||
// Returns the generator instance. | ||
module.exports = function prompt(prompts, done) { | ||
exports.prompt = function prompt(prompts, done) { | ||
prompts = Array.isArray(prompts) ? prompts : [prompts]; | ||
|
||
var results = {}; | ||
(function next(prompt) { | ||
if (!prompt) { | ||
return done(null, results); | ||
} | ||
prompts = prompts.map(evaluatePrompts); | ||
|
||
if (!prompt.prompt) { | ||
prompt.prompt = prompt.message; | ||
} | ||
prompt_.message = '[' + '?'.green + ']'; | ||
prompt_.delimiter = ' '; | ||
prompt_.start(); | ||
|
||
read(prompt, function (err, value) { | ||
var results = {}; | ||
(function next(prompt) { | ||
function handleResult(err, value) { | ||
if (err) { | ||
return done(err); | ||
} | ||
|
||
results[prompt.name] = value; | ||
next(prompts.shift()); | ||
}); | ||
} | ||
|
||
if (!prompt) { | ||
return done(null, results); | ||
} | ||
|
||
prompt_.get(prompt, handleResult); | ||
})(prompts.shift()); | ||
|
||
return this; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters