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 DIRECTORY = "./src/packs/_source/zauber"; readdirSync(DIRECTORY).forEach( file => { let originalSource = JSON.parse(readFileSync(join(DIRECTORY, file), {encoding: "utf8"})); let id = randomID(); let targetSource = { _id: id, _key: "!items!" + id, type: "Spell", name: originalSource.name, system: {...originalSource} } delete targetSource.system.name let target = JSON.stringify(targetSource, null, 2); let newFileName = "./" + join(DIRECTORY, targetSource.name.toLowerCase().replace(/[ /]/g, "-").replace(/\--{2,}/g, "-").replace(/[.,!]/g, "") + ".json"); console.log(newFileName); writeFileSync(newFileName, target, { encoding: "utf8" }); }); } catch (err) { console.error(err); }