UNPKG

2.16 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2018, Kinvey, Inc. All rights reserved.
3 *
4 * This software is licensed to you under the Kinvey terms of service located at
5 * http://www.kinvey.com/terms-of-use. By downloading, accessing and/or using this
6 * software, you hereby accept such terms of service (and any agreement referenced
7 * therein) and agree that you have read, understand and agree to be bound by such
8 * terms of service and are of legal age to agree to such terms with Kinvey.
9 *
10 * This software contains valuable confidential and proprietary information of
11 * KINVEY, INC and is subject to applicable licensing agreements.
12 * Unauthorized reproduction, transmission or distribution of this file and its
13 * contents is a violation of applicable laws.
14 */
15
16const inquirer = require('inquirer');
17
18const { isNullOrUndefined } = require('./Utils');
19
20/**
21 * Serves as a wrapper around the module used for gathering user input.
22 */
23class Prompter {
24 /**
25 * Prompts with the specified question(s).
26 * @param {Array|Object} question
27 * @param {string} question.message
28 * @param {string} question.name
29 * @param {string} question.type
30 * @param {Array} [question.choices]
31 * @param [question.validate]
32 * @param [question.when]
33 * @param done
34 */
35 static prompt(question, done) {
36 const questionsArr = Array.isArray(question) ? question : [question];
37 inquirer.prompt(questionsArr, (data) => {
38 done(null, data);
39 });
40 }
41
42 /**
43 * Builds a question.
44 * @param message
45 * @param name
46 * @param type
47 * @param defaultValue
48 * @param choices
49 * @param validate
50 * @param when
51 * @returns {{message: *, name: *, type: *, choices: *, validate: *, when: *}}
52 */
53 static buildQuestion(message, name, type, defaultValue, choices, validate, when) {
54 const q = {
55 message,
56 name,
57 type,
58 choices,
59 default: defaultValue
60 };
61
62 if (typeof validate === 'function') {
63 q.validate = validate;
64 }
65
66 if (!isNullOrUndefined(when)) {
67 q.when = when;
68 }
69
70 return q;
71 }
72}
73
74module.exports = Prompter;