1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | "use strict";
|
11 |
|
12 | const async = require('async'),
|
13 | argx = require('argx'),
|
14 | objnest = require('objnest'),
|
15 | Question = require('./question'),
|
16 | questionInterface = require('./question_interface');
|
17 |
|
18 |
|
19 | function 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 |
|
56 | module.exports = askconfig;
|