UNPKG

@pega/constellation-dx-components-build-utils

Version:

This tool uses a 'v3' approach to group components in a library, create a component map, employ webpack, and load the library like Pega-generated components, constellation app-static.

120 lines (113 loc) 5.88 kB
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); if(!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)); if(SELECT_LIBRARY_NAME && SELECT_LIBRARY_VERSION){ const manifestData = await AppStaticService.fetchLibraryManifestData(SELECT_LIBRARY_NAME, SELECT_LIBRARY_VERSION, tokenParam, appStaticSVCUrl); if(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 = []; if(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) if(libNames){ for(let i=0; i< libNames.length; i++){ if(libNames[i]){ if(!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 }