forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompactNpcsJson.ts
More file actions
43 lines (34 loc) · 1.65 KB
/
Copy pathcompactNpcsJson.ts
File metadata and controls
43 lines (34 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import fs from "node:fs";
import path from "node:path";
import { compactNpcsData, loadDefaultNpcsData, loadNpcsDataFromJsonFile } from "../npcData";
function resolveCliPath(value: string | undefined, fallback: string): string {
if (!value?.trim()) {
return fallback;
}
return path.resolve(process.cwd(), value);
}
function formatBytes(bytes: number): string {
return `${(bytes / 1024).toFixed(1)} KB`;
}
function main(): void {
const inputArg = process.argv[2]?.trim();
const inputPath = inputArg ? resolveCliPath(inputArg, inputArg) : null;
const outputPath = resolveCliPath(
process.argv[3],
inputPath ? inputPath.replace(/\.json$/i, ".compact.json") : path.resolve(process.cwd(), "reports/npcs.compact.json"),
);
const normalizedNpcs = inputPath ? loadNpcsDataFromJsonFile(inputPath) : loadDefaultNpcsData();
const compactedNpcs = compactNpcsData(normalizedNpcs);
const output = `${JSON.stringify(compactedNpcs, null, 4)}\n`;
const inputSize = inputPath ? fs.statSync(inputPath).size : Buffer.byteLength(JSON.stringify(normalizedNpcs), "utf8");
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, output, "utf8");
const outputSize = Buffer.byteLength(output, "utf8");
const savedBytes = inputSize - outputSize;
const savedPercent = inputSize > 0 ? (savedBytes / inputSize) * 100 : 0;
console.log(`Compacted NPC dataset to ${outputPath}`);
console.log(`Input: ${formatBytes(inputSize)}`);
console.log(`Output: ${formatBytes(outputSize)}`);
console.log(`Saved: ${formatBytes(savedBytes)} (${savedPercent.toFixed(2)}%)`);
}
main();