UNPKG

1.47 kBJavaScriptView Raw
1/**
2 * Ask for configuration.
3 * @function askconfig
4 * @param {object} props - Values to ask.
5 * @param {object} [options] - Optional settings.
6 * @param {string} [options.msg] - Message to print before interaction.
7 * @param {function} callback - Callback when done.
8 */
9
10"use strict";
11
12const async = require('async'),
13 argx = require('argx'),
14 objnest = require('objnest'),
15 Question = require('./question'),
16 questionInterface = require('./question_interface');
17
18/** @lends askconfig */
19function askconfig(props, options, callback) {
20 let args = argx(arguments);
21 callback = args.pop('function');
22 props = args.shift();
23 options = args.pop('object') || {};
24
25 if (options.msg) {
26 [].concat(options.msg).forEach((msg) => {
27 console.log(msg);
28 });
29 }
30
31 let questions = Object.keys(props).map((key) => {
32 return Question.new({
33 key: key,
34 default: props[key]
35 });
36 });
37
38 let qi = questionInterface();
39 let result = {};
40 let i = 0;
41 async.eachSeries(questions, (question, callback) => {
42 qi.question(question.query(), (answer) => {
43 let key = question.key || i;
44 i++;
45 result[key] = answer || question.default;
46 callback(null);
47 });
48 }, (err) => {
49 qi.on('close', () => {
50 callback(err, objnest.expand(result));
51 });
52 qi.close();
53 });
54}
55
56module.exports = askconfig;