56 lines
1.6 KiB
JavaScript
56 lines
1.6 KiB
JavaScript
import fs from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
const ROOT = process.cwd();
|
|
const GALLERY_ROOT = path.join(ROOT, "public", "images", "gallery");
|
|
const OUTPUT_FILE = path.join(ROOT, "public", "gallery.json");
|
|
|
|
async function exists(p) {
|
|
try {
|
|
await fs.access(p);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
try {
|
|
if (!(await exists(GALLERY_ROOT))) {
|
|
console.log("Gallery root not found, creating empty gallery.json");
|
|
await fs.writeFile(OUTPUT_FILE, JSON.stringify({ vip: [], personal: [], minibus: [] }, null, 2));
|
|
return;
|
|
}
|
|
|
|
const categories = ["vip", "personal", "minibus"];
|
|
const result = {};
|
|
|
|
for (const category of categories) {
|
|
const categoryPath = path.join(GALLERY_ROOT, category);
|
|
if (await exists(categoryPath)) {
|
|
const files = await fs.readdir(categoryPath);
|
|
const images = files
|
|
.filter((file) => {
|
|
const ext = path.extname(file).toLowerCase();
|
|
return [".jpg", ".jpeg", ".png", ".webp", ".gif", ".svg"].includes(ext);
|
|
})
|
|
.sort()
|
|
.map((img) => `/images/gallery/${category}/${img}`);
|
|
|
|
result[category] = images;
|
|
} else {
|
|
result[category] = [];
|
|
}
|
|
}
|
|
|
|
await fs.writeFile(OUTPUT_FILE, JSON.stringify(result, null, 2));
|
|
console.log(`Gallery metadata generated at ${OUTPUT_FILE}`);
|
|
console.log(`Summary: ${Object.keys(result).map(c => `${c}: ${result[c].length}`).join(", ")}`);
|
|
} catch (err) {
|
|
console.error("Error generating gallery metadata:", err);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main();
|