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.

134 lines (129 loc) 5.91 kB
const inquirer = require('inquirer'); const { AppStaticService } = require('../../services/appstatic.service'); const CONSTANT = require('../constant'); const chalk = require('chalk'); const { getQuestionsListDynamically } = require('../helper'); /** * This function provides interactive list of libraries/versions available for org from token. 1. List all the libray names 2. Take a choice prompt of deleting entire lib/specific version of a lib(Only -dev version allowed to delete) 3. Delete operation * @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 deleteLib = async (tokenParam, appStaticSVCUrl) => { try{ // Make a fetch call and list all the libraries available. const libraryList = await AppStaticService.fetchLibraryNameOrVersions('', tokenParam, appStaticSVCUrl); const { SELECT_LIBRARY_NAME } = await inquirer.prompt(getQuestionsListDynamically('SELECT_LIBRARY_NAME', 'Select a library', libraryList)); const deleteLibOrSpecificVersion = () => { return [ { name: 'SELECT_OPTIONS', message: 'Choose any of the option', type: 'list', choices: CONSTANT.QUESTIONS.DELETE_LIB_OR_SPECIFIC_VERSION, default: false } ]; } const { SELECT_OPTIONS } = await inquirer.prompt(deleteLibOrSpecificVersion()); 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; } if(SELECT_OPTIONS === 'delete_library'){ // Take confirmation before deleting const confirmation = await inquirer.prompt([ { type: 'confirm', name: 'confirmed', message: `Are you sure you want to delete ?: ${SELECT_LIBRARY_NAME}`, default: false } ]); if(confirmation.confirmed){ // There is only endpoint for deleting one version at a time, hence get list of versions of a lib first. await deleteVersions({ libName: SELECT_LIBRARY_NAME, libVersions: [...libVersionsList], isDeleteAll: true }, tokenParam, appStaticSVCUrl); } else{ // TODO: Should we go back or just exit return; } } else if(SELECT_OPTIONS === 'delete_version'){ const { SELECT_LIBRARY_VERSION } = await inquirer.prompt(getQuestionsListDynamically('SELECT_LIBRARY_VERSION', 'Select a library version', libVersionsList)); await deleteVersions({ libName: SELECT_LIBRARY_NAME, libVersions: [SELECT_LIBRARY_VERSION], isDeleteAll: false }, tokenParam, appStaticSVCUrl); } else{ return; } } catch(err){ console.log(chalk(`Error occured while fetching the library list: ${err}`)); process.exit(1); } } async function deleteVersions({libName, libVersions, isDeleteAll = false}, tokenParam, appStaticSVCUrl) { try { let deletedCount = 0; let notDeletedCount = 0; for (const version of libVersions) { try { if(version.indexOf('dev') !== -1){ const libVersion = `${libName}:${version}`; const deleted = await AppStaticService.deleteLibraryOrVersions(libVersion, tokenParam, appStaticSVCUrl); if(deleted){ console.log(chalk.magenta(`Deleted version: ${libVersion}`)); deletedCount++; } else{ console.log(chalk.magenta(`Not able to delete: ${libVersion}`)); } } else{ console.error(chalk.yellow(`Not able to delete ${version}, Only dev version(e.g. 1.0.1-dev) can be deleted.`)); } } catch (error) { console.error(chalk.red(`Failed to delete version: ${version}`)); notDeletedCount++; } } if (deletedCount === libVersions.length && isDeleteAll) { console.log(chalk.green('All versions deleted successfully! ✅')); } else if(deletedCount !== libVersions.length && isDeleteAll) { console.log(chalk.yellow(`Deleted ${deletedCount} versions, ${notDeletedCount} versions not deleted.`)); } } catch (error) { console.error(chalk.red('Error occurred while deleting versions:', error)); } } /** * This function delete lib based on library/version passed. * @param {string} libraryName Library name required arg. * @param {string} libraryVersion Library version required arg. * @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 deleteLibVersion = async (libName, libVersion, tokenParam, appStaticSVCUrl) => { if(libName && libVersion){ await deleteVersions({ libName: libName, libVersions: [libVersion], isDeleteAll: false }, tokenParam, appStaticSVCUrl); } else { console.log(chalk.red(`Args missing Library name=${libName}, Library version=${libVersion}`)); } } module.exports = { deleteLib, deleteLibVersion }