Passed
Pull Request — master (#24)
by Inumidun
01:22
created

lib/Inquire.js   A

Size

Lines of Code 62

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
nc 1
dl 0
loc 62
rs 10
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A Inquire.js ➔ ??? 0 12 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) ? true : 'Please enter a value';
18
      },
19
      yesNo(value) {
20
        return (value && ['y', 'n', 'yes', 'no'].indexOf(value.toLowerCase()) !== -1) ?
21
          true : 'Invalid input to yes/no question';
22
      }
23
    };
24
  }
25
26
  /**
27
   * Add a question to the questions property
28
   * @param {String} name - name of the question
29
   * @param {String} type - type of input
30
   * @param {String} message - body of the message
31
   * @param {String} validate - validation method to use
32
   * @returns {Void} - returns nothing
33
   *
34
   * @memberOf Inquire
35
   */
36
  question(name, type, message, validate) {
37
    const details = {
38
      name,
39
      type,
40
      message,
41
      validate: (!validate) ? undefined : this.validate[validate]
42
    };
43
    this.questions.push(details);
44
  }
45
46
  /**
47
   * Combine and ask all the questions in the questions property
48
   * @param {Function} callback - function to run after user is done with questions
49
   * @returns {Void} - returns nothing
50
   * @memberOf Inquire
51
   */
52
  ask(callback) {
53
    return new Promise((resolve) => {
54
      inquirer.prompt(this.questions).then((answers) => {
55
        callback(answers);
56
        resolve(answers);
57
      });
58
    });
59
  }
60
}
61
62
module.exports = Inquire;
63