Files
vermine2047/tools/pushLDBtoYAML.mjs
T
2024-11-18 09:27:33 +01:00

52 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { extractPack } from "@foundryvtt/foundryvtt-cli";
import { promises as fs } from "fs";
import path from "path";
const MODULE_ID = process.cwd();
const yaml = true;
const log = true;
const packs = await fs.readdir("./packs");
for (const pack of packs) {
if (pack === ".gitattributes") continue;
const packPath = path.join("./packs", pack);
const stats = await fs.lstat(packPath); // Utilisation de lstat pour vérifier si c'est un fichier ou un répertoire
if (!stats.isDirectory()) {
console.log(`${pack} est un fichier, il est ignoré.`);
continue; // Si ce n'est pas un répertoire, passer à l'élément suivant
}
console.log("Unpacking " + pack);
const directory = `./src/packs/${pack}`;
try {
console.log(directory);
for (const file of await fs.readdir(directory)) {
await fs.unlink(path.join(directory, file));
}
} catch (error) {
if (error.code === "ENOENT") console.log("No files inside of " + pack);
else console.log(error);
}
await extractPack(
`${MODULE_ID}/packs/${pack}`,
`${MODULE_ID}/src/packs/${pack}`,
{
yaml,
transformName,
log
}
);
}
/**
* Prefaces the document with its type
* @param {object} doc - The document data
*/
function transformName(doc) {
const safeFileName = doc.name.replace(/[^a-zA-Z0-9А-я]/g, "_");
const type = doc._key.split("!")[1];
const prefix = ["actors", "items"].includes(type) ? doc.type : type;
return `${doc.name ? `${prefix}_${safeFileName}_${doc._id}` : doc._id}.${yaml ? "yml" : "json"
}`;
}