fix generator

This commit is contained in:
moaz_dw 2024-11-22 02:06:36 +03:00
parent 1e5b935bb2
commit 3d98fc56d1

View File

@ -1,63 +1,53 @@
const fs = require("fs");
const path = require("path");
const MODEL_ENUM_FILE = "./src/enums/Model.ts"; // Path to the ModelEnum file
const MODEL_ENUM_FILE = "./src/enums/Model.ts"; // Path to the ModalEnum file
// The new enums to be added
const newEnums = [
{ key: "TEST_EDIT", value: "TEST.edit" },
{ key: "TEST_ADD", value: "TEST.add" },
{ key: "TEST_DELETE", value: "TEST.delete" },
];
// Function to generate the new enum entries
const generateEnumEntries = (enums) => {
return enums
.map(({ key, value }) => ` ${key} = "${value}",`)
.join("\n");
};
const enumName = "ModalEnum"; // Update this to match your enum name
const generateEnumEntries = (enums) =>
enums.map(({ key, value }) => ` ${key} = "${value}",`).join("\n");
// Function to update the ModelEnum file
const updateModelEnumFile = () => {
if (!fs.existsSync(MODEL_ENUM_FILE)) {
console.error(`Error: File ${MODEL_ENUM_FILE} does not exist.`);
return;
}
// Read the existing content of the ModelEnum file
const fileContent = fs.readFileSync(MODEL_ENUM_FILE, "utf-8");
// Extract the current enums
const existingEnums = new Set();
const enumRegex = /(\w+)\s*=\s*"([\w.]+)"/g;
let match;
while ((match = enumRegex.exec(fileContent)) !== null) {
existingEnums.add(match[1]);
}
const existingEnums = new Set(
Array.from(fileContent.matchAll(/(\w+)\s*=\s*"([\w.]+)"/g), (match) =>
match[1].toLowerCase()
)
);
// Filter out enums that already exist
const uniqueEnums = newEnums.filter(({ key }) => !existingEnums.has(key));
const uniqueEnums = newEnums.filter(
({ key }) => !existingEnums.has(key.toLowerCase())
);
if (uniqueEnums.length === 0) {
console.log("No new enums to add. All enums already exist.");
return;
}
// Generate the new enum entries
const newEnumEntries = generateEnumEntries(uniqueEnums);
// Insert the new enums before the closing brace of the ModelEnum
const updatedContent = fileContent.replace(
/export\s+enum\s+ModelEnum\s*{([\s\S]*?)}/,
new RegExp(`export\\s+enum\\s+${enumName}\\s*{([\\s\\S]*?)}`),
(match, existingContent) => {
return `export enum ModelEnum {\n${existingContent.trim()}\n\n${newEnumEntries}\n}`;
return `export enum ${enumName} {\n${existingContent.trim()}\n\n${newEnumEntries}\n}`;
}
);
// Write the updated content back to the file
fs.writeFileSync(MODEL_ENUM_FILE, updatedContent, "utf-8");
console.log("ModelEnum file updated successfully!");
console.log("ModalEnum file updated successfully!");
};
updateModelEnumFile();