sendou.ink/scripts/replace-img-names.ts
Kalle dd1adad94b
Some checks are pending
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
BIome v2 upgrade
2025-06-22 16:49:27 +03:00

67 lines
1.6 KiB
TypeScript

import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import invariant from "~/utils/invariant";
import { logger } from "~/utils/logger";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const GEAR_IMAGES_DIR_PATH = path.join(
__dirname,
"..",
"public",
"static-assets",
"img",
"gear",
);
const GEAR_JSON_PATH = path.join(__dirname, "output", "gear.json");
async function main() {
const gear = JSON.parse(fs.readFileSync(GEAR_JSON_PATH, "utf8"));
for (const gearSlot of ["head", "clothes", "shoes"] as const) {
const gearSlotDirPath = path.join(GEAR_IMAGES_DIR_PATH, gearSlot);
const files = await fs.promises.readdir(gearSlotDirPath);
const type =
gearSlot === "head" ? "Hed" : gearSlot === "shoes" ? "Shs" : "Clt";
for (const file of files) {
// did we already replace the name
if (
!file.startsWith("Shs") &&
!file.startsWith("Clt") &&
!file.startsWith("Hed")
) {
continue;
}
if (file.endsWith(".webp")) {
fs.unlinkSync(path.join(gearSlotDirPath, file));
continue;
}
const internalName = file.replace(".png", "").split("_")[1];
invariant(internalName);
const gearId = gear.find(
(g: any) => g.internalName === internalName && g.type === type,
)?.id;
if (typeof gearId !== "number") {
fs.unlinkSync(path.join(gearSlotDirPath, file));
continue;
}
fs.renameSync(
path.join(gearSlotDirPath, file),
path.join(gearSlotDirPath, `${gearId}.png`),
);
}
}
logger.info("done with all");
}
void main();