skyrocket-cli
Version:
Command line interface for rapid Skyrocket projects development.
61 lines (55 loc) • 1.39 kB
JavaScript
const async = require('async')
const inquirer = require('inquirer')
/**
* Metalsmith plugin to inquirer prompt wrapper.
*
* @param {Object} prompt
*/
module.exports = function prompt (prompt) {
return (files, metalsmith, done) => {
askQuestions(prompt, metalsmith.metadata(), done)
}
}
/**
* Single inquirer prompt wrapper.
*
* @param {Object} prompt
* @param {Object} data => metalsmith.metadata()
* @param {Function} next
*/
function singleQuestion (prompt, key, data, next) {
// simulate the when property
if (typeof prompt.when === 'function') {
if (!prompt.when(data)) {
return next()
}
}
inquirer.prompt([{
type: prompt.type,
name: key,
message: prompt.message,
default: prompt.default || '',
choices: prompt.choices || [],
validate: prompt.validate || (() => true)
}]).then(answer => {
if (key === 'cli_prefix' && answer[key]) {
data[key] = answer[key] + '-'
} else {
data[key] = answer[key]
}
if (typeof prompt.action === 'function') prompt.action(answer)
next()
})
}
/**
* Iterate mutiple prompts
*
* @param {Object} prompt
* @param {Object} data => metalsmith.metadata()
* @param {Function} done
*/
function askQuestions (prompts, data, done) {
async.eachSeries(Object.keys(prompts), (key, next) => {
singleQuestion(prompts[key], key, data, next)
}, done)
}