UNPKG

@pega/custom-dx-components

Version:

Utility for building custom UI components

288 lines (218 loc) 8.94 kB
import inquirer from 'inquirer'; import { Listr } from 'listr2'; import chalk from 'chalk'; import validate from '../validator/index.js'; import { lintComponent } from '../linter/index.js'; import { getComponentDirectoryPath, showVersion, getC11NB2STokenAndStaticServer, getLocalLibraryVersions, getLibraryBased, deletePublishExtras, addDebugLog, checkLibraryAndArchives, checkJWTExpiration, getConfigDefaults, getUseInputConfig, getInputConfigForCommand } from '../../util.js'; import { publishLib, listLibJSONResponse, deleteLibVersion } from '@pega/constellation-dx-components-build-utils/index.js'; import { getLibraryVersionQuestion } from './helper.js'; export function getCustomTasks(componentKey, sourceMap, devBuild, options) { addDebugLog("getCustomTasks", `componentKey: ${componentKey}, sourceMap: ${sourceMap}, devBuild: ${devBuild}`, ""); const sDevBuild = devBuild ? '(dev build)' : ''; return new Listr( [ { title: 'Validate config schema', task: async () => { await validate(componentKey); } }, { title: 'Lint component', task: async () => { const targetDirectory = await getComponentDirectoryPath(componentKey); // console.log(`in buildComponent Lint component task: componentKey: ${componentKey} targetDirectory: ${targetDirectory}`); await lintComponent(targetDirectory); } } ], { concurrent: false, exitOnError: true } ); } export const doesLibraryExist = async (libraryName, libraryVersion) => { addDebugLog("doesLibraryExist", `libraryName: ${libraryName}, libraryVersion: ${libraryVersion}`, ""); const tokenAndStaticServer = await getC11NB2STokenAndStaticServer(); const libList = await listLibJSONResponse(tokenAndStaticServer.C11NB2S, tokenAndStaticServer.appStaticContentServer); let libVersionExists = false; if (libList && libList[libraryName] && libList[libraryName][libraryVersion]) { libVersionExists = true; } return libVersionExists; } export const publishNewLibraryAsync = async (libraryName, version, checkDeleted = false) => { addDebugLog("publishNewLibraryAsync", `libraryName: ${libraryName}, version: ${version}, checkDeleted: ${checkDeleted} `, ""); const tokenAndStaticServer = await getC11NB2STokenAndStaticServer(); if (checkDeleted ) { console.log(chalk.yellow(`Verifying library has been deleted..`)); let libExists = true; let count = 0; while (libExists) { // sleep a second and try again await new Promise(r => setTimeout(r, 1000)); // keep checking until no longer exists libExists = await doesLibraryExist(libraryName, version); count ++; if (count > 100) { console.log(chalk.red.bold(`Library hasn't been deleted, can't proceed.`)); process.exit(1); } } console.log(chalk.green(`Deleted..`)); } console.log("\nPublishing Library " + chalk.bold.green(`${libraryName}/${version}`) + " to " + chalk.bold.green(`${tokenAndStaticServer.appStaticContentServer}`)); addDebugLog("publishLib", "services call", ""); await publishLib(libraryName, version, tokenAndStaticServer.C11NB2S, tokenAndStaticServer.appStaticContentServer); addDebugLog("deleteLib", "services end", ""); } export default async (options) => { const isLibraryBased = getLibraryBased(); if (!isLibraryBased) { console.log(`Command only supported for ${chalk.bold.green('library mode')} components.`) process.exit(); } if (options.params.length != 4) { // internal so already called await showVersion(); await checkLibraryAndArchives(); await checkJWTExpiration(); } addDebugLog("publishLibrary", "", "+"); let organization; let library; let version; let devBuild; let internalPublish = false; let libraryName; let noRealPublish = false; if (options.params.length === 5) { libraryName = options.params[3]; version = options.params[4]; } else if (options.params.length === 6) { libraryName = options.params[3]; version = options.params[4]; noRealPublish = true; } else if (options.params.length === 4) { // internal call from publish internalPublish = true; const orgLib = getConfigDefaults(); library = orgLib.library; organization = orgLib.organization; // libraryName = `${organization}_${library}`; libraryName = orgLib.currentOrgLib; version = orgLib.buildVersion; // check to see if devBuild // const devBuild = await getConfigDevBuild(); // const devBuild = orgLib.devBuild; // if (devBuild) { // version = version.concat("-dev"); // } } const tokenAndStaticServer = await getC11NB2STokenAndStaticServer(); if (tokenAndStaticServer.C11NB2S === undefined) { console.log(chalk.redBright("Need to authenticate, missing services token.\nPublishing a library requires authentication to acquire a token to publish.")); process.exit(1); } let libVersions = []; if (!libraryName) { const compDef = getConfigDefaults(); organization = compDef.organization; library = compDef.library; // libraryName = `${organization}_${library}`; libraryName = compDef.currentOrgLib; try { libVersions = await getLocalLibraryVersions(libraryName); } catch (ex) {} } console.log(`\nPreparing to publish ${chalk.bold.green(`${libraryName}/${version}`)} `) if (libVersions.length > 0 || version) { if (!version) { const versionQuestions = await getLibraryVersionQuestion(libVersions); const versionAnswers = await inquirer.prompt(versionQuestions); ({version} = versionAnswers); } const useInputConfig = getUseInputConfig(); let inputConfig = {}; if (useInputConfig) { inputConfig = await getInputConfigForCommand("publish"); } let okToPublish = true; // this check matches check below, but for JEST, we don't want to go any further, just verifying if will // be caught if (noRealPublish && !version.includes("-dev")) { console.log("PRODUCTION, no publish"); return; } if (!version.includes("-dev") && !internalPublish) { okToPublish = false; const publishAnswers = await inquirer.prompt([ { name: 'confirmPublish', type: 'confirm', message: `You are about to publish a ${chalk.bold.yellow('PRODUCTION')} version which can ${chalk.bold.yellow('NOT')} be deleted, proceed ?`, default: false } ]); if (publishAnswers.confirmPublish) { okToPublish = true; } } if (noRealPublish) { // this is as far as JEST test can go since need server for rest and don't want to use server. console.log("OK to publish."); return; } if (okToPublish) { let libVersionExists = await doesLibraryExist(libraryName, version); let overrideExisting = false; let checkDeleted = false; await deletePublishExtras(libraryName, version); if (useInputConfig) { if (inputConfig.okToOverride) { // to override overrideExisting = true; } } if (libVersionExists && !overrideExisting) { console.log("\nLibrary " + chalk.bold.yellow(`${libraryName}/${version}`) + " already exists."); const questions = [ { name: 'override', type: 'confirm', message: `Do you wish to overrite it`, default: false } ]; await inquirer.prompt(questions).then(async answers => { overrideExisting = answers.override; }); } if (libVersionExists) { if (overrideExisting) { console.log("\nDeleting existing library " + chalk.bold.yellow(`${libraryName}:${version}`) + " ..."); addDebugLog("deleteLibVersion", `services call: libraryName: ${libraryName}, version: ${version}`, ""); await deleteLibVersion(libraryName, version, tokenAndStaticServer.C11NB2S, tokenAndStaticServer.appStaticContentServer); addDebugLog("deleteLibVersion", "services end", ""); checkDeleted = true; } else { // don't override, so end console.log(chalk.yellow(`You will need to increment your library version to publish`)); addDebugLog("publishLibrary", "END", "-"); return; } } // need a timeout to give services time to delete the library // setTimeout( function(){ await publishNewLibraryAsync(libraryName, version, checkDeleted)}, 700 ); await publishNewLibraryAsync(libraryName, version, checkDeleted); } } else { console.log(chalk.bold.redBright(`No library versions named ${libraryName}.`)); console.log(chalk.bold.redBright(`You need to build library ${libraryName} first.`)); } addDebugLog("publishLibrary", "END", "-"); return true; };