@pega/custom-dx-components
Version:
Utility for building custom UI components
256 lines (185 loc) • 7.95 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { join } from 'path';
import inquirer from 'inquirer';
import { Listr } from 'listr2';
import chalk from 'chalk';
import { showVersion, getServerType, setServerType, getSubComponents, deleteLocalComponent, checkPathAccess, getComponents, getLibraryBased, setLibraryBased, forceDefaultsUpdate } from '../../util.js';
import createAll from '../create-all/index.js';
import create from '../create/index.js';
import createLib from '../create-lib/index.js';
import deleteLib from '../delete-lib/index.js';
import { JEST_ASSETS_ORIGINAL_PATH, JEST_ASSETS_LIBRARY_PATH, JEST_CREATE_PATH, JEST_CREATE_ALL_CONSTELLATION_PATH, JEST_CREATE_ALL_LAUNCHPAD_PATH, TASKS_CONFIG_JSON_FILENAME, COMPONENTS_DIRECTORY_PATH } from '../../constants.js';
const getBuildAssetsQuestions = async () => {
const message = "\n**IMPORTANT** Building Jest Assets will remove any local components in your /src/components directory\n\t and will also delete ALL local archives in your /store and is not reversable!!\n";
console.log(`${chalk.yellow.bold(message)}`);
return [
{
name: 'buildAssets',
type: 'confirm',
message: 'Generate Jest Assets ?',
default: false
}
];
};
const deleteTestComponent = async(path, componentKey) => {
const directory = join(path, componentKey);
try {
fs.rmSync(directory, { recursive: true });
console.log(`${chalk.red.bold(componentKey)} is deleted from assets`);
} catch (err) {
console.log('no file');
throw new Error(`No such file ${componentKey}`);
}
}
const copyAssets = async(destinationPath) => {
const currentDirectory = process.cwd();
const pegaConfigJsonPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
await checkPathAccess(pegaConfigJsonPath);
let data = fs.readFileSync(pegaConfigJsonPath, { encoding: 'utf8' });
data = JSON.parse(data);
if (!data[COMPONENTS_DIRECTORY_PATH]) {
console.error(
`${chalk.red.bold('ERROR')} Could not able find components directory path in config.json`
);
process.exit(1);
}
const srcDir = join(currentDirectory, data[COMPONENTS_DIRECTORY_PATH]);
fs.cpSync(srcDir, destinationPath, { recursive: true});
}
const deleteAssets = async() => {
const currentDirectory = process.cwd();
const pegaConfigJsonPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
await checkPathAccess(pegaConfigJsonPath);
let data = fs.readFileSync(pegaConfigJsonPath, { encoding: 'utf8' });
data = JSON.parse(data);
if (!data[COMPONENTS_DIRECTORY_PATH]) {
console.error(
`${chalk.red.bold('ERROR')} Could not able find components directory path in config.json`
);
process.exit(1);
}
const localComponents = await getComponents();
for (const index in localComponents) {
const componentToDelete = localComponents[index];
// console.log(chalk.bold.green("Deleting: " + componentToDelete));
// eslint-disable-next-line no-await-in-loop
const directory = join(currentDirectory, data[COMPONENTS_DIRECTORY_PATH], componentToDelete);
try {
fs.rmSync(directory, { recursive: true });
console.log(`${chalk.red.bold(componentToDelete)} is deleted from Local`);
} catch (err) {
console.log('no file');
throw new Error(`No such file ${componentToDelete}`);
}
}
// delete the store
}
const createJestOriginalAssets = async(isLaunchPad = false) => {
// initially delete local assets
await deleteAssets();
// const framework = await getFramework();
let args = [ '','', '', 'My', '0.0.1', 'DXIL', '', 'Pega'];
let myOptions = {};
myOptions["params"] = args;
await createAll(myOptions);
const jestCreateAllPath = isLaunchPad ? JEST_CREATE_ALL_LAUNCHPAD_PATH : JEST_CREATE_ALL_CONSTELLATION_PATH;
// goto Constellation
// get current test assets and delete them
let createAllPath = path.join(path.resolve(), JEST_ASSETS_ORIGINAL_PATH, jestCreateAllPath);
const fileList = await getSubComponents(createAllPath, '');
for (const index in fileList) {
const componentToDelete = fileList[index];
// console.log(chalk.bold.green("Deleting: " + componentToDelete));
// eslint-disable-next-line no-await-in-loop
await deleteTestComponent(createAllPath, componentToDelete);
}
await copyAssets(createAllPath);
await deleteAssets();
args = [ '','', '', 'Field', 'Text', 'MyTestText', 'My Test Text', '0.0.1','DXIL', '', 'My Test Text Description', 'Pega'];
myOptions = {};
myOptions["params"] = args;
await create(myOptions);
let createPath = path.join(path.resolve(), JEST_ASSETS_ORIGINAL_PATH, JEST_CREATE_PATH);
const myFileList = await getSubComponents(createPath, '');
for (const index in myFileList) {
const componentToDelete = myFileList[index];
// console.log(chalk.bold.green("Deleting: " + componentToDelete));
// eslint-disable-next-line no-await-in-loop
await deleteTestComponent(createPath, componentToDelete);
}
await copyAssets(createPath);
await deleteAssets();
}
const createJestLibraryAssets = async(isLaunchPad = false) => {
// initially delete local assets
await deleteAssets();
let args = ['', '', '', 'DXIL', '0.0.1', 'N'];
let myOptions = {};
myOptions["params"] = args;
await createLib(myOptions);
// const framework = await getFramework();
args = [ '', '', '', 'My', '0.0.1', 'DXIL', '', 'Pega'];
myOptions = {};
myOptions["params"] = args;
await createAll(myOptions);
const jestCreateAllPath = isLaunchPad ? JEST_CREATE_ALL_LAUNCHPAD_PATH : JEST_CREATE_ALL_CONSTELLATION_PATH;
// goto Constellation
// get current test assets and delete them
let createAllPath = path.join(path.resolve(), JEST_ASSETS_LIBRARY_PATH, jestCreateAllPath);
const fileList = await getSubComponents(createAllPath, '');
for (const index in fileList) {
const componentToDelete = fileList[index];
// console.log(chalk.bold.green("Deleting: " + componentToDelete));
// eslint-disable-next-line no-await-in-loop
await deleteTestComponent(createAllPath, componentToDelete);
}
await copyAssets(createAllPath);
await deleteAssets();
args = [ '','', '', 'Field', 'Text', 'MyTestText', 'My Test Text', '0.0.1','DXIL', '', 'My Test Text Description', 'Pega'];
myOptions = {};
myOptions["params"] = args;
await create(myOptions);
let createPath = path.join(path.resolve(), JEST_ASSETS_LIBRARY_PATH, JEST_CREATE_PATH);
const myFileList = await getSubComponents(createPath, '');
for (const index in myFileList) {
const componentToDelete = myFileList[index];
// console.log(chalk.bold.green("Deleting: " + componentToDelete));
// eslint-disable-next-line no-await-in-loop
await deleteTestComponent(createPath, componentToDelete);
}
await copyAssets(createPath);
await deleteAssets();
args = [ '','', '', 'Pega_DXIL', '0.0.1-dev'];
myOptions = {};
myOptions["params"] = args;
await deleteLib(myOptions);
}
export default async options => {
await showVersion();
const questions = await getBuildAssetsQuestions();
const answers = await inquirer.prompt(questions);
const { buildAssets } = answers;
if (buildAssets) {
// get current server type
const savedServerType = await getServerType();
const savedLibraryBased = getLibraryBased();
await setLibraryBased(false);
// Constellation
await setServerType("infinity");
await createJestOriginalAssets(false);
//Launchpad
await setServerType("launchpad");
await createJestOriginalAssets(true);
// Constellation Library
await setLibraryBased(true);
await setServerType("infinity");
await createJestLibraryAssets(false);
//Launchpad
// await setServerType("launchpad");
// await createJestLibraryAssets(true);
// restore serverType
await setServerType(savedServerType);
await setLibraryBased(savedLibraryBased);
}
};