Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active December 23, 2015 00:09
Show Gist options
  • Save cowboy/6551901 to your computer and use it in GitHub Desktop.
Save cowboy/6551901 to your computer and use it in GitHub Desktop.
grunt-init y/n question helper.
[
{
"name": "yes",
"message": "This question will default to yes (true).",
"default": true
},
{
"name": "no",
"message": "This question will default to no (false).",
"default": false
}
]
'use strict';
// Basic template description.
exports.description = 'Foo.';
// Template-specific notes to be displayed before question prompts.
exports.notes = 'Blah blah blah.';
// The actual init template.
exports.template = function(grunt, init, done) {
// Helper to create a "yes/no" question.
function yn(o) {
o = grunt.util._.defaults({}, o, {
default: true,
warning: 'You must answer [y]es or [n]o.',
});
var defaultYes = o.default === true || String(o.default).toLowerCase() === 'y' || String(o.default)[0] === 'Y';
o.default = defaultYes ? 'Y/n' : 'y/N';
o.sanitize = function(value, data, done) {
data[o.name] = defaultYes ? /y/i.test(value) : !/n/i.test(value);
done();
};
return o;
}
// Helper to process an array of "yes/no" questions.
function yns(o) {
return o.map(yn);
}
// Prompt for these values.
init.process({}, yns(require('./questions.json')), function(err, props) {
console.log(props);
done();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment