All files / lib/list list.js

98.52% Statements 67/68
70.83% Branches 17/24
100% Functions 6/6
98.46% Lines 64/65

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 110 111 112 113 114 115 116 117 118 119 1202x 2x 2x 2x               2x 3x   3x 3x   3x 3x 3x 2x 1x 1x   1x 1x 1x 1x 1x 1x 1x                             1x 1x       2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x   1x 1x   1x 3x 3x 3x 9x   3x     1x                 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 2x 2x 1x     4x           5x         8x 8x     2x      
const inquirer = require('inquirer');
const { AppStaticService } = require('../../services/appstatic.service');
const chalk = require('chalk');
const { getQuestionsListDynamically } = require('../helper');
 
/**
 * This function provides interactive list of libraries/versions available for org from token.
 * User can use down/up/right arrow to explore content of libraries.
 * @param {string} tokenParam optional B2S token for appstatic service, else it will take value from  constant.js B2STOKEN
 * @param {string} appStaticSVCUrl optional appstatic service url, else it will take value from constant.js APPSTATICURL
 */
const listLib = async (tokenParam, appStaticSVCUrl) => {
    try{
        // Make a fetch call and list all the libraries available.
        let libraryList = await AppStaticService.fetchLibraryNameOrVersions('', tokenParam, appStaticSVCUrl);
        Iif(!libraryList)
            return;
        libraryList = libraryList.filter(item => !matchesPattern(item)); // remove uuidv4 temp dir
        const { SELECT_LIBRARY_NAME } = await inquirer.prompt(getQuestionsListDynamically('SELECT_LIBRARY_NAME', 'Select a library to get versions available', libraryList));
        const libVersionsList = await AppStaticService.fetchLibraryNameOrVersions(SELECT_LIBRARY_NAME, tokenParam, appStaticSVCUrl);
        if(libVersionsList && libVersionsList.length === 0){
            console.log(chalk.yellow(`There are no versions in library selected`));
            return;
        } else{
            const { SELECT_LIBRARY_VERSION } = await inquirer.prompt(getQuestionsListDynamically('SELECT_LIBRARY_VERSION', `List of versions available in ${SELECT_LIBRARY_NAME}`, libVersionsList));
            Eif(SELECT_LIBRARY_NAME && SELECT_LIBRARY_VERSION){
                const manifestData = await AppStaticService.fetchLibraryManifestData(SELECT_LIBRARY_NAME, SELECT_LIBRARY_VERSION, tokenParam, appStaticSVCUrl);
                Eif(manifestData){
                    const TreePrompt = require('inquirer-tree-prompt');
                    inquirer.registerPrompt('tree', TreePrompt);
                    inquirer.prompt([
                        {
                            type: 'tree',
                            name: 'components',
                            message: `Components available in ${SELECT_LIBRARY_NAME}:${SELECT_LIBRARY_VERSION}`,
                            tree:
                                modifiedManifestDataToTree(manifestData)
 
                        }
                    ])
                }
            }
        }
    }
    catch(err){
        console.log(chalk(`Error occurred while fetching the library list and versions: ${err}`));
        return;
    }
}
 
const modifiedManifestDataToTree = (manifestData) => {
    const components = manifestData["components"];
    const modifiedData = [];
    Eif(manifestData["versions"]){
        const comptDataObj = {};
        comptDataObj['value'] = 'Cosmos package versions used';
        const cosmosDataArr = []
        for (const cosmospkg in manifestData["versions"]) {
            const cosmosDataObj = {};
            cosmosDataObj['value'] = cosmospkg;
            cosmosDataObj['children'] = [manifestData["versions"][cosmospkg]];
            cosmosDataArr.push(cosmosDataObj)
        }
        comptDataObj['children'] = cosmosDataArr;
        modifiedData.push(comptDataObj);
    }
    for (const component in components) {
        const comptDataObj = {};
        comptDataObj['value'] = component;
        comptDataObj['children'] = components[component].filter(function (files) {
            return !files.endsWith('.map')
          });
        modifiedData.push(comptDataObj);
    }
 
    return modifiedData;
}
 
/**
 * This function return json response of libraries/versions available for org from token.
 * Example : ["Dept1_constellation-ui-gallery":["0.0.1":'[{comptsconfig json data}]',"0.0.2":'[{comptsconfig json data}]'],"Dept2_constellation-ui-gallery":["0.0.1":'[{comptsconfig json data}]',"0.0.2":'[{comptsconfig json data}]'],"lib-a":[],"pega-as-internal":["0.0.2":'[{comptsconfig json data}]'],"pega-constellation-ui-gallery":["1.0.0":'[{comptsconfig json data}]'],"pega-temp-internal":[]]
 * @param {string} tokenParam optional B2S token for appstatic service, else it will take value from  constant.js B2STOKEN
 * @param {string} appStaticSVCUrl optional appstatic service url, else it will take value from constant.js APPSTATICURL
 */
const listLibJSONResponse = async (tokenParam, appStaticSVCUrl) => {
    let libData = [];
    let versionsData = [];
    const libNames = await AppStaticService.fetchLibraryNameOrVersions('', tokenParam, appStaticSVCUrl)
    Eif(libNames){
        for(let i=0; i< libNames.length; i++){
            Eif(libNames[i]){
                Eif(!matchesPattern(libNames[i])){
                    versionsData = [];
                    const versions = await AppStaticService.fetchLibraryNameOrVersions(libNames[i], tokenParam, appStaticSVCUrl);
                    if(versions){
                        for(let j=0; j < versions.length; j++){
                            const comptscfg = await AppStaticService.fetchLibraryManifestData(libNames[i], versions[j], tokenParam, appStaticSVCUrl, true);
                            if(comptscfg){
                                versionsData[versions[j]] = JSON.stringify(comptscfg);
                            }
                        }
                        libData[libNames[i]] = versionsData;
                    }
                }
            }
        }
    }
    return libData;
}
 
function matchesPattern(input) {
    // Regex pattern for any alphanumeric prefix followed by a UUID v4(temp dir)
    const pattern = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
    return pattern.test(input);
}
 
module.exports = {
    listLib,
    listLibJSONResponse
}