@pega/custom-dx-components
Version:
Utility for building custom UI components
209 lines (162 loc) • 5.86 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,
getLibraryBased,
addDebugLog,
zipVersionAndArchive,
restoreFromArchive,
checkLibraryAndArchives,
getConfigDefaults,
forceDefaultsUpdate,
getHaveDependencyDifference,
cleanUpTemp,
getLibraryArchiveDirectories,
getUseInputConfig,
getInputConfigForCommand,
convertYNToBoolean,
hasArchives,
getLibraryBasedCL
} from '../../util.js';
import { getSwitchLibraryComponentQuestions, getSwitchVersionComponentQuestions } from './helper.js';
import { showCurrentStatus } from '../show-status/index.js';
import { arch } from 'os';
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("switchLibrary", "", "+");
let libraryName;
let version;
let newOrgLib = "";
let inputOrgLib = "";
// permBuild is the opposite of devBuild
let okToContinue;
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("switchLib");
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);
}
if (convertYNToBoolean(inputConfig.okToContinue)) {
const newLibraryName = inputConfig.libraryName;
newOrgLib = `${orgLib.organization}_${newLibraryName}`;
version = inputConfig.libraryVersion;
const isPermanent = inputConfig?.isPermanent || "N";
let permBuild = convertYNToBoolean(isPermanent);
if (isLibraryBasedCL) {
// no -dev for PegaInfinity
permBuild = true;
}
const hasArch = await hasArchives();
if (hasArch) {
if (orgLib.currentOrgLib === newOrgLib) {
console.log(`${chalk.bold.red(`Your current library is already ${orgLib.displayLibVersion}`)}`);
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();
}
// version here includes -dev if appropriate
if (!permBuild) {
version = version.concat("-dev");
}
}
else {
process.exit();
}
}
else {
// params are in different order then questions, because want of VS Code plugin
if (options.params.length >= 6) {
newOrgLib = options.params[3];
version = options.params[4];
const okToCont = options.params[5];
okToContinue = !!(okToCont === 'Y' || okToCont === 'y' || okToCont === true || okToCont === 'true');
} else {
if (options.params.length >= 5) {
inputOrgLib = options.params[3];
version = options.params[4];
}
else if (options.params.length >= 4) {
inputOrgLib = options.params[3];
}
const orgLib = getConfigDefaults();
const archVersionList = await getLibraryArchiveDirectories(orgLib.currentOrgLib);
if (archVersionList.length === 0) {
console.log(chalk.red(`No other available libraries to switch to.`));
process.exit();
}
const questions = await getSwitchLibraryComponentQuestions(archVersionList, inputOrgLib);
const answers = await inquirer.prompt(questions);
({ okToContinue, newOrgLib } = answers);
if (!answers.okToContinue) {
process.exit()
}
if (newOrgLib === ""|| newOrgLib === undefined ) {
newOrgLib = inputOrgLib;
}
if (version === "" || version === undefined) {
const nextQuestion = await getSwitchVersionComponentQuestions(newOrgLib);
const nextAnswers = await inquirer.prompt(nextQuestion);
({ version } = nextAnswers);
}
}
}
// in this scenario, version contains "-dev", if appropriate
const newVersion = version;
const existingOrgLib = orgLib.currentOrgLib;
let oldVersion = orgLib.buildVersion;
const switchToFileName = `${newOrgLib}_${newVersion}.arch.zip`;
console.log("");
const tasks = new Listr(
[
{
title: `Storing ${orgLib.displayLibVersion}`,
task: async () => {
await zipVersionAndArchive(existingOrgLib, oldVersion);
}
},
{
title: `Restoring ${newOrgLib}/${newVersion}`,
task: async () => {
await restoreFromArchive(switchToFileName, newOrgLib, 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 ${newOrgLib}/${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("switchLibrary", "END", "-");
return true;
};