@pega/custom-dx-components
Version:
Utility for building custom UI components
390 lines (293 loc) • 11.5 kB
JavaScript
import fs from 'fs';
import { promisify } from 'util';
import { join } from 'path';
import path from 'path';
import inquirer from 'inquirer';
import ncp from 'ncp';
import chalk from 'chalk';
import {
checkPathAccess,
showVersion,
addDebugLog,
checkLibraryAndArchives,
getConfigDefaults,
getLibraryBased,
zipVersionAndArchive,
zipVersionAndArchiveCL,
cleanUpTemp
} from '../../util.js';
import { TASKS_CONFIG_JSON_FILENAME, ARCHIVES_PATH, BIN_ARCHIVES_PATH } from '../../constants.js';
import { getFilePathQuestions, getLibraryQuestion, getLibraryVersionQuestion, updateSavedExportFilePath, getFullArchiveQuestions } from './helper.js';
import { localBuildLibrary } from '../build-lib/index.js';
export const DXCB_CONFIG_INTERNAL_JSON_FILENAME = 'src/dxcb.config.json';
const currentDirectory = process.cwd();
const pegaConfigJsonPath = join(currentDirectory, TASKS_CONFIG_JSON_FILENAME);
const copy = promisify(ncp);
export const updateConfig = async (
{
oldComponentKey,
newComponentKey,
library,
version,
targetDirectory,
},
options,
onlyCompanion = false
) => {
addDebugLog("updateConfig", `oldComponentKey: ${oldComponentKey}, newComponentKey: ${newComponentKey}`, "");
let configData = fs.readFileSync(join(targetDirectory, "/config.json"), { encoding: 'utf8' });
configData = configData && JSON.parse(configData);
configData.name = newComponentKey;
if (configData.componentKey) configData.componentKey = newComponentKey;
configData.library = library;
configData.version = version;
// stringify "4", makes the json string look like JSON in the file, formated instead of a single line
fs.writeFileSync(join(targetDirectory, "/config.json"), JSON.stringify(configData, null, 4), { encoding: 'utf8',flag:'w' });
};
export const updateFile = async (
fileName,
oldComponentKeyPC,
newComponentKeyPC,
targetDirectory,
) => {
addDebugLog("updateFile", `fileName: ${fileName}, oldComponentKeyPC: ${oldComponentKeyPC}, newComponentKeyPC: ${newComponentKeyPC}, targetDirectory: ${targetDirectory}`, "");
let fileData = fs.readFileSync(join(targetDirectory, "/", fileName), { encoding: 'utf8' });
if (fileData.indexOf(oldComponentKeyPC) >= 0) {
fileData = fileData.replaceAll(oldComponentKeyPC, newComponentKeyPC);
fs.writeFileSync(join(targetDirectory, "/", fileName), fileData, { encoding: 'utf8', flag:'w' });
}
};
export const exportCLArch = async(exportPath, orgLibName, selectedVersion) => {
addDebugLog("exportCLArch", `exportPath: ${exportPath}, orgLibName: ${orgLibName}, selectedVersion: ${selectedVersion}`, "");
const currentDirectory = process.cwd();
const archiveDirectory = join (currentDirectory, BIN_ARCHIVES_PATH);
const fileName = `${orgLibName}_${selectedVersion}.zip`;
const archFileName = join(archiveDirectory, fileName);
const configDef = await getConfigDefaults();
const exportFile = join(exportPath, fileName);
let output;
// need to build the libary
console.log(chalk.green(`\nBuilding binaries...\n`));
const arOrgLib = orgLibName.split("_")
const arVersion = selectedVersion.split("-");
const devBuild = arVersion.length > 1 ? true : false;
await localBuildLibrary(arOrgLib[0], arOrgLib[1], arVersion[0], devBuild);
const libraryDir = join(currentDirectory, orgLibName, selectedVersion );
if (fs.existsSync(libraryDir)) {
console.log(chalk.green(`\nExporting binaries...\n`));
if (!fs.existsSync(exportPath)) {
console.log(`\Path: ${chalk.yellow(`${exportPath}`)} doesn't exist.`);
const newPathAnswer = await inquirer.prompt([
{
name: 'createNewPath',
type: 'confirm',
message: 'Create path',
default: true
},
]);
const createNewPath = newPathAnswer.createNewPath;
if (createNewPath) {
fs.mkdirSync(exportPath, { recursive: true });
await updateSavedExportFilePath(exportPath);
}
else {
console.log(chalk.green("Store not exported."));
process.exit();
}
}
if (fs.existsSync(exportFile)) {
console.log(`\nFile: ${chalk.yellow(`${fileName}`)} already exists in ${chalk.yellow(`${exportPath}`)}.`);
const overwriteQuestion = await inquirer.prompt([
{
name: 'overWriteFile',
type: 'confirm',
message: 'Overrwrite',
default: true
},
]);
if (!overwriteQuestion.overWriteFile) {
console.log(chalk.green("Store not exported."));
process.exit();
}
}
// archive it
output = await zipVersionAndArchiveCL(configDef.currentOrgLib, configDef.buildVersion);
// if archiving, need to wait before copying
output.on('close', function() {
fs.copyFileSync(archFileName, exportFile );
console.log(`\n${chalk.green(`${fileName}`)} has been exported to ${chalk.green(`${exportPath}`)}`);
});
}
else {
console.log.chalk.green(`\nBinary does not exist.`);
}
}
export const exportArch = async(exportPath, orgLibName, selectedVersion, fullArchive = "Full", currentOrArchive = "" ) => {
addDebugLog("exportArch", `exportPath: ${exportPath}, orgLibName: ${orgLibName}, selectedVersion: ${selectedVersion}`, "");
const currentDirectory = process.cwd();
const archiveDirectory = join (currentDirectory, ARCHIVES_PATH, `${orgLibName}`, `${selectedVersion}`);
const fileName = `${orgLibName}_${selectedVersion}.zip`;
const archFileName = join(archiveDirectory, fileName);
const configDef = await getConfigDefaults();
const exportFile = join(exportPath, fileName);
let output;
let archiveBeforeStore = false;
if (fullArchive != "Full") {
await exportCLArch(exportPath, orgLibName, selectedVersion);
return;
}
if (orgLibName === configDef.currentOrgLib && selectedVersion === configDef.buildVersion) {
let currentOrStore = currentOrArchive;
if (currentOrArchive === "") {
console.log("\nYou've selected the current working library/version.");
console.log("Export store or the current.");
const archAnswers = await inquirer.prompt([
{
name: 'currentOrStore',
type: 'rawlist',
message: 'Select current or store',
default: "Current",
choices: ["Current", "Store"]
},
]);
currentOrStore = archAnswers.currentOrStore;
}
if (currentOrStore === "Current") {
archiveBeforeStore = true;
}
}
if (!fs.existsSync(exportPath)) {
console.log(`\Path: ${chalk.yellow(`${exportPath}`)} doesn't exist.`);
const newPathAnswer = await inquirer.prompt([
{
name: 'createNewPath',
type: 'confirm',
message: 'Create path',
default: true
},
]);
const createNewPath = newPathAnswer.createNewPath;
if (createNewPath) {
fs.mkdirSync(exportPath, { recursive: true });
await updateSavedExportFilePath(exportPath);
}
else {
console.log(chalk.green("Store not exported."));
process.exit();
}
}
if (fs.existsSync(exportFile)) {
console.log(`\nFile: ${chalk.yellow(`${fileName}`)} already exists in ${chalk.yellow(`${exportPath}`)}.`);
const overwriteQuestion = await inquirer.prompt([
{
name: 'overWriteFile',
type: 'confirm',
message: 'Overrwrite',
default: true
},
]);
if (!overwriteQuestion.overWriteFile) {
console.log(chalk.green("Store not exported."));
process.exit();
}
}
if (archiveBeforeStore) {
// archive it
output = await zipVersionAndArchive(configDef.currentOrgLib, configDef.buildVersion);
// if archiving, need to wait before copying
output.on('close', function() {
fs.copyFileSync(archFileName, exportFile );
console.log(`\n${chalk.green(`${fileName}`)} has been exported to ${chalk.green(`${exportPath}`)}`);
});
}
else {
fs.copyFileSync(archFileName, exportFile );
console.log(`\n${chalk.green(`${fileName}`)} has been exported to ${chalk.green(`${exportPath}`)}`);
}
}
export const getZipFileList = async(filePath) => {
if (fs.existsSync(filePath)) {
return fs
.readdirSync(filePath, { withFileTypes: true })
.filter(dirent => !dirent.isDirectory() && dirent.name.match(/.*\.(zip?)/ig))
.map(dirent => dirent.name);
}
else {
console.log(chalk.red.bold(`File path: ${filePath} does not exist or no permissions.`));
process.exit(1);
}
}
export default async options => {
await showVersion();
await checkLibraryAndArchives();
await checkPathAccess(pegaConfigJsonPath);
const isLibraryBased = getLibraryBased();
if (!isLibraryBased) {
console.log(`Command only supported for ${chalk.bold.green('library mode')} components.`)
process.exit();
}
await cleanUpTemp();
addDebugLog("exportArchive", "", "+");
if (options.params.length >= 8) {
let fullArchive = options.params[3];
const libraryName = options.params[4];
const libraryVersion = options.params[5];
const filePath = options.params[6];
const currentOrArchive = options.params[7];
await exportArch(filePath, libraryName, libraryVersion, fullArchive, currentOrArchive );
}
else {
let orgLibName = "";
let selectedVersion = "";
let fullArchive = "";
if (options.params.length >= 5) {
fullArchive = options.params[3];
orgLibName = options.params[4];
if (options.params.length >= 6) {
selectedVersion = options.params[5];
}
}
if (fullArchive === "" ) {
const fullArchiveQuestions = await getFullArchiveQuestions();
const fullArchiveAnswers = await inquirer.prompt(fullArchiveQuestions);
({ fullArchive } = fullArchiveAnswers);
}
let filePathQuestions;
let filePathAnswers;
let filePath;
// if Binary, then we need to just build the current library, get the results and
// and export an archive as zip
if (fullArchive === "Binary") {
filePathQuestions = await getFilePathQuestions();
filePathAnswers = await inquirer.prompt(filePathQuestions);
({ filePath } = filePathAnswers);
await updateSavedExportFilePath(filePath);
const configDef = getConfigDefaults();
orgLibName = configDef.currentOrgLib;
selectedVersion = configDef.buildVersion;
}
else {
if (orgLibName === "") {
const libraryQuestion = await getLibraryQuestion();
const libraryAnswers = await inquirer.prompt(libraryQuestion);
({ orgLibName } = libraryAnswers);
}
if (selectedVersion === "") {
const libraryVersionQuestion = await getLibraryVersionQuestion(orgLibName);
const libraryVersionAnswer = await inquirer.prompt(libraryVersionQuestion);
({ selectedVersion } = libraryVersionAnswer);
}
filePathQuestions = await getFilePathQuestions();
filePathAnswers = await inquirer.prompt(filePathQuestions);
({ filePath } = filePathAnswers);
await updateSavedExportFilePath(filePath);
}
// if (path.sep === "/") {
// if (filePath.lastIndexOf(path.sep) != filePath.length - 1) {
// filePath = filePath.concat(path.sep);
// }
// }
await exportArch( filePath, orgLibName, selectedVersion, fullArchive);
}
addDebugLog("exportArchive", "END", "-");
};