@pega/custom-dx-components
Version:
Utility for building custom UI components
192 lines (156 loc) • 5.84 kB
JavaScript
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import { join } from 'path';
import {
sanitize,
validateSemver,
getLibraryArchiveDirectories,
addDebugLog,
getConfigDefaults,
getComponents,
updateConfigVersion,
getLibraryBasedCL,
copyFileToShared
} from '../../util.js';
import { SHARED_FILE_NAMES } from '../../constants.js';
export const updateAllComponentsConfig = async( newVersion ) => {
addDebugLog("updateAllComponentsConfig", `newVersion: ${newVersion}`, "");
const componentList = await getComponents();
const orgLib = getConfigDefaults();
// const currentOrgLib = `${orgLib.organization}_${orgLib.library}`;
const currentOrgLib = orgLib.currentOrgLib;
for (const index in componentList) {
const componentKey = componentList[index];
await updateConfigVersion(componentKey, newVersion, currentOrgLib);
}
};
export const updateAllMigrateToLibChanges = async() => {
// get src/components directories
const componentList = await getComponents();
const currentDirectory = process.cwd();
// for each directory
for (const index in componentList) {
const componentKey = componentList[index];
const compDir = join(currentDirectory,'src', 'components', componentKey);
const componentFiles = fs.readdirSync(compDir);
componentFiles.forEach( file => {
const filePath = join(compDir, file);
const fileData = fs.readFileSync(filePath, { encoding: 'utf8' });
let newOutput = fileData;
if (SHARED_FILE_NAMES.includes(file)) {
// fix create nonce for ESLint 9
// if (file.includes('create-nonce')) {
// newOutput = newOutput.replace("__webpack_nonce__ = window.__webpack_nonce__;", "// eslint-disable-next-line no-undef\n__webpack_nonce__ = window.__webpack_nonce__;");
// }
// write to shared
copyFileToShared(file, newOutput);
// delete file from main components
fs.rmSync(filePath);
}
else if (file.includes(".tsx") || file.includes(".ts") ) {
// going to search for shared file names and change the import path
for (const index in SHARED_FILE_NAMES) {
const sFileName = SHARED_FILE_NAMES[index].split(".")[0];
// test for ../ first
let orgPath = "../".concat(sFileName);
let newPath = "../shared/".concat(sFileName);
newOutput = newOutput.replaceAll(orgPath, newPath);
// then test for ./
orgPath = "./".concat(sFileName);
newPath = "../shared/".concat(sFileName);
newOutput = newOutput.replaceAll(orgPath, newPath);
// updates for ESLint 9
newOutput = newOutput.replaceAll("/* eslint-disable react-hooks/exhaustive-deps */", "");
newOutput = newOutput.replaceAll("// eslint-disable-next-line react-hooks/exhaustive-deps", "");
newOutput = newOutput.replaceAll("// eslint-disable-next-line @typescript-eslint/no-unused-vars", "");
newOutput = newOutput.replaceAll("/* eslint-disable react-hooks/rules-of-hooks */", "");
newOutput = newOutput.replaceAll("/* eslint-disable-next-line no-empty */", "");
newOutput = newOutput.replaceAll("// eslint-disable-next-line no-empty", "");
}
fs.writeFileSync(filePath, newOutput);
}
});
}
console.log();
console.log(`${chalk.green(`>> Code base was upgraded. You may still have some lint errors that need to be fixed manually.`)}`);
console.log(`${chalk.green(`>> Run ${chalk.bold(`npm run validateAll`)} to check for issues.`)}`);
}
export const getCurrentOrOriginalQuestion = async () => {
addDebugLog("getCurrentOrOriginal", "", "");
return [
{
name: 'currentOrOriginal',
type: 'rawlist',
message: 'Current config changes or original defaults ?',
default: 'Original',
choices: ['Current', 'Original']
}
];
}
export const getOkToProceedQuestion = async () => {
addDebugLog("getCreateLibComponentQuestions", "", "");
return [
{
name: 'okToContinue',
type: 'confirm',
message: 'Ok to continue ?',
default: true
}
];
}
export const getCreateLibComponentQuestions = async (libraryName = "") => {
addDebugLog("getCreateLibComponentQuestions", "", "");
const archDirectories = await getLibraryArchiveDirectories();
const orgLib = getConfigDefaults();
const isLibraryBasedCL = getLibraryBasedCL();
return [
{
name: 'libraryName',
message: 'Enter library name',
default: libraryName,
validate: value => {
/* value should not be empty
It should not have spaces
It should not start with a number
Only case-insensitive alphanumeric values are allowed
*/
if (value && !/^\d/.test(value) && value === sanitize(value)) {
if (archDirectories && archDirectories.length > 0) {
const newOrgLib = `${orgLib.organization}_${value}`;
if (archDirectories.includes(newOrgLib)) {
return `Library ${value} already exists.`;
}
else {
return true;
}
}
else {
return true;
}
}
else {
return 'Only alphanumeric values are allowed, starting with alphabets, no spaces.';
}
}
},
{
name: 'version',
message: 'Enter version ',
default: '0.0.1',
validate: value => {
if (validateSemver(value)) {
return true;
}
return 'Please provide semver compatible version e.g 0.0.1';
}
},
{
name: 'permBuild',
type: 'confirm',
message: `Version to be ${chalk.bold.yellow('PERMANENT')} upon publish `,
default: false,
when: () => !isLibraryBasedCL
}
];
};