All files / src/systemTools prompt.js

81.4% Statements 70/86
54.55% Branches 30/55
100% Functions 6/6
90.16% Lines 55/61

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 833x 3x 3x 3x   3x   16x 8x 8x                       8x     36x 36x 36x 36x 36x 36x 36x   36x 36x 36x 31x 465x 465x 465x 465x     5x 5x 4x 4x 4x 4x 4x 4x 4x     36x 36x 36x 36x 36x 36x 3x   3x 2856x 2856x 2856x 2854x 2854x   2x 2x     2856x 2856x 2048x 808x 806x   2856x     465x  
import chalk from 'chalk';
import inquirer from 'inquirer';
import { logWarning, logTask, rnvStatus, logEnd, logToSummary } from './logger';
import Config from '../config';
 
const highlight = chalk.grey.bold;
 
export const inquirerPrompt = async (params) => {
    const c = Config.getConfig();
    const msg = params.logMessage || params.warningMessage;
    if (c.program.ci) {
        throw msg || '--ci option does not allow prompts';
    }
    if (msg && params.logMessage) logTask(msg, chalk.grey);
    if (msg && params.warningMessage) logWarning(msg);
 
    // allow passing in just { type: 'prompt', ... } instead of { type: 'prompt', name: 'prompt', ... }
    const { type, name } = params;
    if (type === 'confirm' && !name) params.name = 'confirm';
 
    const result = await inquirer.prompt(params);
    return result;
};
 
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 = {};
    const renderer = renderMethod || _generateOptionString;
    if (isArray) {
        inputData.map((v, i) => {
            asString += renderer(i, v, mapping, v);
            valuesAsArray.push(v);
      E      if (!mapping) keysAsArray.push(v);
      E      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.sort(_sort);
    output.valuesAsArray = valuesAsArray.sort(_sort);
    output.keysAsObject = keysAsObject;
    output.valuesAsObject = valuesAsObject;
    output.asString = asString;
    return output;
};
 
const _sort = (a, b) => {
    let aStr = '';
    let bStr = '';
    if (typeof a === 'string') {
        aStr = a.toLowerCase();
        bStr = b.toLowerCase();
    } else {
    I    if (a && a.name) aStr = a.name.toLowerCase();
    I    if (b && b.name) bStr = b.name.toLowerCase();
    }
 
    let com = 0;
    if (aStr > bStr) {
        com = 1;
    } else if (aStr < bStr) {
        com = -1;
    }
    return com;
};
 
const _generateOptionString = (i, obj, mapping, defaultVal) => ` [${highlight(i + 1)}]> ${highlight(mapping ? '' : defaultVal)} \n`;