UNPKG

@pega/custom-dx-components

Version:

Utility for building custom UI components

185 lines (128 loc) 5.82 kB
import inquirer from 'inquirer'; import ora from 'ora'; import chalk from 'chalk'; import { showVersion, getC11NB2STokenAndStaticServer, getLocalLibraryVersions, getLibraryBased, deleteLocalLibrary, deleteLocalLibraryVersion, addDebugLog, getConfigDefaults, getLibraryBasedCL, getPegaServerConfig, deleteInfinityServerComponentLibrary, checkAccessTokenExpiration, getApplicationAndVersionFromToken } from '../../util.js'; import { deleteLib } from '@pega/constellation-dx-components-build-utils/index.js'; import { getDeleteLibraryQuestions, getLibraryVersionQuestion, getRulesetAndVersionQuestion, SOURCE_OF_COMPONENT_TYPES, getInfinityLibraries } from './helper.js'; export default async (options) => { const isLibraryBased = getLibraryBased(); if (!isLibraryBased) { console.log(`Command only supported for ${chalk.bold.green('library mode')} components.`) process.exit(); } await showVersion(); addDebugLog("deletePublished", "", "+"); let sourceOfLibrary; let organization; let library; let version; const compDef = getConfigDefaults(); organization = compDef.organization; library = compDef.library; const libraryName = compDef.currentOrgLib; if (options.params.length >= 5) { sourceOfLibrary = options.params[3]; version = options.params[4]; } else { const questions = await getDeleteLibraryQuestions(); const answers = await inquirer.prompt(questions); ({ sourceOfLibrary } = answers); if (sourceOfLibrary === SOURCE_OF_COMPONENT_TYPES.LOCAL ) { // local let libVersions = []; try { libVersions = await getLocalLibraryVersions(libraryName); } catch (ex) {} if (libVersions.length > 0) { // ALL option libVersions.unshift('ALL'); const versionQuestions = await getLibraryVersionQuestion(libraryName, libVersions); const versionAnswers = await inquirer.prompt(versionQuestions); ({version} = versionAnswers); } else { // No library with that name console.log(chalk.redBright(`No local library named: ${libraryName} exists.`)); addDebugLog("deletePublished", "END", "-"); process.exit(1); } } } if (sourceOfLibrary === SOURCE_OF_COMPONENT_TYPES.LOCAL ) { if (version === 'ALL') { await deleteLocalLibrary(libraryName); } else { await deleteLocalLibraryVersion(libraryName, version); } } else { // server await checkAccessTokenExpiration(); const isLibraryBasedCL = getLibraryBasedCL(); if (isLibraryBasedCL) { const serverConfig = await getPegaServerConfig(); const { app_name, app_version } = await getApplicationAndVersionFromToken(); console.log(); console.log(`Current server: ${chalk.green.bold(`${serverConfig.server}`)}`); console.log(`List of libraries for Application: ${chalk.green.bold(`${app_name} - ${app_version}`)}`); // want to get all in application, so ignore ruleset/version questions // const questions = await getRulesetAndVersionQuestion(serverConfig); // const answers = await inquirer.prompt(questions); // const { rulesetName, rulesetVersion } = answers; // const arLibs = await getInfinityLibraries(rulesetName, rulesetVersion); const arLibs = await getInfinityLibraries(); if (arLibs && arLibs.pxResults && arLibs.pxResults.length > 0) { console.log(chalk.bold.yellow(`\n>> Caution...`)); console.log(chalk.bold.yellow(`>> This list may contain built on libraries if your application is built on an application.`)); console.log(chalk.bold.yellow(`>> Deleting built on libraries may cause issues with built on application.\n`)); const libs = arLibs.pxResults.map((oLib, index) => ({ name: oLib.pyLabel + " (" + oLib.pyRuleSet + ":" + oLib.pyRuleSetVersion + ")", value: index})); const libAnswers = await inquirer.prompt([ { name: 'library', type: 'rawlist', message: 'Pick component library to delete', choices: libs } ]); const libraryName = arLibs.pxResults[libAnswers.library].pyRuleName; const rulesetName = arLibs.pxResults[libAnswers.library].pyRuleSet; const rulesetVersion = arLibs.pxResults[libAnswers.library].pyRuleSetVersion; // let spinner; try { await deleteInfinityServerComponentLibrary(libraryName, rulesetName, rulesetVersion); } catch (err) { let sErr = err.toString(); sErr = sErr.replaceAll("Error: ", ""); console.log(chalk.redBright.bold("Error: " + sErr)); } // spinner.stop(); } else { // No library with that name const serverConfig = await getPegaServerConfig(); console.log(""); console.log(chalk.redBright(`No server component libraries exist in current application ${chalk.redBright.bold(`${app_name} - ${app_version}`)}.`)); addDebugLog("listPublished", "END", "-"); process.exit(1); } } else { // ap static server const tokenAndStaticServer = await getC11NB2STokenAndStaticServer(); if (tokenAndStaticServer.C11NB2S === undefined) { console.log(chalk.redBright("Need to authenticate, missing services token.\nDeleting a library requires authentication to acquire a token to delete.")); addDebugLog("deletePublished", "END", "-"); process.exit(1); } addDebugLog("deleteLib", "services call", ""); await deleteLib(tokenAndStaticServer.C11NB2S, tokenAndStaticServer.appStaticContentServer); addDebugLog("deleteLib", "services end", ""); } } addDebugLog("deletePublished", "END", "-"); return true; };