# All files / src/systemTools prompt.js

31.82% Statements 28/88
15.91% Branches 7/44
16.67% Functions 2/12
28.38% Lines 21/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    1104x

    4x

    4x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x
    1x
    1x
    1x

    4x

    4x

    4x

    4x

    4x

    4x

    4x
     import chalk from 'chalk';

    const highlight = chalk.green;

    export const generateOptions = (inputData, isMultiChoice = false, mapping, renderMethod) => {
        let asString = '';
        const valuesAsObject = {};
        const valuesAsArray = [];
        const keysAsObject = {};
        const keysAsArray = [];
        const isArray = Array.isArray(inputData);

        const output = {
            pick: (v, defaultOption) => new Promise((resolve, reject) => {
                let selectedOptions = [];
                const pickedOpt = v || defaultOption;
                if (isMultiChoice) {
                    const wrongOptions = [];
                    if (pickedOpt) {
                        const choiceArr = v.split(',');
                        choiceArr.forEach((choice) => {
                            let selectedOption = choice;
                            if (isNaN(choice)) {
                                selectedOption = choice;
                            } else {
                                selectedOption = keysAsArray[choice - 1];
                            }
                            selectedOptions.push(selectedOption);
                            if (!valuesAsObject[selectedOption]) {
                                wrongOptions.push(choice);
                            }
                        });
                    } else {
                        selectedOptions = keysAsArray;
                    }
                    if (wrongOptions.length) {
                        reject(`${highlight(wrongOptions.join(','))} ...Really?! 🙈`);
                    } else {
                        output.selectedOptions = selectedOptions;
                        resolve(selectedOptions);
                    }
                } else {
                    let selectedOption = pickedOpt;
                    if (isNaN(pickedOpt)) {
                        selectedOption = pickedOpt;
                    } else {
                        selectedOption = keysAsArray[v - 1];
                    }
                    if (!valuesAsObject[selectedOption]) {
                        reject(`${highlight(pickedOpt)} ...Really?! 🙈`);
                    } else {
                        output.selectedOption = selectedOption;
                        resolve(selectedOption);
                    }
                }
            })
        };
        const renderer = renderMethod || _generateOptionString;
      E  if (isArray) {
            inputData.map((v, i) => {
                asString += renderer(i, v, mapping, v);
                valuesAsArray.push(v);
                if (!mapping) keysAsArray.push(v);
                if (!mapping) valuesAsObject[v] = v;
            });
        } else {
            let i = 0;
            for (const k in inputData) {
                const v = inputData[k];
                asString += renderer(i, v, mapping, k);
                keysAsArray.push(k);
                keysAsObject[k] = true;
                valuesAsObject[k] = v;
                valuesAsArray.push(v);
                i++;
            }
        }
        output.keysAsArray = keysAsArray;
        output.valuesAsArray = valuesAsArray;
        output.keysAsObject = keysAsObject;
        output.valuesAsObject = valuesAsObject;
        output.asString = asString;
        return output;
    };

    const _generateOptionString = (i, obj, mapping, defaultVal) => `-[${highlight(i + 1)}] ${highlight(mapping ? '' : defaultVal)} \n`;

    let _currentQuestion;

    export const askQuestion = (question, obj, key) => new Promise((resolve, reject) => {
        if (!_currentQuestion) {
            _currentQuestion = require('readline').createInterface({
                input: process.stdin,
                output: process.stdout,
            });
        }

        _currentQuestion.question(getQuestion(question), (v) => {
            if (obj && key) obj[key] = v;
            resolve(v === '' ? null : v);
        });
    });

    export const getQuestion = msg => chalk.blue(`\n ❓ ${msg}: `);

    export const finishQuestion = () => new Promise((resolve, reject) => {
        _currentQuestion && _currentQuestion.close();
        _currentQuestion = null;
    });