Files
rustycms/scripts/migrate-img-to-src-description.mjs

71 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
/**
* Migriert content/<locale>/img/*.json5 von altem Schema (title, file.url, …) auf neues Schema (description, src).
* - src = file.url (pflicht im neuen Schema; fehlt file.url → Warnung, src = "")
* - description = description || title || ""
* - _slug bleibt
*/
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const CONTENT_ROOT = path.join(__dirname, "..", "content");
function parseJson5(str) {
try {
return JSON.parse(str);
} catch {
return null;
}
}
function* findImgDirs() {
const locales = fs.readdirSync(CONTENT_ROOT, { withFileTypes: true });
for (const loc of locales) {
if (!loc.isDirectory()) continue;
const imgDir = path.join(CONTENT_ROOT, loc.name, "img");
if (fs.existsSync(imgDir)) yield imgDir;
}
}
let total = 0;
let updated = 0;
let skipped = 0;
let noUrl = 0;
for (const imgDir of findImgDirs()) {
const files = fs.readdirSync(imgDir).filter((f) => f.endsWith(".json5"));
for (const file of files) {
total++;
const filePath = path.join(imgDir, file);
const raw = fs.readFileSync(filePath, "utf8");
const data = parseJson5(raw);
if (!data) {
console.warn("Skip (parse error):", filePath);
skipped++;
continue;
}
const url = data.file?.url;
if (url == null || url === "") {
console.warn("Keine file.url:", filePath);
noUrl++;
}
const description = data.description != null && data.description !== ""
? data.description
: (data.title != null ? String(data.title) : "");
const out = {
_slug: data._slug ?? file.replace(/\.json5$/, ""),
...(description !== "" && { description }),
src: url ?? "",
};
fs.writeFileSync(filePath, JSON.stringify(out, null, 2) + "\n", "utf8");
updated++;
}
}
console.log("Img-Migration:", updated, "von", total, "Dateien aktualisiert.");
if (skipped) console.log("Übersprungen (Parse-Fehler):", skipped);
if (noUrl) console.log("Ohne file.url (src leer):", noUrl);