81 lines
2.6 KiB
TypeScript
81 lines
2.6 KiB
TypeScript
import { migrate as migrate001 } from "./migrations/001";
|
|
|
|
async function migrate(): Promise<void> {
|
|
if (!game.user.isGM) {
|
|
return;
|
|
}
|
|
|
|
const oldMigrationVersion: number = game.settings.get("ds4", "systemMigrationVersion");
|
|
|
|
const targetMigrationVersion = migrations.length;
|
|
|
|
if (isFirstWorldStart(oldMigrationVersion)) {
|
|
game.settings.set("ds4", "systemMigrationVersion", targetMigrationVersion);
|
|
return;
|
|
}
|
|
|
|
return migrateFromTo(oldMigrationVersion, targetMigrationVersion);
|
|
}
|
|
|
|
async function migrateFromTo(oldMigrationVersion: number, targetMigrationVersion: number): Promise<void> {
|
|
if (!game.user.isGM) {
|
|
return;
|
|
}
|
|
|
|
const migrationsToExecute = migrations.slice(oldMigrationVersion, targetMigrationVersion);
|
|
|
|
if (migrationsToExecute.length > 0) {
|
|
ui.notifications.info(
|
|
game.i18n.format("DS4.InfoSystemUpdateStart", {
|
|
currentVersion: oldMigrationVersion,
|
|
targetVersion: targetMigrationVersion,
|
|
}),
|
|
{ permanent: true },
|
|
);
|
|
|
|
for (const [i, migration] of migrationsToExecute.entries()) {
|
|
const currentMigrationVersion = oldMigrationVersion + i + 1;
|
|
console.log("executing migration script ", currentMigrationVersion);
|
|
try {
|
|
await migration();
|
|
game.settings.set("ds4", "systemMigrationVersion", currentMigrationVersion);
|
|
} catch (err) {
|
|
ui.notifications.error(
|
|
game.i18n.format("DS4.ErrorDuringMigration", {
|
|
currentVersion: oldMigrationVersion,
|
|
targetVersion: targetMigrationVersion,
|
|
migrationVersion: currentMigrationVersion,
|
|
}),
|
|
{ permanent: true },
|
|
);
|
|
err.message = `Failed ds4 system migration: ${err.message}`;
|
|
console.error(err);
|
|
return;
|
|
}
|
|
}
|
|
|
|
ui.notifications.info(
|
|
game.i18n.format("DS4.InfoSystemUpdateCompleted", {
|
|
currentVersion: oldMigrationVersion,
|
|
targetVersion: targetMigrationVersion,
|
|
}),
|
|
{ permanent: true },
|
|
);
|
|
}
|
|
}
|
|
|
|
function getTargetMigrationVersion(): number {
|
|
return migrations.length;
|
|
}
|
|
|
|
const migrations: Array<() => Promise<void>> = [migrate001];
|
|
|
|
function isFirstWorldStart(migrationVersion: number): boolean {
|
|
return migrationVersion < 0;
|
|
}
|
|
|
|
export const migration = {
|
|
migrate: migrate,
|
|
migrateFromTo: migrateFromTo,
|
|
getTargetMigrationVersion: getTargetMigrationVersion,
|
|
};
|