Template helper that asks a question in the command line and resolves the template with the answer.
Inspired by conversations with Jon Schlinkert
Install with npm
$ npm i question-helper --save
var question = require('question-helper');
// Prompt a user for a question and get an answer back.
question("What's your name?", function (err, answer) {
console.log(chalk.green("Hi %s!"), answer);
});
Params
key {String}: Either a key on a questions object on the context or a question to ask.options {String}: Additional options to pass.callback {Function}: Callback function that will be passed an error and/or the results of asking the question.Pass a questions object, where the value of each property is a question to ask:
var context = {
questions: {
name: 'What is your name?',
description: 'Project description?',
author: {
name: 'Author name?',
url: 'Author url?',
}
}
};
// Q: 'What is your name?'
question('name', context, function (err, answer) {
if (err) console.log(err);
// A: 'Jon'
});
// Q: 'Project description?'
question('description', context, function (err, answer) {
if (err) console.log(err);
// A: 'Template helper that asks a question...'
});
// Q: 'Author name?'
question('author.name', context, function (err, answer) {
if (err) console.log(err);
// A: 'Brian Woodward'
});
To use with template:
npm i template --save
Then add to your project.
var template = require('template');
handebars
Register handlebars as an engine:
template.engine('hbs', require('engine-handlebars'));
var question = require('question-helper');
var context = {questions: {name: "What's your name?"}};
template.page('author.hbs', "Author: {{question 'name'}}");
template.render('author.hbs', context, function (err, content) {
if (err) return console.log('error', err);
console.log(content);
});
Lo-Dash
Register Lo-Dash as an engine:
template.engine('html', require('engine-lodash'));
var context = {questions: {name: "What's your name?"}};
template.page('author.html', "Author: <%= question('name') %>");
template.render('author.html', context, function (err, content) {
if (err) return console.log('error', err);
console.log(content);
});
Install dev dependencies:
$ npm i -d && npm test
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Brian Woodward
Copyright © 2015 Brian Woodward Released under the MIT license.
This file was generated by verb-cli on August 07, 2015.