From 1ef77b3447021bd240354ed5121192bd138c64b2 Mon Sep 17 00:00:00 2001 From: Thom Werring Date: Mon, 27 Nov 2023 08:37:55 +0100 Subject: [PATCH] Added merge script --- .vscode/launch.json | 17 ------ .vscode/settings.json | 2 +- homebrew/SCGtD/classes.json | 98 +++++++++++++++++++++++++++++++++- homebrew/SCGtD/spells.json | 13 ++--- homebrew/SCGtD/subclasses.json | 22 ++++++-- package.json | 1 + scripts/combine.js | 63 ++++++++++++++++++++++ scripts/lib/import-source.js | 11 ++++ scripts/lib/select-project.js | 7 +++ 9 files changed, 200 insertions(+), 34 deletions(-) delete mode 100644 .vscode/launch.json create mode 100644 scripts/lib/import-source.js create mode 100644 scripts/lib/select-project.js diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 822e5d1..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "node", - "request": "launch", - "name": "Launch Program", - "skipFiles": [ - "/**" - ], - "program": "${file}" - } - ] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 700d906..93fd80e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,7 +2,7 @@ "json.schemas": [ { "fileMatch": [ - "*.json" + "homebrew/*.json" ], "url": "https://raw.githubusercontent.com/TheGiddyLimit/5etools-utils/master/schema/brew-fast/homebrew.json" } diff --git a/homebrew/SCGtD/classes.json b/homebrew/SCGtD/classes.json index 0e0dcd2..43788b9 100644 --- a/homebrew/SCGtD/classes.json +++ b/homebrew/SCGtD/classes.json @@ -1,3 +1,99 @@ { - + "_meta": { + "sources": [ + { + "json": "SCGtD", + "abbreviation": "SCGtD", + "full": "Sebastian Crowe's Guide to Drakkenheim", + "version": "0" + } + ], + "dateAdded": 0, + "dateLastModified": 0 + }, + "class": [ + { + "name": "Apothecary", + "source": "SCGtD", + "classFeatures": [ + + ], + "hd": { + "number": 0, + "faces": 0 + }, + "proficiency": [ + "str", + "str" + ], + "startingProficiencies": { + "armor": [ + "light" + ], + "weapons": [ + "simple and martial are rendered as x weapons, but you can write any string that you want." + ], + "tools": [ + "Name of any tool or artisan tools" + ], + "skills": [ + { + "skillName": true + } + ] + }, + "startingEquipment": { + "default": [ + "Each string will appear as a list entry" + ], + "additionalFromBackground": true, + "goldAlternative": "{@dice xdy}" + }, + "multiclassing": { + "requirements": { + "str": 0 + }, + "proficienciesGained": { + "armor": [ + "light" + ], + "weapons": [ + "simple" + ], + "tools": [ + "" + ], + "skills": [ + { + "acrobatics": true + } + ] + } + }, + "casterProgression": "full", + "spellcastingAbility": "str", + "classSpells": [ + "" + ], + "fluff": [ + + ] + } + ], + "subclass": [{ + "name": "Apothecary", + "source": "SCGtD", + "page": 0, + "className": "Apothecary", + "classSource": "SCGtD", + "shortName": "Apo", + "subclassFeatures": [ + + ], + "casterProgression": "full", + "spellcastingAbility": "str", + "subclassSpells": [ + "" + ] + }] } \ No newline at end of file diff --git a/homebrew/SCGtD/spells.json b/homebrew/SCGtD/spells.json index 21036dd..e57d629 100644 --- a/homebrew/SCGtD/spells.json +++ b/homebrew/SCGtD/spells.json @@ -5,18 +5,11 @@ "json": "SCGtD", "abbreviation": "SCGtD", "full": "Sebastian Crowe's Guide to Drakkenheim", - "authors": [ - "Dungeon Dudes" - ], - "convertedBy": [ - "Werring" - ], - "version": "1.0", - "targetSchema": "1.0.0" + "version": "0" } ], - "dateAdded": 1700993345, - "dateLastModified": 1700993345 + "dateAdded": 0, + "dateLastModified": 0 }, "spell": [ { diff --git a/homebrew/SCGtD/subclasses.json b/homebrew/SCGtD/subclasses.json index 71b893e..62d5401 100644 --- a/homebrew/SCGtD/subclasses.json +++ b/homebrew/SCGtD/subclasses.json @@ -1,12 +1,24 @@ { + "_meta": { + "sources": [ + { + "json": "SCGtD", + "abbreviation": "SCGtD", + "full": "Sebastian Crowe's Guide to Drakkenheim", + "version": "0" + } + ], + "dateAdded": 0, + "dateLastModified": 0 + }, "subclass": [ { - "name": "", - "source": "", + "name": "Barb", + "source": "SCGtD", "page": 0, - "className": "", - "classSource": "", - "shortName": "", + "className": "Barbarian", + "classSource": "PHB", + "shortName": "Barb", "subclassFeatures": [ ], diff --git a/package.json b/package.json index dbcff45..7375dcf 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "type": "module", "scripts": { "validate": "test-json-brew", + "merge": "node scripts/combine.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", diff --git a/scripts/combine.js b/scripts/combine.js index e69de29..7b04eb8 100644 --- a/scripts/combine.js +++ b/scripts/combine.js @@ -0,0 +1,63 @@ +import path from "path"; +import fs from "fs"; +import { selectProject } from "./lib/select-project.js"; +import { importSourceFactory } from "./lib/import-source.js"; + + +const PROJECT_DIR = process.env.npm_config_local_prefix; +const HOMEBREW_DIR = path.join(PROJECT_DIR, "homebrew"); + +const projects = fs.readdirSync(HOMEBREW_DIR, { + withFileTypes: true, +}) + .filter((dir) => dir.isDirectory()) + .map((dir) => dir.name); + +if (projects.length === 0) { + console.error("No homebrew projects"); + process.exit(1); +} + +const project = selectProject(projects); +const SOURCE_DIR = path.join(HOMEBREW_DIR, project); +const importSource = importSourceFactory(SOURCE_DIR); + +const meta = importSource("meta.json"); +meta._meta.dateLastModified = parseInt(Date.now()/1000); + +const sources = fs.readdirSync(SOURCE_DIR, { + withFileTypes: true, +}) + .filter((source) => source.isFile() && source.name.endsWith(".json") && source.name !== "meta.json") + .map((source) => source.name); + + +const homebrew = sources.reduce((result, source) => { + const sourceData = importSource(source); + delete sourceData._meta; + + Object.entries(sourceData).forEach(([sourceKey, sourceValue]) => { + if (!(sourceKey in result)) { + + result[sourceKey] = sourceValue; + return; + } + if (Array.isArray(result[sourceKey])) { + result[sourceKey] = result[sourceKey].concat(sourceValue); + return; + } + throw new Error(`Could not merge non-array key ${sourceKey}`); + }); + + return result; +}, meta); + + +console.log(homebrew.subclass); + +const outFile = path.join(HOMEBREW_DIR, `${project}.json`); +if (fs.existsSync(outFile)) { + fs.unlinkSync(outFile); +} + +fs.writeFileSync(outFile, JSON.stringify(homebrew, null, 4), { encoding: "utf-8"}); \ No newline at end of file diff --git a/scripts/lib/import-source.js b/scripts/lib/import-source.js new file mode 100644 index 0000000..4126344 --- /dev/null +++ b/scripts/lib/import-source.js @@ -0,0 +1,11 @@ +import fs from "fs"; +import path from "path"; + +/** + * + * @param {string} sourceDir + * @returns ({string} filePath ) => Record + */ +export function importSourceFactory(sourceDir) { + return (filePath) => JSON.parse(fs.readFileSync(path.join(sourceDir, filePath)).toString("utf-8")); +} \ No newline at end of file diff --git a/scripts/lib/select-project.js b/scripts/lib/select-project.js new file mode 100644 index 0000000..80c512c --- /dev/null +++ b/scripts/lib/select-project.js @@ -0,0 +1,7 @@ +/** + * @param {string[]} projects + * @returns string + */ +export function selectProject(projects) { + return projects[0]; +} \ No newline at end of file