@pega/custom-dx-components
Version:
Utility for building custom UI components
294 lines (221 loc) • 8.46 kB
JavaScript
import inquirer from 'inquirer';
import chalk from 'chalk';
import {
showVersion,
getLibraryBased,
checkLibraryAndArchives,
addDebugLog,
getConfigDefaults,
getLibraryArchivesVersions,
deleteLocalArchiveLibrary,
deleteLocalArchiveLibraryVersion,
deleteLocalComponent,
getHaveDependencyDifference,
getComponents,
getLibraryArchiveDirectories,
deleteArchives,
forceDefaultsUpdate,
restoreFromArchive,
cleanUpTemp,
getUseInputConfig,
getInputConfigForCommand,
convertYNToBoolean
} from '../../util.js';
import { getDeleteLibraryQuestions, getLibraryVersionQuestion } from './helper.js';
import { showCurrentStatus } from '../show-status/index.js';
export const deleteLibraryVersion = async (libraryName, version = "", okToDelete) => {
addDebugLog("deleteLibraryVersion", "", "+");
const isLibraryBased = getLibraryBased();
const compDef = getConfigDefaults();
let libVersions = [];
let confirmDeletion;
if (!isLibraryBased) {
console.log(`Command only supported for ${chalk.bold.green('library mode')} components.`);
process.exit();
}
try {
libVersions = await getLibraryArchivesVersions(libraryName, "");
}
catch (ex) {}
if (version === "" || version === "ALL") {
if (libVersions.length > 0) {
// ALL option
if (version !== "ALL") {
if (libVersions.length > 1) {
libVersions.unshift('ALL');
}
const versionQuestions = await getLibraryVersionQuestion(libraryName, libVersions);
const versionAnswers = await inquirer.prompt(versionQuestions);
({version, confirmDeletion} = versionAnswers);
if (!confirmDeletion) {
console.log(chalk.green(`Library not deleted.`));
process.exit();
}
}
if (okToDelete === undefined) {
if (libraryName === compDef.currentOrgLib && (version === "ALL" || version === compDef.buildVersion)) {
console.log(`\n${chalk.bold.yellow(`${compDef.displayLibVersion}`)} is your ${chalk.yellow('CURRENT')} library/version.`);
console.log(chalk.bold.yellow('Your current components will be DELETED!!!'))
const confirmAnswers = await inquirer.prompt([
{
name: 'confirmDeletion',
type: 'confirm',
message: 'Ok to delete?',
default: false
}
]);
if (!confirmAnswers.confirmDeletion) {
console.log(chalk.green(`Library not deleted.`));
process.exit();
}
}
}
else if (!okToDelete) {
console.log(chalk.green(`Library not deleted.`));
process.exit();
}
}
else {
// No library with that name
console.log(chalk.redBright(`No local library named: ${libraryName} exists.`));
addDebugLog("deleteLibraryVersion", "END", "-");
process.exit();
}
}
else {
if (okToDelete === undefined) {
if (libraryName === compDef.currentOrgLib && (version === "ALL" || version === compDef.buildVersion)) {
console.log(`\n${chalk.bold.yellow(`${compDef.displayLibVersion}`)} is your ${chalk.yellow('CURRENT')} library/version.`);
console.log(chalk.bold.yellow('Your current components will be DELETED!!!'))
const confirmAnswers = await inquirer.prompt([
{
name: 'confirmDeletion',
type: 'confirm',
message: 'Ok to delete?',
default: false
}
]);
if (!confirmAnswers.confirmDeletion) {
console.log(chalk.green(`Library not deleted.`));
process.exit();
}
}
}
else if (!okToDelete) {
console.log(chalk.green(`Library not deleted.`));
process.exit();
}
}
// so if going to remove the only version, then remove ALL
if (version === 'ALL' || libVersions.length == 1) {
await deleteLocalArchiveLibrary(libraryName);
}
else {
await deleteLocalArchiveLibraryVersion(libraryName, version);
}
if (libraryName === compDef.currentOrgLib && (version === "ALL" || version === compDef.buildVersion)) {
// delete all components
const localComponents = await getComponents()
for (const component of localComponents) {
// console.log(chalk.bold.green("Deleting: " + componentToDelete));
// eslint-disable-next-line no-await-in-loop
await deleteLocalComponent(component);
}
// switch to another version, if no versions to switch to then delete /store so will be back to state of nothing
const archiveLibraries = await getLibraryArchiveDirectories("");
if (archiveLibraries && archiveLibraries.length > 0) {
if (archiveLibraries.includes(libraryName)) {
const archLibVersions = await getLibraryArchivesVersions(libraryName, "");
const lastVersion = archLibVersions[archLibVersions.length -1];
const switchToFileName = `${libraryName}_${lastVersion}.zip`;
await restoreFromArchive(switchToFileName, libraryName, lastVersion);
await forceDefaultsUpdate();
const configDef = getConfigDefaults();
console.log(`\nSwitched to ${chalk.green(`${configDef.displayLibVersion}`)}`);
await showCurrentStatus();
}
else {
// pick first
const newLib = archiveLibraries[0];
const archLibVersions = await getLibraryArchivesVersions(newLib, "");
const lastVersion = archLibVersions[archLibVersions.length -1];
const switchToFileName = `${newLib}_${lastVersion}.zip`;
await restoreFromArchive(switchToFileName, newLib, lastVersion);
await forceDefaultsUpdate();
const configDef = getConfigDefaults();
console.log(`\nSwitched to ${chalk.green(`${configDef.displayLibVersion}`)}`);
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');
}
}
}
else {
// no libraries, so remove "/store"
await deleteArchives();
}
}
else {
// switch to another version, if no versions to switch to then delete /store so will be back to state of nothing
const archiveLibraries = await getLibraryArchiveDirectories("");
if (!archiveLibraries || archiveLibraries.length === 0) {
// no libraries, so remove "/store"
await deleteArchives();
}
}
addDebugLog("deleteLibraryVersion", "END", "-");
}
export default async (options) => {
const isLibraryBased = getLibraryBased();
const useInputConfig = getUseInputConfig();
if (!isLibraryBased) {
console.log(`Command only supported for ${chalk.bold.green('library based')} components.`)
process.exit();
}
await showVersion();
await checkLibraryAndArchives();
await cleanUpTemp();
addDebugLog("deleteLibrary", "", "+");
let version = "";
let okToDelete;
const compDef = getConfigDefaults();
let libraryName;
if (useInputConfig) {
const inputConfig = await getInputConfigForCommand("deleteLib");
libraryName = inputConfig.libraryName;
version = inputConfig.libraryVersion;
const isPermanent = inputConfig.isPermanent;
const permBuild = convertYNToBoolean(isPermanent);
if (!permBuild && version != "ALL") {
version = version.concat("-dev");
}
okToDelete = convertYNToBoolean(inputConfig.confirmDelete);
}
else {
if (options.params.length >= 5) {
libraryName = options.params[3];
version = options.params[4];
if (options.params.length >=6 ) {
const okToDel = options.params[5];
okToDelete = !!(okToDel === 'Y' || okToDel === 'y' || okToDel === true || okToDel === 'true');
}
}
else {
if (options.params.length === 4) {
libraryName = options.params[3];
version = "ALL"
}
else {
const questions = await getDeleteLibraryQuestions();
const answers = await inquirer.prompt(questions);
({ libraryName } = answers);
}
}
}
await deleteLibraryVersion(libraryName, version, okToDelete);
addDebugLog("deleteLibrary", "END", "-");
return true;
};