fix generator
This commit is contained in:
parent
1e5b935bb2
commit
3d98fc56d1
|
|
@ -1,63 +1,53 @@
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
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 = [
|
const newEnums = [
|
||||||
{ key: "TEST_EDIT", value: "TEST.edit" },
|
{ key: "TEST_EDIT", value: "TEST.edit" },
|
||||||
{ key: "TEST_ADD", value: "TEST.add" },
|
{ key: "TEST_ADD", value: "TEST.add" },
|
||||||
{ key: "TEST_DELETE", value: "TEST.delete" },
|
{ key: "TEST_DELETE", value: "TEST.delete" },
|
||||||
];
|
];
|
||||||
|
|
||||||
// Function to generate the new enum entries
|
const enumName = "ModalEnum"; // Update this to match your enum name
|
||||||
const generateEnumEntries = (enums) => {
|
|
||||||
return enums
|
const generateEnumEntries = (enums) =>
|
||||||
.map(({ key, value }) => ` ${key} = "${value}",`)
|
enums.map(({ key, value }) => ` ${key} = "${value}",`).join("\n");
|
||||||
.join("\n");
|
|
||||||
};
|
|
||||||
|
|
||||||
// Function to update the ModelEnum file
|
|
||||||
const updateModelEnumFile = () => {
|
const updateModelEnumFile = () => {
|
||||||
if (!fs.existsSync(MODEL_ENUM_FILE)) {
|
if (!fs.existsSync(MODEL_ENUM_FILE)) {
|
||||||
console.error(`Error: File ${MODEL_ENUM_FILE} does not exist.`);
|
console.error(`Error: File ${MODEL_ENUM_FILE} does not exist.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the existing content of the ModelEnum file
|
|
||||||
const fileContent = fs.readFileSync(MODEL_ENUM_FILE, "utf-8");
|
const fileContent = fs.readFileSync(MODEL_ENUM_FILE, "utf-8");
|
||||||
|
|
||||||
// Extract the current enums
|
const existingEnums = new Set(
|
||||||
const existingEnums = new Set();
|
Array.from(fileContent.matchAll(/(\w+)\s*=\s*"([\w.]+)"/g), (match) =>
|
||||||
const enumRegex = /(\w+)\s*=\s*"([\w.]+)"/g;
|
match[1].toLowerCase()
|
||||||
let match;
|
)
|
||||||
while ((match = enumRegex.exec(fileContent)) !== null) {
|
);
|
||||||
existingEnums.add(match[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter out enums that already exist
|
const uniqueEnums = newEnums.filter(
|
||||||
const uniqueEnums = newEnums.filter(({ key }) => !existingEnums.has(key));
|
({ key }) => !existingEnums.has(key.toLowerCase())
|
||||||
|
);
|
||||||
|
|
||||||
if (uniqueEnums.length === 0) {
|
if (uniqueEnums.length === 0) {
|
||||||
console.log("No new enums to add. All enums already exist.");
|
console.log("No new enums to add. All enums already exist.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate the new enum entries
|
|
||||||
const newEnumEntries = generateEnumEntries(uniqueEnums);
|
const newEnumEntries = generateEnumEntries(uniqueEnums);
|
||||||
|
|
||||||
// Insert the new enums before the closing brace of the ModelEnum
|
|
||||||
const updatedContent = fileContent.replace(
|
const updatedContent = fileContent.replace(
|
||||||
/export\s+enum\s+ModelEnum\s*{([\s\S]*?)}/,
|
new RegExp(`export\\s+enum\\s+${enumName}\\s*{([\\s\\S]*?)}`),
|
||||||
(match, existingContent) => {
|
(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");
|
fs.writeFileSync(MODEL_ENUM_FILE, updatedContent, "utf-8");
|
||||||
|
console.log("ModalEnum file updated successfully!");
|
||||||
console.log("ModelEnum file updated successfully!");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
updateModelEnumFile();
|
updateModelEnumFile();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user