Skip to content

Commit

Permalink
Replaced 'read' with high-level 'prompt' lib
Browse files Browse the repository at this point in the history
/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
passy committed Apr 23, 2013
1 parent 0486c2c commit f12b33e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
64 changes: 51 additions & 13 deletions lib/actions/prompt.js
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;
};
4 changes: 2 additions & 2 deletions lib/util/conflicter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -198,4 +198,4 @@ conflicter.colorLines = function colorLines(name, str) {
return str.split('\n').map(function (str) {
return conflicter.color(name, str);
}).join('\n');
};
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f12b33e

Please sign in to comment.