59 lines
1.9 KiB
JavaScript
59 lines
1.9 KiB
JavaScript
let crypto;
|
|
|
|
import { readdirSync, readFileSync, writeFileSync } from "fs";
|
|
import {dirname, join} from "path";
|
|
|
|
try {
|
|
|
|
|
|
crypto = await import('node:crypto');
|
|
|
|
/**
|
|
* Generate a random alphanumeric string ID of a given requested length using `crypto.getRandomValues()`.
|
|
* @param {number} length The length of the random string to generate, which must be at most 16384.
|
|
* @returns {string} A string containing random letters (A-Z, a-z) and numbers (0-9).
|
|
*/
|
|
function randomID(length = 16) {
|
|
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
const cutoff = 0x100000000 - (0x100000000 % chars.length);
|
|
const random = new Uint32Array(length);
|
|
do {
|
|
crypto.getRandomValues(random);
|
|
} while (random.some(x => x >= cutoff));
|
|
let id = "";
|
|
for (let i = 0; i < length; i++) id += chars[random[i] % chars.length];
|
|
return id;
|
|
}
|
|
|
|
|
|
const convert = function (from, to, ofType) {
|
|
|
|
const SOURCE = from;
|
|
const DEST = to;
|
|
const TYPE = ofType;
|
|
|
|
readdirSync(SOURCE).forEach(file => {
|
|
|
|
let originalSource = JSON.parse(readFileSync(join(SOURCE, file), {encoding: "utf8"}));
|
|
let id = randomID();
|
|
let targetSource = {
|
|
_id: id,
|
|
_key: "!items!" + id,
|
|
type: TYPE,
|
|
name: originalSource.name.trim(),
|
|
system: {...originalSource}
|
|
}
|
|
let target = JSON.stringify(targetSource, null, 2);
|
|
let newFileName = "./" + join(DEST, targetSource.name.toLowerCase().replace(/[ /]/g, "-").replace(/\--{2,}/g, "-").replace(/[.,!]/g, "").trim() + ".json");
|
|
console.log(newFileName);
|
|
writeFileSync(newFileName, target, {encoding: "utf8"});
|
|
});
|
|
|
|
}
|
|
|
|
convert("./src/packs/_source/zauber", "./src/packs/__source/zauber", "Spell");
|
|
convert("./src/packs/_source/vorteile", "./src/packs/__source/vorteile", "Advantage");
|
|
|
|
} catch (err) {
|
|
console.error(err);
|
|
} |