mirror of
https://github.com/Kornstalx/5etools-mirror-2.github.io.git
synced 2025-10-28 20:45:35 -05:00
78 lines
2.0 KiB
JavaScript
78 lines
2.0 KiB
JavaScript
import fs from "fs";
|
|
import * as ut from "../util.js";
|
|
import "../../js/parser.js";
|
|
import "../../js/utils.js";
|
|
import "../../js/utils-proporder.js";
|
|
|
|
const FILE_BLOCKLIST = new Set([
|
|
"loot.json",
|
|
"msbcr.json",
|
|
"monsterfeatures.json",
|
|
"index.json",
|
|
"life.json",
|
|
"makecards.json",
|
|
"renderdemo.json",
|
|
"makebrew-creature.json",
|
|
"sources.json",
|
|
"fluff-index.json",
|
|
"changelog.json",
|
|
|
|
"index-meta.json",
|
|
"index-props.json",
|
|
"index-sources.json",
|
|
"index-timestamps.json",
|
|
|
|
"package.json",
|
|
"package-lock.json",
|
|
]);
|
|
|
|
const _logUnhandledProps = ({unhandledKeys}) => {
|
|
if (!Object.keys(unhandledKeys).length) return;
|
|
|
|
console.warn(`Unhandled keys:`);
|
|
Object.keys(unhandledKeys)
|
|
.forEach(prop => console.warn(`\t${prop}`));
|
|
};
|
|
|
|
export const getPrettified = (json, {isFoundryPrefixProps = false, unhandledKeys = {}} = {}) => {
|
|
if (!PropOrder.hasOrderRoot(json)) return {json, isModified: false};
|
|
|
|
json = PropOrder.getOrderedRoot(
|
|
json,
|
|
{
|
|
fnUnhandledKey: uk => unhandledKeys[uk] = true,
|
|
isFoundryPrefixProps,
|
|
},
|
|
);
|
|
return {json, isModified: true};
|
|
};
|
|
|
|
export const prettifyFile = (file, {unhandledKeys = null} = {}) => {
|
|
const isLogUnhandledProps = unhandledKeys == null;
|
|
unhandledKeys ||= {};
|
|
|
|
console.log(`\tPrettifying ${file}...`);
|
|
const json = ut.readJson(file);
|
|
const {json: jsonPrettified, isModified} = getPrettified(
|
|
json,
|
|
{
|
|
isFoundryPrefixProps: file.includes("foundry.json") || file.split("/").last().startsWith("foundry-"),
|
|
unhandledKeys,
|
|
},
|
|
);
|
|
if (isModified) fs.writeFileSync(file, CleanUtil.getCleanJson(jsonPrettified), "utf-8");
|
|
|
|
if (isLogUnhandledProps) _logUnhandledProps({unhandledKeys});
|
|
};
|
|
|
|
export const prettifyFolder = folder => {
|
|
const unhandledKeys = {};
|
|
console.log(`Prettifying directory ${folder}...`);
|
|
const files = ut.listFiles({dir: folder});
|
|
files
|
|
.filter(file => file.endsWith(".json") && !FILE_BLOCKLIST.has(file.split("/").last()))
|
|
.forEach(file => prettifyFile(file, {unhandledKeys}));
|
|
|
|
_logUnhandledProps({unhandledKeys});
|
|
};
|