Files
5etools-mirror-2.github.io/node/util-tag-jsons.js
TheGiddyLimit 2eeeb0771b v1.209.0
2024-07-10 20:47:40 +01:00

90 lines
2.2 KiB
JavaScript

import fs from "fs";
import "../js/utils.js";
import "../js/render.js";
import "../js/render-dice.js";
import * as ut from "./util.js";
import "../js/converterutils.js";
import "../js/converterutils-entries.js";
function run (args) {
TagJsons._BLOCKLIST_FILE_PREFIXES = [
...ut.FILE_PREFIX_BLOCKLIST,
"foundry-",
"foundry.json",
// specific files
"demo.json",
];
let files;
if (args.file) {
files = [args.file];
} else {
files = ut.listFiles({dir: `./data`, blocklistFilePrefixes: TagJsons._BLOCKLIST_FILE_PREFIXES});
if (args.filePrefix) {
files = files.filter(f => f.startsWith(args.filePrefix));
if (!files.length) throw new Error(`No file with prefix "${args.filePrefix}" found!`);
}
}
const creatureList = getTaggableCreatureList(args.bestiaryFile);
files.forEach(file => {
console.log(`Tagging file "${file}"`);
const json = ut.readJson(file);
if (json instanceof Array) return;
TagJsons.mutTagObject(json, {creaturesToTag: creatureList});
const outPath = args.inplace ? file : file.replace("./data/", "./trash/");
if (!args.inplace) {
const dirPart = outPath.split("/").slice(0, -1).join("/");
fs.mkdirSync(dirPart, {recursive: true});
}
fs.writeFileSync(outPath, CleanUtil.getCleanJson(json));
});
}
/**
* Return creatures from the provided bestiary which are one of:
* - A named creature
* - A copy of a creature
* - An NPC
* as these creatures are likely to be missed by the automated tagging during conversion.
*/
function getTaggableCreatureList (filename) {
if (!filename) return [];
const bestiaryJson = ut.readJson(filename);
return (bestiaryJson.monster || [])
.filter(it => it.isNamedCreature || it.isNpc || it._copy)
.map(it => ({name: it.name, source: it.source}));
}
function setUp () {
ut.patchLoadJson();
}
function teardown () {
ut.unpatchLoadJson();
}
function loadSpells () {
const spellIndex = ut.readJson(`./data/spells/index.json`);
return Object.entries(spellIndex).map(([source, filename]) => {
if (SourceUtil.isNonstandardSource(source)) return [];
return ut.readJson(`./data/spells/${filename}`).spell;
}).flat();
}
export {
setUp,
loadSpells,
run,
teardown,
getTaggableCreatureList,
};