From 68fa136fe3c51ec1240db134d84e89b0cf580820 Mon Sep 17 00:00:00 2001 From: esheyw Date: Thu, 1 Feb 2024 20:19:19 -0800 Subject: [PATCH] test of pack-on-release --- .github/workflows/main.yaml | 7 +++- .gitignore | 3 +- TODO | 3 +- build/pack.mjs | 14 +++++++ build/unpack.mjs | 24 ++++++++++++ package.json | 4 +- scripts/classes/MHLDialog.mjs | 2 +- scripts/constants.mjs | 12 +++++- scripts/helpers/errorHelpers.mjs | 30 ++------------- scripts/init.mjs | 17 ++++++--- scripts/macros/lashingCurrents.mjs | 2 +- scripts/macros/updateInitiativeStatistics.mjs | 6 +-- scripts/settings.mjs | 38 ++++++++++++------- .../Lashing_Currents_hRCi5uHwsTJHQ4Dt.json | 26 +++++++++++++ 14 files changed, 131 insertions(+), 57 deletions(-) create mode 100644 build/pack.mjs create mode 100644 build/unpack.mjs create mode 100644 unpacks/helper-library-macros/Lashing_Currents_hRCi5uHwsTJHQ4Dt.json diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 6395205..0e1286f 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -10,9 +10,12 @@ jobs: steps: # Checkout - uses: actions/checkout@v2 - # build sass + # install deps - run: npm ci + # build sass - run: npm run sass + # pack packs + - run: npm run pack # Substitute the Manifest and Download URLs in the module.json - name: Substitute Manifest and Download Links For Versioned Ones @@ -27,7 +30,7 @@ jobs: download: https://github.com/${{github.repository}}/releases/download/${{github.event.release.tag_name}}/module.zip # Create a zip file with all files required by the module to add to the release - - run: zip -r ./module.zip module.json LICENSE styles/*.css styles/*.map scripts/ templates/ lang/ + - run: zip -r ./module.zip module.json LICENSE styles/*.css styles/*.map scripts/ templates/ lang/ packs/ # Create a release for this specific version - name: Update Release with Files diff --git a/.gitignore b/.gitignore index 4e88795..22dd871 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ styles/*.map .vscode/ foundry.js commons.js -packs/ +packs/** +node_modules \ No newline at end of file diff --git a/TODO b/TODO index ac4e8ba..a79b994 100644 --- a/TODO +++ b/TODO @@ -2,4 +2,5 @@ - brushup the group initiative skills dialog macro - double check CSS on various themes/no-dorako - npm package setup, run link dev - - or just a standalone script \ No newline at end of file + - or just a standalone script +- move validator wavy underline from inline style to sass \ No newline at end of file diff --git a/build/pack.mjs b/build/pack.mjs new file mode 100644 index 0000000..654d531 --- /dev/null +++ b/build/pack.mjs @@ -0,0 +1,14 @@ +import { compilePack } from "@foundryvtt/foundryvtt-cli"; +import { promises as fs } from "fs"; + +const MODULE_ID = process.cwd(); + +const packs = await fs.readdir("./unpacks"); +for (const pack of packs) { + if (pack === ".gitattributes") continue; + console.log("Packing " + pack); + await compilePack( + `${MODULE_ID}/unpacks/${pack}`, + `${MODULE_ID}/packs/${pack}` + ); +} \ No newline at end of file diff --git a/build/unpack.mjs b/build/unpack.mjs new file mode 100644 index 0000000..87224f6 --- /dev/null +++ b/build/unpack.mjs @@ -0,0 +1,24 @@ +import { extractPack } from "@foundryvtt/foundryvtt-cli"; +import { promises as fs } from "fs"; +import path from "path"; + +const MODULE_ID = process.cwd(); + +const packs = await fs.readdir("./packs"); +for (const pack of packs) { + if (pack === ".gitattributes") continue; + console.log("Unpacking " + pack); + const directory = `./unpacks/${pack}`; + try { + for (const file of await fs.readdir(directory)) { + await fs.unlink(path.join(directory, file)); + } + } catch (error) { + if (error.code === "ENOENT") console.log("No files inside of " + pack); + else console.log(error); + } + await extractPack( + `${MODULE_ID}/packs/${pack}`, + `${MODULE_ID}/unpacks/${pack}` + ); +} \ No newline at end of file diff --git a/package.json b/package.json index 885d048..d7116f2 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "PF2e Macro & Helper Library", "main": "scripts/init.mjs", "scripts": { - "sass": "sass styles/main.scss styles/main.css" + "sass": "sass styles/main.scss styles/main.css", + "unpack": "node build/unpack.mjs", + "pack": "node build/pack.mjs" }, "repository": { "type": "git", diff --git a/scripts/classes/MHLDialog.mjs b/scripts/classes/MHLDialog.mjs index 62e87af..546f234 100644 --- a/scripts/classes/MHLDialog.mjs +++ b/scripts/classes/MHLDialog.mjs @@ -4,7 +4,7 @@ import { localize } from "../helpers/stringHelpers.mjs"; const PREFIX = "MHL.Dialog"; export class MHLDialog extends Dialog { constructor(data, options = {}) { - //validate the validator. TODO: add facility for list of non-empty inputs instead of function + //validate the validator. if ("validator" in data) { let validator = data.validator; switch (typeof validator) { diff --git a/scripts/constants.mjs b/scripts/constants.mjs index 5e26365..e9c1b01 100644 --- a/scripts/constants.mjs +++ b/scripts/constants.mjs @@ -1,4 +1,4 @@ -export const MODULE = "pf2e-macro-helper-library"; +export const MODULE_ID = "pf2e-macro-helper-library"; export const PHYSICAL_ITEM_TYPES = [ "armor", "backpack", @@ -16,3 +16,13 @@ export const COLOURS = { error: "var(--color-level-error, red)", }; export const LABELABLE_TAGS = ["button", "input", "meter", "output", "progress", "select", "textarea"]; +export const SETTINGS = { + "notify-on-error": { + config: true, + default: true, + hint: "MHL.Settings.NotifyOnError.Hint", + name: "MHL.Settings.NotifyOnError.Name", + scope: "client", + type: Boolean, + }, +}; diff --git a/scripts/helpers/errorHelpers.mjs b/scripts/helpers/errorHelpers.mjs index 627343b..c9b4ae5 100644 --- a/scripts/helpers/errorHelpers.mjs +++ b/scripts/helpers/errorHelpers.mjs @@ -33,43 +33,19 @@ export function localizedBanner( } export function MHLError(str, data = {}, { notify = null, prefix = "MHL | ", log = {}, func = null } = {}) { - if (func && typeof func === "string") prefix += `${func} |`; + if (func && typeof func === "string") prefix += `${func} | `; return localizedError(str, data, { notify, prefix, log }); } -// taken from https://stackoverflow.com/a/32728075, slightly modernized -/** - * Checks if value is empty. Deep-checks arrays and objects - * Note: isEmpty([]) == true, isEmpty({}) == true, isEmpty([{0:false},"",0]) == true, isEmpty({0:1}) == false - * @param value - * @returns {boolean} - */ -export function isEmpty(value) { - const isEmptyObject = (a) => { - if (!Array.isArray(a)) { - // it's an Object, not an Array - const hasNonempty = Object.keys(a).some((e) => !isEmpty(a[e])); - return hasNonempty ? false : isEmptyObject(Object.keys(a)); - } - return !a.some((e) => !isEmpty(e)); - }; - return ( - value == false || - typeof value === "undefined" || - value == null || - (typeof value === "object" && isEmptyObject(value)) - ); -} - export function log(loggable, type = null, prefix = null) { type ??= "debug"; if (!CONSOLE_TYPES.includes(type)) { - throw MHLError(`MHL.Error.LogTypes`, { types: BANNER_TYPES.join(", ") }, { func: "log: ", log: { type } }); + throw MHLError(`MHL.Error.LogTypes`, { types: BANNER_TYPES.join(", ") }, { func: "log", log: { type } }); } prefix ??= ""; return console[type](prefix, loggable); } -export function mhlog(loggable, type = null, prefix = "MHL |") { +export function mhlog(loggable, type = null, prefix = "MHL | ") { return log(loggable, type, prefix); } diff --git a/scripts/init.mjs b/scripts/init.mjs index 6764746..2ae0e2a 100644 --- a/scripts/init.mjs +++ b/scripts/init.mjs @@ -1,7 +1,9 @@ import * as helpers from "./helpers/index.mjs"; import * as macros from "./macros/index.mjs"; -import * as classes from './classes/index.mjs'; -import { registerSettings } from "./settings.mjs"; +import * as classes from "./classes/index.mjs"; +import { registerSettings, updateSettingsCache } from "./settings.mjs"; +import { MODULE_ID } from "./constants.mjs"; +export const MODULE = ()=> game.modules.get(MODULE_ID) Hooks.on("init", () => { game.pf2emhl = { macros, @@ -15,11 +17,16 @@ Hooks.on("init", () => { if (game.modules.get("esheyw-transfer")?.active) { globalThis.mh = game.pf2emhl; } + game.pf2emhl.settings = {}; registerSettings(); Handlebars.registerHelper("mhlocalize", (value, options) => { - if ( value instanceof Handlebars.SafeString ) value = value.toString(); + if (value instanceof Handlebars.SafeString) value = value.toString(); const data = options.hash; - return helpers.localize(value,data) + return helpers.localize(value, data); }); -}); \ No newline at end of file +}); + +Hooks.once("setup", () => { + updateSettingsCache(); +}); diff --git a/scripts/macros/lashingCurrents.mjs b/scripts/macros/lashingCurrents.mjs index 0f6b1c9..50766e9 100644 --- a/scripts/macros/lashingCurrents.mjs +++ b/scripts/macros/lashingCurrents.mjs @@ -3,7 +3,7 @@ import { pickItemFromActor } from "../helpers/pf2eHelpers.mjs"; import { MHLError, localizedBanner } from "../helpers/errorHelpers.mjs"; const PREFIX = "MHL.Macro.LashingCurrents"; export async function lashingCurrents() { - const func = "lashingCurrents: "; + const func = "lashingCurrents"; const token = oneTokenOnly(); const actor = token.actor; const FORBIDDEN_RUNES = ["bloodbane", "kinWarding"]; diff --git a/scripts/macros/updateInitiativeStatistics.mjs b/scripts/macros/updateInitiativeStatistics.mjs index d2ccba0..b6aee6a 100644 --- a/scripts/macros/updateInitiativeStatistics.mjs +++ b/scripts/macros/updateInitiativeStatistics.mjs @@ -1,7 +1,7 @@ import { anyTokens } from "../helpers/tokenHelpers.mjs"; import { MHLDialog } from "../classes/MHLDialog.mjs"; -import { MODULE, fu } from "../constants.mjs"; -import { MHLError, mhlog } from "../helpers/errorHelpers.mjs"; +import { MODULE_ID, fu } from "../constants.mjs"; +import { MHLError } from "../helpers/errorHelpers.mjs"; import { localize } from "../helpers/stringHelpers.mjs"; export async function updateInitiativeStatistics() { @@ -76,7 +76,7 @@ export async function updateInitiativeStatistics() { const dialogData = { contentData, title: `Set Initiative Statistics`, - content: `modules/${MODULE}/templates/updateInitiativeStatistics.hbs`, + content: `modules/${MODULE_ID}/templates/updateInitiativeStatistics.hbs`, buttons: { yes: { icon: "", diff --git a/scripts/settings.mjs b/scripts/settings.mjs index 0101e24..6ec7b50 100644 --- a/scripts/settings.mjs +++ b/scripts/settings.mjs @@ -1,17 +1,27 @@ -import { MODULE } from "./constants.mjs"; -import { localize } from "./helpers/stringHelpers.mjs"; +import { MODULE_ID, SETTINGS, fu } from "./constants.mjs"; + export function registerSettings() { - game.settings.register(MODULE, "notify-on-error", { - config: true, - default: true, - hint: localize("MHL.Settings.NotifyOnError.Hint"), - name: localize("MHL.Settings.NotifyOnError.Name"), - scope: "client", - type: Boolean, - }); + for (const [setting, data] of Object.entries(SETTINGS)) { + const settingPath = setting.replace("_", "."); + fu.setProperty(game.pf2emhl.settings, settingPath, data?.default ?? null); + const originalOnChange = data?.onChange ?? null; + data.onChange = (value) => { + fu.setProperty(game.pf2emhl.settings, settingPath, value); + if (originalOnChange) originalOnChange(value); + }; + game.settings.register(MODULE_ID, setting, data); + } +} + +export function updateSettingsCache() { + for (const setting of Object.keys(SETTINGS)) { + const settingPath = setting.replace("_", "."); + fu.setProperty(game.pf2emhl.settings, settingPath, game.settings.get(MODULE_ID, setting)); + } } -export const NOTIFY = () => game.settings.get(MODULE, 'notify-on-error'); -export function setting(path) { - return game.settings.get(MODULE, path); -} \ No newline at end of file +export function setting(key) { + const settingPath = key.replace("_", "."); + const cached = fu.getProperty(game.pf2mhl.settings, settingPath); + return cached !== undefined ? cached : game.settings.get(MODULE_ID, key); +} diff --git a/unpacks/helper-library-macros/Lashing_Currents_hRCi5uHwsTJHQ4Dt.json b/unpacks/helper-library-macros/Lashing_Currents_hRCi5uHwsTJHQ4Dt.json new file mode 100644 index 0000000..bdc7c66 --- /dev/null +++ b/unpacks/helper-library-macros/Lashing_Currents_hRCi5uHwsTJHQ4Dt.json @@ -0,0 +1,26 @@ +{ + "name": "Lashing Currents", + "type": "script", + "scope": "global", + "author": "xuYHxk9sJoYKM7d6", + "img": "icons/magic/water/waves-water-blue.webp", + "command": "await game.pf2emhl.macros.lashingCurrents();", + "folder": null, + "ownership": { + "default": 0 + }, + "flags": { + "core": {} + }, + "_stats": { + "systemId": "pf2e", + "systemVersion": "5.12.7", + "coreVersion": "11.315", + "createdTime": 1706489620730, + "modifiedTime": 1706489726405, + "lastModifiedBy": "xuYHxk9sJoYKM7d6" + }, + "_id": "hRCi5uHwsTJHQ4Dt", + "sort": 0, + "_key": "!macros!hRCi5uHwsTJHQ4Dt" +}