UNPKG

@pega/custom-dx-components

Version:

Utility for building custom UI components

273 lines (232 loc) 9.51 kB
import inquirer from 'inquirer'; import chalk from 'chalk'; import { showVersion, getC11NB2STokenAndStaticServer, getLocalLibraries, getLocalLibraryVersions, getLocalLibraryVersionData, getLibraryBased, addDebugLog, checkLibraryAndArchives, getConfigDefaults, cleanUpTemp, getPegaServerConfig, getLibraryBasedCL, getApplicationAndVersionFromToken } from '../../util.js'; import { listLibJSONResponse } from '@pega/constellation-dx-components-build-utils/index.js'; import { SOURCE_OF_COMPONENT_TYPES, getServerLibraries, getInfinityLibraries, getServerLocalQuestion } from './helper.js'; export const getServerLibraryVersions = async libraryName => { addDebugLog('getServerLibraryVersions', `libraryName: ${libraryName}`, ''); // server const tokenAndStaticServer = await getC11NB2STokenAndStaticServer(); if (tokenAndStaticServer.C11NB2S === undefined) { console.log(chalk.redBright('Need to authenticate, missing services token.')); process.exit(1); } addDebugLog('listLibJSONResponse', 'services call', ''); const oLibList = await listLibJSONResponse(tokenAndStaticServer.C11NB2S, tokenAndStaticServer.appStaticContentServer); addDebugLog('listLibJSONResponse', 'services end', ''); let versionList = null; if (oLibList && oLibList[libraryName]) { versionList = oLibList[libraryName]; } return versionList; }; 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(); await checkLibraryAndArchives(false); addDebugLog('listPublished', '', '+'); await cleanUpTemp(); let sourceOfLibrary; let organization; let library; const compDef = getConfigDefaults(); organization = compDef.organization; library = compDef.library; let libraryName = ''; let selectedVersion = ''; let libVersions = []; if (options.params.length >= 5) { sourceOfLibrary = options.params[3]; libraryName = options.params[4]; selectedVersion = options.params[5]; libVersions.push(selectedVersion); } else { const questions = await getServerLocalQuestion(); const answers = await inquirer.prompt(questions); ({ sourceOfLibrary } = answers); } if (sourceOfLibrary === SOURCE_OF_COMPONENT_TYPES.LOCAL) { // local const arLibs = await getLocalLibraries(); if (arLibs && arLibs.length > 0) { if (libraryName === '') { const libAnswers = await inquirer.prompt([ { name: 'library', type: 'rawlist', message: 'Pick library', choices: arLibs } ]); libraryName = libAnswers.library; } if (libVersions.length == 0) { try { libVersions = await getLocalLibraryVersions(libraryName); } catch (ex) {} } if (libVersions.length > 0) { if (selectedVersion === '') { const libAnswers = await inquirer.prompt([ { name: 'libVersion', type: 'rawlist', message: 'Enter version', choices: libVersions } ]); selectedVersion = libAnswers.libVersion; } let data = await getLocalLibraryVersionData(libraryName, selectedVersion); data = JSON.parse(data); // get built on from first component if exists let builtOn = '(date unavailable)'; if (data[0] && data[0].buildDate) { builtOn = new Date(data[0].buildDate); } let libData = data.map(component => ({ Component: component.name, Label: component.label, Version: component.version, Type: component.type, 'Sub Type': component.subtype })); console.log(chalk.bold.blueBright(`\nList of ${chalk.inverse(`Local`)} ${libraryName}:${selectedVersion} components built on ${builtOn}.`)); console.table(libData, ['Component', 'Label', 'Version', 'Type', 'Sub Type']); } else { // No library with that name console.log(chalk.redBright(`No local library named: ${libraryName} exists.`)); console.log(chalk.redBright(`You need to publish library: ${libraryName} first.`)); addDebugLog('listPublished', 'END', '-'); process.exit(1); } } else { console.log(chalk.redBright(`\nNo local libraries exist.`)); } } else { const isLibraryBasedCL = getLibraryBasedCL(); if (isLibraryBasedCL) { const arLibs = await getInfinityLibraries(); const { app_name, app_version } = await getApplicationAndVersionFromToken(); if (arLibs && arLibs.pxResults && arLibs.pxResults.length > 0) { // const libs = arLibs.pxResults.map((oLib, index) => ({ name: oLib.pyLabel, value: index})); const libs = arLibs.pxResults.map((oLib, index) => ({ name: `${oLib.pyLabel} (${oLib.pyRuleSet}:${oLib.pyRuleSetVersion})`, value: index })); const serverConfig = await getPegaServerConfig(); console.log(`\nServer ${chalk.green.bold(`${serverConfig.server}`)}.`); console.log(`List of libraries (including built on) for Application: ${chalk.bold.green(`${app_name} - ${app_version}`)}`); // console.log(`in ruleset: ${chalk.green.bold(`${serverConfig.rulesetName}:${serverConfig.rulesetVersion}`)}`); const libAnswers = await inquirer.prompt([ { name: 'library', type: 'rawlist', message: 'Pick component library', choices: libs } ]); const libraryIndex = libAnswers.library; const data = JSON.parse(arLibs.pxResults[libraryIndex].pxComponentDefinition); const library = arLibs.pxResults[libraryIndex].pyLabel.replace(' ', '/'); const ruleSet = arLibs.pxResults[libraryIndex].pyRuleSet; const ruleSetVersion = arLibs.pxResults[libraryIndex].pyRuleSetVersion; // get built on from first component if exists let builtOn = '(date unavailable)'; if (data[0] && data[0].buildDate) { builtOn = new Date(data[0].buildDate); } let libData = data.map(component => ({ Component: component.name, Label: component.label, Version: component.version, Type: component.type, 'Sub Type': component.subtype })); console.log( chalk.bold.blueBright(`\nList of ${library} from ${chalk.inverse(`${ruleSet}:${ruleSetVersion}`)} components built on ${builtOn}.`) ); console.table(libData, ['Component', 'Label', 'Version', 'Type', 'Sub Type']); } else { // No library with that name const serverConfig = await getPegaServerConfig(); console.log(chalk.redBright(`No server component libraries exist in application ${chalk.redBright.bold(`${app_name} - ${app_version}`)}.`)); // console.log(chalk.redBright(`No server component libraries exist in given ruleset ${chalk.bold(`${serverConfig.rulesetName}:${serverConfig.rulesetVersion}`)}`)); console.log(chalk.redBright(`You need to publish a library to a ruleset in this application first.`)); addDebugLog('listPublished', 'END', '-'); process.exit(1); } } else { // old app static server const arLibs = await getServerLibraries(); if (arLibs && arLibs.length > 0) { const libAnswers = await inquirer.prompt([ { name: 'library', type: 'rawlist', message: 'Pick library', choices: arLibs } ]); libraryName = libAnswers.library; const libVersions = await getServerLibraryVersions(libraryName); if (libVersions) { const keys = Object.keys(libVersions); const libVersionAnswers = await inquirer.prompt([ { name: 'libVersion', type: 'rawlist', message: 'Enter version', choices: keys } ]); const selectedVersion = libVersionAnswers.libVersion; const versionData = libVersions[selectedVersion]; const data = JSON.parse(versionData); // get built on from first component if exists let builtOn = '(date unavailable)'; if (data[0] && data[0].buildDate) { builtOn = new data[0].buildDate(); } let libData = data.map(component => ({ Component: component.name, Label: component.label, Version: component.version, Type: component.type, 'Sub Type': component.subtype })); console.log(chalk.bold.blueBright(`\nList of ${libraryName}/${selectedVersion} components built on ${builtOn}.`)); console.table(libData, ['Component', 'Label', 'Version', 'Type', 'Sub Type']); } else { console.log(chalk.red('No libraries available on server.')); } } else { // No library with that name console.log(chalk.redBright(`No server library named: ${libraryName} exists.`)); console.log(chalk.redBright(`You need to publish library: ${libraryName} first.`)); addDebugLog('listPublished', 'END', '-'); process.exit(1); } } } addDebugLog('listLibrary', 'END', '-'); return true; };