@pega/custom-dx-components
Version:
Utility for building custom UI components
192 lines (149 loc) • 5.32 kB
JavaScript
import { join } from 'path';
import { Listr } from 'listr2';
import inquirer from 'inquirer';
import chalk from 'chalk';
// eslint-disable-next-line import/order
import {
showVersion,
getPegaServerConfig,
getLibraryBased,
getLibraryBasedCL,
addDebugLog,
zipVersionAndArchive,
restoreFromArchive,
checkLibraryAndArchives,
getConfigDefaults,
getHaveDependencyDifference,
forceDefaultsUpdate,
modAddAboutData,
cleanUpTemp,
getUseInputConfig,
getInputConfigForCommand,
convertYNToBoolean,
hasArchives,
hasLibraryAndVersion
} from '../../util.js';
import { getSwitchVersionComponentQuestions } from './helper.js';
import { showCurrentStatus } from '../show-status/index.js';
export default async options => {
const isLibraryBased = getLibraryBased();
const isLibraryBasedCL = getLibraryBasedCL();
const useInputConfig = getUseInputConfig();
if (!isLibraryBased) {
console.log(`Command only supported for ${chalk.bold.green('library mode')} components.`);
process.exit();
}
await showVersion();
await checkLibraryAndArchives();
await cleanUpTemp();
addDebugLog("switchVersion", "", "+");
let libraryName;
let version;
// permBuild is the opposite of devBuild
let okToContinue;
const serverConfig = await getPegaServerConfig();
const currentDirectory = process.cwd();
const componentDirectory = join (currentDirectory, "src", "components");
const orgLib = getConfigDefaults();
// if we have chosen to use INPUT CONFIG FILE
// process and no inputs
if (useInputConfig) {
const inputConfig = await getInputConfigForCommand("switchLibVersion");
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);
}
okToContinue = convertYNToBoolean(inputConfig.okToContinue);
if (okToContinue) {
version = inputConfig.libraryVersion;
const isPermanent = inputConfig?.isPermanent || "N";
let permBuild = convertYNToBoolean(isPermanent);
if (isLibraryBasedCL) {
// no -dev for PegaInfinity
permBuild = true;
}
const hasArch = await hasArchives();
const orgLib = getConfigDefaults();
const hasLib = await hasLibraryAndVersion(orgLib.library, version, !permBuild);
// version here includes -dev if appropriate
if (!permBuild) {
version = version.concat("-dev");
}
if (hasArch) {
if (orgLib.buildVersion == version ) {
console.log(`${chalk.bold.red(`Your current library/version is already ${orgLib.displayLibVersion}.`)}`);
process.exit();
}
if (!hasLib) {
console.log(`${chalk.bold.red(`${orgLib.library}/${version} does not exist.`)}`);
process.exit();
}
}
else {
console.log(`${chalk.bold.red(`No libraries exist.`)}`);
console.log("You need to create a library first via 'npm run createLib'.\n")
process.exit();
}
}
else {
process.exit();
}
}
else {
if (options.params.length >= 5) {
const okToCont = options.params[3];
version = options.params[4];
// if have [5] will be "NoAbout" from Jest, to turn off build date
if (options.params.length >= 6 ) {
if (options.params[5] === "NoAbout") {
modAddAboutData(false);
}
}
okToContinue = !!(okToCont === 'Y' || okToCont === 'y' || okToCont === true || okToCont === 'true');
} else {
const questions = await getSwitchVersionComponentQuestions();
const answers = await inquirer.prompt(questions);
({ okToContinue, version } = answers);
}
}
if (okToContinue) {
// in this scenario, version contains "-dev", if appropriate
const newVersion = version;
const existingOrgLib = orgLib.currentOrgLib;
const oldVersion = orgLib.buildVersion;
const switchToFileName = `${existingOrgLib}_${newVersion}.arch.zip`;
console.log("");
const tasks = new Listr(
[
{
title: `Storing ${orgLib.displayLibVersion}`,
task: async () => {
await zipVersionAndArchive(existingOrgLib, oldVersion);
}
},
{
title: `Restoring ${existingOrgLib}/${newVersion}`,
task: async () => {
await restoreFromArchive(switchToFileName, existingOrgLib, newVersion);
}
}
],
{ concurrent: false, exitOnError: true }
);
await tasks.run().catch(err => {
console.log(chalk.bold.red(err.toString()));
process.exit(1);
});
console.log(`${chalk.bold.green(`\nSwitched to ${orgLib.currentOrgLib}/${newVersion}!\n`)}`);
await forceDefaultsUpdate();
await showCurrentStatus();
if (getHaveDependencyDifference()) {
console.log('\n***************************************************************');
console.log(chalk.bold.yellow('Dependencies have changed between versions, recommend updating.'));
console.log(`>>> PLEASE run ${chalk.bold.yellow("'npm update'")}.`);
console.log('***************************************************************\n');
}
addDebugLog("switchVersion", "END", "-");
}
return true;
};