@pega/custom-dx-components
Version:
Utility for building custom UI components
319 lines (244 loc) • 9.09 kB
JavaScript
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import { join } from 'path';
import { Listr } from 'listr2';
import { addDebugLog, getConfigDefaults, convertIntoPascalCase, getComponents, getComponentDirectoryPath, getDirectoryFiles, setConfigImportRelativePath, forceDefaultsUpdate } from '../../util.js';
import { TASKS_CONFIG_JSON_FILENAME, TEMP_PATH, PACKAGE_JSON_FILENAME } from '../../constants.js';
export const SOURCE_OF_COMPONENT_TYPES = {
SERVER: 'Server',
LOCAL: 'Local'
};
export const setTasksConfig = async (libraryName, version, devBuild, currDir) => {
addDebugLog("setTasksConfig", `libraryName: ${libraryName}, version: ${version}, devBuild: ${devBuild}`, "");
const pegaConfigJsonPath = join(currDir, TASKS_CONFIG_JSON_FILENAME);
const sPegaData = fs.readFileSync(pegaConfigJsonPath, { encoding: 'utf8' });
const configData = JSON.parse(sPegaData);
let serverConfig = configData['server-config'];
serverConfig.devBuild = devBuild;
let compConfig = configData['component'];
compConfig.library = libraryName;
compConfig.version = version;
// update file
// stringify "4", makes the json string look like JSON in the file, formated instead of a single line
fs.writeFileSync(pegaConfigJsonPath, JSON.stringify(configData, null, 4), { encoding: 'utf8',flag:'w' });
}
export const setPackageOrg = async (organization, currDir) => {
addDebugLog("setPackageOrg", "", "");
const packageJsonPath = join(currDir, PACKAGE_JSON_FILENAME);
const sPackage = fs.readFileSync(packageJsonPath, { encoding: 'utf8'});
const packageData = JSON.parse(sPackage);
packageData.organization = organization;
// update file
// stringify "4", makes the json string look like JSON in the file, formated instead of a single line
fs.writeFileSync(packageJsonPath, JSON.stringify(packageData, null, 4), { encoding: 'utf8',flag:'w' });
}
export const updateConfig = async (
{
oldComponentKey,
newComponentKey,
organization,
library,
version,
currentDirectory,
}
) => {
addDebugLog("updateConfig", `oldComponentKey: ${oldComponentKey}, newComponentKey: ${newComponentKey}`, "");
let configData = fs.readFileSync(join(currentDirectory, "/config.json"), { encoding: 'utf8' });
configData = configData && JSON.parse(configData);
configData.name = newComponentKey;
if (configData.componentKey) configData.componentKey = newComponentKey;
configData.organization = organization;
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(currentDirectory, "/config.json"), JSON.stringify(configData, null, 4), { encoding: 'utf8',flag:'w' });
};
export const updateFile = async (
fileName,
oldComponentKeyPC,
newComponentKeyPC,
currentDirectory,
) => {
addDebugLog("updateFile", `fileName: ${fileName}, oldComponentKeyPC: ${oldComponentKeyPC}, newComponentKeyPC: ${newComponentKeyPC}, currentDirectory: ${currentDirectory}`, "");
let fileData = fs.readFileSync(join(currentDirectory, "/", fileName), { encoding: 'utf8' });
if (fileData.indexOf(oldComponentKeyPC) >= 0) {
fileData = fileData.replaceAll(oldComponentKeyPC, newComponentKeyPC);
fs.writeFileSync(join(currentDirectory, "/", fileName), fileData, { encoding: 'utf8',flag:'w' });
}
};
export const reanmeDirectory = async(currentDirectory, targetDirectory) => {
if (!fs.existsSync(targetDirectory)) {
fs.mkdirSync(targetDirectory, { recursive: true });
}
const targetFileList = await getDirectoryFiles(currentDirectory);
for (const targetFile of targetFileList) {
const currentFilePath = join(currentDirectory, targetFile);
const targetFilePath = join(targetDirectory, targetFile);
fs.copyFileSync(currentFilePath, targetFilePath)
}
// fs.rm(currentDirectory, { recursive: true }, err => {
// if (err) {
// throw err;
// }
// });
fs.rmSync(currentDirectory, { recursive: true, force: true, maxRetries: 2 });
}
// rename the component
export const renameComponent = async (
componentKey,
library,
version,
currDir
) => {
addDebugLog("renameComponent", `library: ${library}, componentKey: ${componentKey}`, "");
const componentName = componentKey.split("_")[2];
const configDef = getConfigDefaults();
const orgLibName = `${configDef.organization}_${library}`;
const organization = configDef.organization;
const newComponentKey = `${orgLibName}_${componentName}`;
const oldComponentKey = componentKey;
const newComponentKeyPC = convertIntoPascalCase(newComponentKey);
const oldComponentKeyPC = convertIntoPascalCase(oldComponentKey);
const currentDirectory = await getComponentDirectoryPath(componentKey, currDir);
const targetDirectory = await getComponentDirectoryPath(newComponentKey, currDir);
const targetFileList = await getDirectoryFiles(currentDirectory);
for (const fileIndex in targetFileList) {
const fileName = targetFileList[fileIndex];
if (fileName === "config.json") {
await updateConfig(
{
oldComponentKey,
newComponentKey,
componentName,
organization,
library,
version,
currentDirectory,
}
);
}
else {
await updateFile(
fileName,
oldComponentKeyPC,
newComponentKeyPC,
currentDirectory );
}
} // for
// rename the directory
await reanmeDirectory(currentDirectory, targetDirectory);
return newComponentKey;
};
export const renameComponents = async(compList, libraryName, version, compDir) => {
for (const componentKey of compList) {
await renameComponent(componentKey, libraryName, version, compDir);
}
}
export const updateTempArchive = async(libraryName, version, devBuild) => {
const currentDirectory = process.cwd();
const tempStoreDirectory = join(currentDirectory, TEMP_PATH);
const configDef = getConfigDefaults();
// remove -dev if exists
const justVersion = version.replace("-dev", "");
const tasks = new Listr(
[
{
title: 'Update tasks.config',
task: async () => {
await setTasksConfig(libraryName, justVersion, devBuild, tempStoreDirectory);
}
},
{
title: 'Update package.json',
task: async () => {
await setPackageOrg(configDef.organization, tempStoreDirectory);
}
},
{
title: `Renaming components`,
task: async () => {
const compDir = join(tempStoreDirectory, "src", "components");
const compList = await getComponents(compDir);
await renameComponents(compList, libraryName, version, compDir);
}
}
],
{
exitOnError: true
}
);
await tasks.run().catch(err => {
console.log(chalk.bold.red(err.toString()));
process.exit(1);
});
}
export const getFilePathQuestions = async () => {
addDebugLog("getFilePathQuestions", "", "");
console.log("\nFile path examples:");
if (path.sep === "/") {
console.log("\tMac OS/Unix:\t/Users/name/...\n");
}
else {
console.log("\tWindows:\tC:\\Users\\name\\...\n");
}
const configDef = getConfigDefaults();
const currentDirectory = process.cwd();
const arPathParts = currentDirectory.split(path.sep);
let defaultPath = join(arPathParts[0], arPathParts[1], arPathParts[2], configDef.importRelativePath);
if (path.sep === "/") {
defaultPath = "/".concat(defaultPath);
}
return [
{
name: 'filePath',
message: 'Enter full file path of zip file location:',
default: defaultPath
}
];
};
export const getFileNameQuestions = async (zipFileList) => {
addDebugLog("getFileNameQuestions", "", "");
return [
{
name: 'fileName',
type: 'rawlist',
message: `Select zip file`,
choices: zipFileList
},
];
};
export const updateSavedFilePath = async(filePath) => {
const currentDirectory = process.cwd();
const arPathParts = currentDirectory.split(path.sep);
const configDef = getConfigDefaults();
let defaultPath = join(path.sep, arPathParts[0], arPathParts[1], arPathParts[2]);
if (path.sep != "/") {
// windows
if (defaultPath.indexOf(path.sep) === 0) {
// if start with \\, then remove
defaultPath = defaultPath.substring(1);
}
if (filePath.indexOf(defaultPath) === 0) {
let relativePath = "";
if (filePath !== defaultPath) {
// same starting path, so we can save the relative
relativePath = filePath.replace(defaultPath.concat("/"), "");
}
await setConfigImportRelativePath(relativePath);
await forceDefaultsUpdate();
}
}
else {
// mac
if (filePath.indexOf(defaultPath) === 0) {
let relativePath = "";
if (filePath !== defaultPath) {
// same starting path, so we can save the relative
relativePath = filePath.replace(defaultPath.concat("/"), "");
}
await setConfigImportRelativePath(relativePath);
await forceDefaultsUpdate();
}
}
}