Lines of Code | 65 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | const inquirer = require('inquirer'); |
||
2 | |||
3 | /** |
||
4 | * Construct questions and ask then using the inquirer module |
||
5 | * @class Inquire |
||
6 | */ |
||
7 | class Inquire { |
||
8 | |||
9 | /** |
||
10 | * Creates an instance of Inquire. |
||
11 | * @memberOf Inquire |
||
12 | */ |
||
13 | constructor() { |
||
14 | this.questions = []; |
||
15 | this.validate = { |
||
16 | notEmpty(value) { |
||
17 | return (value && value.length && value.length > 0) ? |
||
18 | true : 'Please enter a value'; |
||
19 | }, |
||
20 | yesNo(value) { |
||
21 | return (value && |
||
22 | ['y', 'n', 'yes', 'no'].indexOf(value.toLowerCase()) !== -1) ? |
||
23 | true : 'Invalid input to yes/no question'; |
||
24 | } |
||
25 | }; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Add a question to the questions property |
||
30 | * @param {String} name - name of the question |
||
31 | * @param {String} type - type of input |
||
32 | * @param {String} message - body of the message |
||
33 | * @param {String} validate - validation method to use |
||
34 | * @returns {Void} - returns nothing |
||
35 | * |
||
36 | * @memberOf Inquire |
||
37 | */ |
||
38 | question(name, type, message, validate) { |
||
39 | const details = { |
||
40 | name, |
||
41 | type, |
||
42 | message, |
||
43 | validate: (!validate) ? undefined : this.validate[validate] |
||
44 | }; |
||
45 | this.questions.push(details); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Combine and ask all the questions in the questions property |
||
50 | * @param {Function} callback - function to run after user is done |
||
51 | * with questions |
||
52 | * @returns {Void} - returns nothing |
||
53 | * @memberOf Inquire |
||
54 | */ |
||
55 | ask(callback) { |
||
56 | return new Promise((resolve) => { |
||
57 | inquirer.prompt(this.questions).then((answers) => { |
||
58 | callback(answers); |
||
59 | resolve(answers); |
||
60 | }); |
||
61 | }); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | module.exports = Inquire; |
||
66 |