json-dropdown-tools
Version:
A tool to generate dropdown menus from JSON as HTML or JS modules
54 lines (45 loc) • 1.67 kB
JavaScript
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { menuToHtml, generateMenuJs } from "./menu-tools.js";
import { writeCssToFile } from "./menu-css-generator.js";
// -----------------------
// Resolve __dirname for ES modules
// -----------------------
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// -----------------------
// Paths and output folder
// -----------------------
const menuJsonPath = path.join(__dirname, "menu.json");
const distPath = path.join(__dirname, "dist");
// Clean and recreate dist/
if (fs.existsSync(distPath)) {
fs.rmSync(distPath, { recursive: true, force: true });
}
fs.mkdirSync(distPath);
// Output file paths
const htmlOutputPath = path.join(distPath, "menu.html");
const jsOutputPath = path.join(distPath, "menu.js");
const cssOutputPath = path.join(distPath, "menu.css");
// -----------------------
// Load JSON
// -----------------------
const menuJson = JSON.parse(fs.readFileSync(menuJsonPath, "utf-8"));
// -----------------------
// Generate HTML
// -----------------------
const htmlString = menuToHtml(menuJson);
fs.writeFileSync(htmlOutputPath, htmlString, "utf-8");
console.log(`✅ menu.html generated at ${htmlOutputPath}`);
// -----------------------
// Generate JS Module
// -----------------------
const jsString = generateMenuJs(menuJson);
fs.writeFileSync(jsOutputPath, jsString, "utf-8");
console.log(`✅ menu.js generated at ${jsOutputPath}`);
// -----------------------
// Generate CSS
// -----------------------
writeCssToFile(menuJson, cssOutputPath);
console.log(`✅ menu.css generated at ${cssOutputPath}`);