@pega/custom-dx-components
Version:
Utility for building custom UI components
306 lines (243 loc) • 8.49 kB
JavaScript
import inquirer from 'inquirer';
import { Listr } from 'listr2';
import chalk from 'chalk';
import fs from 'fs';
import validate from '../validator/index.js';
import bundleComponent from '../bundle/index.js';
import { lintComponent } from '../linter/index.js';
import {
getComponentDirectoryPath,
getPegaServerConfig,
showVersion,
getLibraryBased,
getLibraryBasedCL,
getUseInputConfig,
addDebugLog,
updateServerConfigRuleSetAndVersion,
checkLibraryAndArchives,
getConfigDevBuild,
checkAccessTokenExpiration,
checkJWTExpiration,
getConfigDefaults,
getInputConfigForCommand,
convertYNToBoolean,
checkForBranch,
getApplicationAndVersionFromToken,
zipVersionAndArchive,
applyPegaData } from '../../util.js';
import { getPublishComponentQuestions, publishComponentToServer, zipComponent } from './helper.js';
import { default as buildLibrary } from '../build-lib/index.js';
import { default as publishLibrary } from '../publish-lib/index.js';
import {
TOKEN_PATH
} from '../../constants.js';
export default async options => {
await showVersion();
await checkLibraryAndArchives();
const isLibraryBased = getLibraryBased();
const isLibraryBasedCL = getLibraryBasedCL();
const useInputConfig = getUseInputConfig();
addDebugLog("publish", "", "+");
await checkAccessTokenExpiration();
//
// library based V2 publishing
//
if (isLibraryBased) {
await checkJWTExpiration();
const devBuild = await getConfigDevBuild();
let okToPublish = false;
// let content;
const orgLib = getConfigDefaults();
const currentOrgLib = orgLib.currentOrgLib;
// const currentVersion = devBuild ? `${orgLib.version}-dev`: orgLib.version;
const currentVersion = orgLib.buildVersion;
const serverConfig = await getPegaServerConfig();
let configJson = {};
await applyPegaData(configJson);
const {app_name, app_version, sub } = await getApplicationAndVersionFromToken();
console.log();
console.log(`Publishing to server: ${chalk.green.bold(`${serverConfig.server}`)}`);
console.log(`Cosmos version: ${chalk.green.bold(`${configJson.packageCosmosVersion}`)}`);
console.log(`Application: ${chalk.green.bold(`${app_name} - ${app_version}`)} for ${chalk.magentaBright.bold(`${sub}`)}`);
console.log();
if (useInputConfig) {
const inputConfig = await getInputConfigForCommand("publish");
if (!inputConfig || Object.keys(inputConfig).length === 0) {
console.log(chalk.redBright("Configured for input config, but no input.config.json file found."));
process.exit(1);
}
const okToPubPerm = inputConfig?.okToPublishPermanent || "Y";
const okToPublishPerm = convertYNToBoolean(okToPubPerm);
// console.log("Input config: ");
// console.log(inputConfig);
if (inputConfig.okToPublish ) {
if (devBuild || (!devBuild && okToPublishPerm)) {
okToPublish = true;
}
}
options.params.push(inputConfig.ruleSetName);
options.params.push(inputConfig.ruleSetVersion);
}
else {
const publishThisVersion = await inquirer.prompt([
{
name: 'confirmPublish',
type: 'confirm',
message: `Publish ${chalk.bold.green(`${orgLib.displayLibVersion}`)}, proceed ?`,
default: true
}
]);
if (publishThisVersion.confirmPublish) {
okToPublish = true;
}
}
if (okToPublish) {
if (options.params.length >= 8 || useInputConfig) {
// const rulesetName = options.params[3];
// const rulesetVersion = options.params[4]
// content = { rulesetName, rulesetVersion };
} else {
// VS Code plugin would send in the component
let paramComponentKey;
if (options.params.length == 4) {
paramComponentKey = options.params[3];
}
const questions = await getPublishComponentQuestions("LibraryMode");
const answers = await inquirer.prompt(questions);
// content = {
// ...answers
// };
options.params.push(answers.rulesetName);
options.params.push(answers.rulesetVersion);
await checkForBranch(answers.rulesetName);
updateServerConfigRuleSetAndVersion(answers.rulesetName, answers.rulesetVersion);
}
}
if (okToPublish && !devBuild && !isLibraryBasedCL) {
okToPublish = false;
console.log("");
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 (okToPublish) {
// marking as internal, so no questions will be ask
options.params.push("internal");
try {
await buildLibrary(options);
await zipVersionAndArchive(orgLib.currentOrgLib, orgLib.buildVersion);
await publishLibrary(options);
}
catch (err) {
console.log(chalk.redBright(err));
process.exit(1);
}
}
// end of V2 publishing
return true;
}
// otherwise continue with original V1 publishing of individual components
//
// original V1 publish
//
let OauthData;
try {
OauthData = fs.readFileSync(TOKEN_PATH, 'utf8');
}
catch (ex) {
}
if (!OauthData) {
console.log(chalk.redBright("Need to authenticate, missing services token."));
process.exit(1);
}
// non library based publish
let componentKey;
let sourceMap;
let devBuild;
let content;
let doFetch = true;
// const defaultPegaConfig = await getPegaServerConfig();
const componentDefaults = getConfigDefaults();
if (options.params.length >= 7) {
componentKey = options.params[3];
const rulesetName = options.params[4];
const rulesetVersion = options.params[5];
const dBuild = options.params[6];
devBuild = dBuild === 'Y' || dBuild === 'y' || dBuild === true || dBuild === 'true';
await checkForBranch(rulesetName);
// eslint-disable-next-line no-unneeded-ternary,no-nested-ternary
doFetch = options.params.length >= 7 ? (options.params[7] === 'noFetch' ? false : true) : true;
content = { componentKey, rulesetName, rulesetVersion, devBuild };
} else {
// VS Code plugin would send in the component
let paramComponentKey;
if (options.params.length == 4) {
paramComponentKey = options.params[3];
}
const questions = await getPublishComponentQuestions(paramComponentKey);
const answers = await inquirer.prompt(questions);
({ componentKey = paramComponentKey, sourceMap, devBuild } = answers);
content = {
...answers
};
}
await checkForBranch(content.rulesetName);
// update ruleset and version config for future
if (componentDefaults.serverType === "infinity") {
updateServerConfigRuleSetAndVersion(content.rulesetName, content.rulesetVersion);
}
const sDevBuild = devBuild ? '(dev build)' : '';
const tasks = 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);
}
},
{
title: `Bundle Component ${sDevBuild}`,
task: async () => bundleComponent(componentKey, sourceMap, devBuild),
skip: () => (options.skipBundle ? 'Skipped bundling component' : undefined)
},
{
title: 'Zip Component',
task: async () => {
const output = await zipComponent(componentKey);
content = { ...content, ...output };
}
},
{
title: 'Publish Component',
task: async () => {
await publishComponentToServer(content, doFetch);
}
}
],
{
exitOnError: true
}
);
await tasks.run().catch(err => {
console.log(chalk.bold.red(err.toString()));
process.exit(1);
});
addDebugLog("publish", "END", "-");
return true;
};