UNPKG

@pega/custom-dx-components

Version:

Utility for building custom UI components

206 lines (154 loc) 6.18 kB
import inquirer from 'inquirer'; import chalk from 'chalk'; import {showVersion, getC11NB2STokenAndStaticServer, getLocalLibraries, getLocalLibraryVersions, getLocalLibraryVersionData, getLibraryBased, addDebugLog, checkLibraryAndArchives, getConfigDefaults, cleanUpTemp } from '../../util.js'; import { listLibJSONResponse } from '@pega/constellation-dx-components-build-utils/index.js'; import { getListLibraryQuestions, SOURCE_OF_COMPONENT_TYPES, getServerLibraries, 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.\nDeleting a library requires authentication to acquire a token to delete.")); 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(); 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 (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 = 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 { // 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 { 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 = 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; };