diff --git a/lib/actions/prompt.js b/lib/actions/prompt.js index 43af5d22..e70072d8 100644 --- a/lib/actions/prompt.js +++ b/lib/actions/prompt.js @@ -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; }; diff --git a/lib/util/conflicter.js b/lib/util/conflicter.js index a40d9ce3..5d600b03 100644 --- a/lib/util/conflicter.js +++ b/lib/util/conflicter.js @@ -94,7 +94,7 @@ conflicter._ask = function (filepath, content, cb) { return cb(err); } - var answer = result.overwrite; + var answer = result.overwrite.overwrite; var ok = 'Yynaqdh'.split('').some(function (valid) { return valid === answer; @@ -198,4 +198,4 @@ conflicter.colorLines = function colorLines(name, str) { return str.split('\n').map(function (str) { return conflicter.color(name, str); }).join('\n'); -}; +}; \ No newline at end of file diff --git a/package.json b/package.json index 336415dc..eb83c3c7 100644 --- a/package.json +++ b/package.json @@ -34,12 +34,12 @@ "underscore.string": "~2.3.1", "lodash": "~1.1.1", "mkdirp": "~0.3.5", - "read": "~1.0.4", "glob": "~3.1.21", "nopt": "~2.1.1", "cli-table": "~0.2.0", "debug": "~0.7.2", - "isbinaryfile": "~0.1.8" + "isbinaryfile": "~0.1.8", + "prompt": "~0.2.9" }, "devDependencies": { "mocha": "~1.9.0",