Update the importable script to use a JSON-based config instead of consts in the Javascript, and make it rename a lot more and remove tags if they aren't used a certain amount
This commit is contained in:
parent
3fdbdf842c
commit
beb7795138
3 changed files with 187 additions and 41 deletions
|
|
@ -1,7 +1,9 @@
|
|||
import { globSync } from "glob";
|
||||
import crypto from "crypto";
|
||||
import { createReadStream } from "fs";
|
||||
import { writeFile } from "fs/promises";
|
||||
import { readFile, writeFile } from "fs/promises";
|
||||
|
||||
const config = JSON.parse(await readFile(`importable/config.json`, `utf8`));
|
||||
|
||||
async function hashFile(path) {
|
||||
return new Promise((resolve) => {
|
||||
|
|
@ -18,67 +20,61 @@ async function hashFile(path) {
|
|||
});
|
||||
};
|
||||
|
||||
const skipFiles = [
|
||||
`vtt.png`,
|
||||
`vtt-512.png`,
|
||||
`anvil.png`,
|
||||
`fvtt.icns`,
|
||||
`fvtt.ico`,
|
||||
`logo-scifi.png`,
|
||||
`logo-scifi-blank.png`,
|
||||
`LICENSE`,
|
||||
];
|
||||
|
||||
const tagRenames = {
|
||||
weapons: `weapon`,
|
||||
wands: `wand`,
|
||||
swords: `sword`,
|
||||
cutlasses: `cutlass`,
|
||||
daggers: `dagger`,
|
||||
commodities: `commodity`,
|
||||
feathers: `feather`,
|
||||
dynamite: `explosive`,
|
||||
bomb: `explosive`,
|
||||
}
|
||||
|
||||
const purgeTags = new Set([
|
||||
// All of the colours
|
||||
`white`, `yellow`, `blue`, `red`, `green`, `black`, `brown`, `teal`, `silver`, `purple`, `gray`, `grey`, `orange`, `lime`, `crimson`, `magenta`, `gold`, `cyan`, `pink`, `tan`, `violet`,
|
||||
|
||||
// Terms that just suck as tags
|
||||
`simple`, `ringed`, `hollow`, `guard`, `pressure`, `gas`, `fuse`,
|
||||
`cloth`, `sharp`, `worn`,
|
||||
]);
|
||||
|
||||
async function main() {
|
||||
const imageList = globSync(`foundry/public/icons/**/*`, { nodir: true });
|
||||
const imageList = globSync(
|
||||
`foundry/public/icons/**/*`,
|
||||
{
|
||||
nodir: true,
|
||||
ignore: config.ignorePatterns,
|
||||
});
|
||||
const images = {};
|
||||
const allTags = {};
|
||||
|
||||
for (const path of imageList) {
|
||||
if (skipFiles.some(file => path.endsWith(file))) continue;
|
||||
if (config.skipFiles.some(file => path.endsWith(file))) continue;
|
||||
|
||||
const hash = await hashFile(path);
|
||||
const filename = path.split(`/`).at(-1);
|
||||
|
||||
const tags = new Set(
|
||||
const tags = Array.from(new Set(
|
||||
path
|
||||
.replace(`foundry/public/icons/`, ``)
|
||||
.replace(/\.[a-z]+$/i, ``)
|
||||
.replaceAll(`-`, `/`)
|
||||
.split(`/`)
|
||||
.map(tag => tagRenames[tag] ?? tag)
|
||||
.filter(tag => !purgeTags.has(tag))
|
||||
);
|
||||
.map(tag => config.replacements[tag] ?? tag)
|
||||
.filter(tag => !config.removals.includes(tag))
|
||||
));
|
||||
|
||||
tags.forEach(t => {
|
||||
allTags[t] ??= 0;
|
||||
allTags[t]++;
|
||||
});
|
||||
|
||||
images[hash] = {
|
||||
name: filename,
|
||||
tags: Array.from(tags),
|
||||
tags,
|
||||
artists: [`FVTT`],
|
||||
path: path.replace(`foundry/public/`, ``),
|
||||
external: true
|
||||
};
|
||||
};
|
||||
|
||||
const sortedTags = Object.entries(allTags)
|
||||
.sort((a,b) => a[0].localeCompare(b[0]))
|
||||
|
||||
// Remove all of the tags that have too low of frequency
|
||||
const removeTags = new Set();
|
||||
for (const [tag, count] of sortedTags) {
|
||||
if (count <= config.min_tag_frequency) {
|
||||
removeTags.add(tag);
|
||||
};
|
||||
};
|
||||
for (const image of Object.values(images)) {
|
||||
image.tags = image.tags.filter(t => !removeTags.has(t));
|
||||
};
|
||||
console.log(`Removed ${removeTags.size} tags from images due to frequency requirements.`);
|
||||
|
||||
await writeFile(
|
||||
`importable/images.json`,
|
||||
JSON.stringify(images),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue