Md Afiq Iskandar 84e8d8e42f Enhance Process Builder with New Variables and External Menu Support
- Added new variables for 'namaAsnaf', 'todoStatus', 'asnafScore', 'resultTimestamp', and 'resultSummary' to improve data handling in process definitions.
- Updated process definition JSON to include new output mappings and enhanced script logic for better API response handling.
- Implemented validation for external menu paths in the menu editor, ensuring proper URL formatting and handling for external links.
- Enhanced the user interface in the menu editor to support external URL inputs, improving user experience when adding or editing menu items.
- Updated API endpoints to skip file operations for external menus, streamlining the process of adding and editing external links.
2025-07-16 11:08:38 +08:00

74 lines
1.9 KiB
JavaScript

import fs from "fs";
import path from "path";
export default defineEventHandler(async (event) => {
const body = await readBody(event);
// Normalize paths
const oldPath = body.filePath.endsWith("/")
? body.filePath
: body.filePath + "/";
const newPath = body.formData.path.endsWith("/")
? body.formData.path
: body.formData.path + "/";
// Get file paths
const oldFilePath = path.join(process.cwd(), "pages", oldPath, "index.vue");
const newFilePath = path.join(process.cwd(), "pages", newPath, "index.vue");
// If external, skip file operations
if (body.formData.external === true) {
return {
statusCode: 200,
message: "External menu successfully updated!",
};
}
try {
// Create template content
const templateContent = buildNuxtTemplate({
title: body.formData.title || body.formData.name,
name: body.formData.name,
});
if (oldPath !== newPath) {
// Create the new folder if it doesn't exist
fs.mkdirSync(path.dirname(newFilePath), { recursive: true });
// Write the new file
fs.writeFileSync(newFilePath, templateContent);
// Delete the old file
fs.unlinkSync(oldFilePath);
// Remove empty directories
let dirToCheck = path.dirname(oldFilePath);
while (dirToCheck !== path.join(process.cwd(), "pages")) {
if (fs.readdirSync(dirToCheck).length === 0) {
fs.rmdirSync(dirToCheck);
dirToCheck = path.dirname(dirToCheck);
} else {
break;
}
}
} else {
// Update existing file
fs.writeFileSync(oldFilePath, templateContent);
}
return {
statusCode: 200,
message:
oldPath !== newPath
? "Menu successfully moved and updated"
: "Menu successfully updated",
};
} catch (error) {
console.error(error);
return {
statusCode: 500,
message: error.message,
};
}
});