@pega/custom-dx-components
Version:
Utility for building custom UI components
191 lines (154 loc) • 5.61 kB
JavaScript
import { join } from 'path';
import fs from 'fs';
import inquirer from 'inquirer';
import chalk from 'chalk';
// eslint-disable-next-line import/order
import {
showVersion,
updateComponentDefaultVersion,
getLibraryBased,
getLibraryBasedCL,
addDebugLog,
zipVersionAndArchive,
setConfigDevBuild,
checkLibraryAndArchives,
getConfigDefaults,
forceDefaultsUpdate,
modAddAboutData,
cleanUpTemp,
getUseInputConfig,
getInputConfigForCommand,
convertYNToBoolean,
hasArchives,
hasLibraryAndVersion
} from '../../util.js';
import { getCreateVersionComponentQuestions, updateAllComponentsConfig, getCreateVersionComponentCLQuestions } 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("createVersion", "", "+");
let libraryName;
let version;
// permBuild is the opposite of devBuild
let permBuild;
let copyExisting;
const componentDefaults = getConfigDefaults();
const currentDirectory = process.cwd();
const componentDirectory = join (currentDirectory, "src", "components");
// if we have chosen to use INPUT CONFIG FILE
// process and no inputs
if (useInputConfig) {
const inputConfig = await getInputConfigForCommand("createLibVersion");
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)) {
version = inputConfig.libraryVersion;
const isPermanent = inputConfig?.isPermanent || "N";
permBuild = convertYNToBoolean(isPermanent);
copyExisting = convertYNToBoolean(inputConfig.copyComponents);
if (isLibraryBasedCL) {
// no -dev for PegaInfinity
permBuild = true;
}
const hasArch = await hasArchives();
const orgLib = getConfigDefaults();
const hasLib = await hasLibraryAndVersion(orgLib.library, version, !permBuild);
if (hasArch) {
if (hasLib) {
console.log(`${chalk.bold.red(`Library ${orgLib.displayLibVersion} already exists.`)}`);
console.log("You can switch to that library by entering 'npm run switchLib'.\n")
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 >= 6) {
version = options.params[3];
const dPerm = options.params[4];
const copyExist = options.params[5]
// if have [6] will be "NoAbout" from Jest, to turn off build date
if (options.params.length >= 7 ) {
if (options.params[6] === "NoAbout") {
modAddAboutData(false);
}
}
permBuild = !!(dPerm === 'Y' || dPerm === 'y' || dPerm === true || dPerm === 'true');
copyExisting = !!(copyExist === 'Y' || copyExist === 'y' || copyExist === true || copyExist === 'true');
} else {
if (isLibraryBasedCL) {
const questions = await getCreateVersionComponentCLQuestions();
const answers = await inquirer.prompt(questions);
({ version, copyExisting } = answers);
}
else {
const questions = await getCreateVersionComponentQuestions();
const answers = await inquirer.prompt(questions);
({ version, permBuild, copyExisting } = answers);
}
}
}
if (isLibraryBasedCL) {
// no -dev for PegaInfinity
permBuild = true;
}
const orgLib = getConfigDefaults();
const savedNewVersion = version;
const existingOrgLib = orgLib.currentOrgLib;
let oldVersion = componentDefaults.version;
if (componentDefaults.devBuild && !isLibraryBasedCL) {
oldVersion = oldVersion.concat("-dev");
}
if (!permBuild) {
version = version.concat("-dev");
}
// store existing
await zipVersionAndArchive(existingOrgLib, oldVersion);
if (copyExisting) {
// change all existing config to new version
await updateAllComponentsConfig(version);
}
else {
// wipe out /src/components
try {
fs.rmSync(componentDirectory, { recursive: true });
addDebugLog("createVersion", `clear out: ${componentDirectory}`, "");
} catch (err) {
console.log(`No such directory: ${componentDirectory} `);
throw new Error(`No such file ${componentDirectory}`);
}
// create a blank directory
addDebugLog("createVersion", `make new directory: ${componentDirectory}`, "");
fs.mkdirSync(componentDirectory, { recursive: true });
}
// update config with new version
await updateComponentDefaultVersion(savedNewVersion);
await setConfigDevBuild(!permBuild);
// create archive of new
await zipVersionAndArchive(existingOrgLib, version);
console.log(`${chalk.bold.green(`\nLibrary ${existingOrgLib}/${oldVersion} has been stored.`)}`);
console.log(`${chalk.bold.green(`Library ${existingOrgLib}/${version} has been created!\n`)}`);
await forceDefaultsUpdate();
await showCurrentStatus();
addDebugLog("createVersion", "END", "-");
return true;
};