feat(config): add migration framework
This commit is contained in:
+23
-26
@@ -3,6 +3,11 @@ import {
|
||||
normalizeConfiguredKeymaps,
|
||||
} from "../constants/keymaps.ts";
|
||||
import { normalizeAskConfig } from "./defaults.ts";
|
||||
import {
|
||||
AskConfigVersionMigrationError,
|
||||
type AskConfigVersionMigrationResult,
|
||||
migrateAskConfigFileToCurrent,
|
||||
} from "./migrations/index.ts";
|
||||
import type { AskConfig, AskConfigFileV1 } from "./schema.ts";
|
||||
import { validateAskConfigFileV1 } from "./schema.ts";
|
||||
|
||||
@@ -25,37 +30,29 @@ export interface AskConfigMigrationResult {
|
||||
}
|
||||
|
||||
export function migrateAskConfig(raw: unknown): AskConfigMigrationResult {
|
||||
if (!(raw && typeof raw === "object")) {
|
||||
let migratedFile: AskConfigVersionMigrationResult;
|
||||
try {
|
||||
migratedFile = migrateAskConfigFileToCurrent(raw);
|
||||
} catch (error) {
|
||||
throw new AskConfigMigrationError(
|
||||
"Config was invalid or unsupported.",
|
||||
error instanceof AskConfigVersionMigrationError
|
||||
? error.reason
|
||||
: "migration_failed"
|
||||
);
|
||||
}
|
||||
|
||||
if (!validateAskConfigFileV1.Check(migratedFile.config)) {
|
||||
throw new AskConfigMigrationError(
|
||||
"Config was invalid or unsupported.",
|
||||
"invalid_or_unsupported"
|
||||
);
|
||||
}
|
||||
|
||||
if (!("schemaVersion" in raw)) {
|
||||
throw new AskConfigMigrationError(
|
||||
"Config was invalid or unsupported.",
|
||||
"invalid_or_unsupported"
|
||||
);
|
||||
}
|
||||
|
||||
if ((raw as { schemaVersion?: unknown }).schemaVersion !== 1) {
|
||||
throw new AskConfigMigrationError(
|
||||
"Config was invalid or unsupported.",
|
||||
"invalid_or_unsupported"
|
||||
);
|
||||
}
|
||||
|
||||
if (!validateAskConfigFileV1.Check(raw)) {
|
||||
throw new AskConfigMigrationError(
|
||||
"Config was invalid or unsupported.",
|
||||
"invalid_or_unsupported"
|
||||
);
|
||||
}
|
||||
|
||||
const config = normalizeAskConfig(raw as AskConfigFileV1);
|
||||
const currentFile = migratedFile.config as AskConfigFileV1;
|
||||
const config = normalizeAskConfig(currentFile);
|
||||
const keymapsResult = normalizeConfiguredKeymaps(
|
||||
(raw as AskConfigFileV1).keymaps as
|
||||
currentFile.keymaps as
|
||||
| Partial<Record<AskConfigurableKeyAction, unknown>>
|
||||
| undefined
|
||||
);
|
||||
@@ -65,7 +62,7 @@ export function migrateAskConfig(raw: unknown): AskConfigMigrationResult {
|
||||
...config,
|
||||
keymaps: normalizeAskConfig().keymaps,
|
||||
},
|
||||
migrated: false,
|
||||
migrated: migratedFile.migrated,
|
||||
notice: `${keymapsResult.error} Using default ask keymaps for this session. Edit the config and restart pi or run /reload.`,
|
||||
};
|
||||
}
|
||||
@@ -75,6 +72,6 @@ export function migrateAskConfig(raw: unknown): AskConfigMigrationResult {
|
||||
...config,
|
||||
keymaps: keymapsResult.keymaps,
|
||||
},
|
||||
migrated: false,
|
||||
migrated: migratedFile.migrated,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import type { AskConfigMigration, VersionedAskConfigFile } from "./types.ts";
|
||||
|
||||
export const CURRENT_ASK_CONFIG_SCHEMA_VERSION = 1;
|
||||
|
||||
const ASK_CONFIG_MIGRATIONS: AskConfigMigration[] = [];
|
||||
|
||||
export class AskConfigVersionMigrationError extends Error {
|
||||
readonly reason: "invalid_or_unsupported" | "migration_failed";
|
||||
|
||||
constructor(
|
||||
message: string,
|
||||
reason: "invalid_or_unsupported" | "migration_failed"
|
||||
) {
|
||||
super(message);
|
||||
this.reason = reason;
|
||||
}
|
||||
}
|
||||
|
||||
export interface AskConfigVersionMigrationResult {
|
||||
config: VersionedAskConfigFile;
|
||||
migrated: boolean;
|
||||
}
|
||||
|
||||
export function migrateAskConfigFileToCurrent(
|
||||
raw: unknown
|
||||
): AskConfigVersionMigrationResult {
|
||||
const start = getVersionedConfigFile(raw);
|
||||
let config = start;
|
||||
let migrated = false;
|
||||
|
||||
while (config.schemaVersion < CURRENT_ASK_CONFIG_SCHEMA_VERSION) {
|
||||
const migration = ASK_CONFIG_MIGRATIONS.find(
|
||||
(candidate) => candidate.from === config.schemaVersion
|
||||
);
|
||||
if (!migration) {
|
||||
throw new AskConfigVersionMigrationError(
|
||||
`No ask config migration from schemaVersion ${config.schemaVersion}`,
|
||||
"migration_failed"
|
||||
);
|
||||
}
|
||||
config = migration.migrate(config);
|
||||
if (config.schemaVersion !== migration.to) {
|
||||
throw new AskConfigVersionMigrationError(
|
||||
`Ask config migration from schemaVersion ${migration.from} did not produce schemaVersion ${migration.to}`,
|
||||
"migration_failed"
|
||||
);
|
||||
}
|
||||
migrated = true;
|
||||
}
|
||||
|
||||
if (config.schemaVersion !== CURRENT_ASK_CONFIG_SCHEMA_VERSION) {
|
||||
throw new AskConfigVersionMigrationError(
|
||||
`Unsupported ask config schemaVersion ${config.schemaVersion}`,
|
||||
"invalid_or_unsupported"
|
||||
);
|
||||
}
|
||||
|
||||
return { config, migrated };
|
||||
}
|
||||
|
||||
function getVersionedConfigFile(raw: unknown): VersionedAskConfigFile {
|
||||
if (!(raw && typeof raw === "object")) {
|
||||
throw new AskConfigVersionMigrationError(
|
||||
"Ask config must be an object",
|
||||
"invalid_or_unsupported"
|
||||
);
|
||||
}
|
||||
const schemaVersion = (raw as { schemaVersion?: unknown }).schemaVersion;
|
||||
if (!Number.isInteger(schemaVersion) || typeof schemaVersion !== "number") {
|
||||
throw new AskConfigVersionMigrationError(
|
||||
"Ask config must include an integer schemaVersion",
|
||||
"invalid_or_unsupported"
|
||||
);
|
||||
}
|
||||
return raw as VersionedAskConfigFile;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
export interface VersionedAskConfigFile {
|
||||
schemaVersion: number;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface AskConfigMigration {
|
||||
from: number;
|
||||
migrate: (config: VersionedAskConfigFile) => VersionedAskConfigFile;
|
||||
to: number;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { mkdir, readFile, rename } from "node:fs/promises";
|
||||
import { dirname } from "node:path";
|
||||
|
||||
export interface AskConfigPathMigrationOptions {
|
||||
currentPath: string;
|
||||
legacyPaths: string[];
|
||||
}
|
||||
|
||||
export async function migrateAskConfigPathIfNeeded({
|
||||
currentPath,
|
||||
legacyPaths,
|
||||
}: AskConfigPathMigrationOptions): Promise<void> {
|
||||
if (await pathExists(currentPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const legacyPath = await findFirstExistingPath(legacyPaths);
|
||||
if (!legacyPath) {
|
||||
return;
|
||||
}
|
||||
|
||||
await mkdir(dirname(currentPath), { recursive: true });
|
||||
await rename(legacyPath, currentPath);
|
||||
}
|
||||
|
||||
async function findFirstExistingPath(
|
||||
paths: string[]
|
||||
): Promise<string | undefined> {
|
||||
for (const path of paths) {
|
||||
if (await pathExists(path)) {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
async function pathExists(path: string): Promise<boolean> {
|
||||
try {
|
||||
await readFile(path, "utf-8");
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (isMissingFileError(error)) {
|
||||
return false;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function isMissingFileError(error: unknown): boolean {
|
||||
return (
|
||||
!!error &&
|
||||
typeof error === "object" &&
|
||||
"code" in error &&
|
||||
error.code === "ENOENT"
|
||||
);
|
||||
}
|
||||
+14
-34
@@ -10,6 +10,7 @@ import {
|
||||
toAskConfigFileV1,
|
||||
} from "./defaults.ts";
|
||||
import { AskConfigMigrationError, migrateAskConfig } from "./migrate.ts";
|
||||
import { migrateAskConfigPathIfNeeded } from "./path-migrations.ts";
|
||||
import type { AskConfig } from "./schema.ts";
|
||||
|
||||
export interface AskConfigNotice {
|
||||
@@ -28,15 +29,13 @@ export class AskConfigStore {
|
||||
private notice?: AskConfigNotice;
|
||||
private readonly listeners = new Set<(config: AskConfig) => void>();
|
||||
private readonly configPath: string;
|
||||
private readonly legacyConfigPath?: string;
|
||||
private readonly legacyConfigPaths: string[];
|
||||
|
||||
constructor(
|
||||
configPath = getAskConfigPath(),
|
||||
legacyConfigPath = getLegacyAskConfigPath()
|
||||
) {
|
||||
this.configPath = configPath;
|
||||
this.legacyConfigPath =
|
||||
legacyConfigPath === configPath ? undefined : legacyConfigPath;
|
||||
constructor(configPath?: string, legacyConfigPaths?: string[]) {
|
||||
this.configPath = configPath ?? getAskConfigPath();
|
||||
this.legacyConfigPaths = (
|
||||
legacyConfigPaths ?? (configPath ? [] : getLegacyAskConfigPaths())
|
||||
).filter((path) => path !== this.configPath);
|
||||
}
|
||||
|
||||
subscribe(onChange: (config: AskConfig) => void): () => void {
|
||||
@@ -148,17 +147,10 @@ export class AskConfigStore {
|
||||
}
|
||||
|
||||
private async migrateLegacyConfigIfNeeded(): Promise<void> {
|
||||
if (!this.legacyConfigPath) {
|
||||
return;
|
||||
}
|
||||
if (await pathExists(this.configPath)) {
|
||||
return;
|
||||
}
|
||||
if (!(await pathExists(this.legacyConfigPath))) {
|
||||
return;
|
||||
}
|
||||
await mkdir(dirname(this.configPath), { recursive: true });
|
||||
await rename(this.legacyConfigPath, this.configPath);
|
||||
await migrateAskConfigPathIfNeeded({
|
||||
currentPath: this.configPath,
|
||||
legacyPaths: this.legacyConfigPaths,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,11 +162,11 @@ export function getAskConfigStore(): AskConfigStore {
|
||||
}
|
||||
|
||||
export function getAskConfigPath(): string {
|
||||
return join(getAgentDir(), "eko24ive-pi-ask.json");
|
||||
return join(getAgentDir(), "extensions", "eko24ive-pi-ask.json");
|
||||
}
|
||||
|
||||
export function getLegacyAskConfigPath(): string {
|
||||
return join(getAgentDir(), "extensions", "eko24ive-pi-ask.json");
|
||||
export function getLegacyAskConfigPaths(): string[] {
|
||||
return [join(getAgentDir(), "eko24ive-pi-ask.json")];
|
||||
}
|
||||
|
||||
function createBackupPath(path: string, date: Date): string {
|
||||
@@ -182,18 +174,6 @@ function createBackupPath(path: string, date: Date): string {
|
||||
return path.replace(JSON_EXTENSION_PATTERN, `.${timestamp}.bak.json`);
|
||||
}
|
||||
|
||||
async function pathExists(path: string): Promise<boolean> {
|
||||
try {
|
||||
await readFile(path, "utf-8");
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (isMissingFileError(error)) {
|
||||
return false;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function isMissingFileError(error: unknown): boolean {
|
||||
return (
|
||||
!!error &&
|
||||
|
||||
Reference in New Issue
Block a user