Skip to content

Commit

Permalink
feat: Add macro migration
Browse files Browse the repository at this point in the history
  • Loading branch information
mcavallo committed Jul 1, 2023
1 parent 2c2ee36 commit 9514372
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export const MODULE = {
NAME: 'Random Target',
ID: 'random-target',
NAMESPACE: 'randomTarget',
MACRO_COMPENDIUM: 'random-target-macros',
MACRO_ID: 'oIVhNA8Po9HpYdJc',
};

export const SETTING_IDS = {
Expand Down
41 changes: 41 additions & 0 deletions src/lib/MacroMigration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { MODULE } from '../constants.js';

export class MacroMigration {
constructor() {
this.compendiumId = `${MODULE.ID}.${MODULE.MACRO_COMPENDIUM}`;
this.macroSourceId = `Compendium.${this.compendiumId}.Macro.${MODULE.MACRO_ID}`;
}

_getGameMacros() {
return Array.from(game.macros).filter(macro => macro._source?.flags?.core?.sourceId === this.macroSourceId);
}

async _getLatestCompendiumVersionCommand() {
const compendiumMacros = await game.packs.get(this.compendiumId).getDocuments();
const latestMacro = compendiumMacros.find(macro => macro._id === MODULE.MACRO_ID);

if (!latestMacro) {
return null;
}

return latestMacro.command;
}

async run() {
const gameMacros = this._getGameMacros();

if (gameMacros.length === 0) {
return;
}

const latestCommand = await this._getLatestCompendiumVersionCommand();

if (!latestCommand) {
return;
}

gameMacros.forEach(macro => {
macro.command = latestCommand;
});
}
}
6 changes: 6 additions & 0 deletions src/module.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { run } from './apps/RandomTarget.js';
import { MODULE } from './constants.js';
import { MacroMigration } from './lib/MacroMigration.js';
import { registerSettings, saveSetting } from './settings.js';

Hooks.once('init', function () {
Expand All @@ -11,3 +12,8 @@ Hooks.once('init', function () {
settings: initialSettings,
};
});

Hooks.once('ready', function () {
const migration = new MacroMigration();
migration.run();
});

0 comments on commit 9514372

Please sign in to comment.