Skip to content

Commit

Permalink
Refactor settings migration code in functional style
Browse files Browse the repository at this point in the history
  • Loading branch information
taichimaeda committed Apr 19, 2024
1 parent 0525a8d commit 0022218
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/settings/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type SettingsMigrator = (settings: object) => object;
export type SettingsMigrator<From, To> = (settings: From) => To;
46 changes: 28 additions & 18 deletions src/settings/migrators/1.1.0-1.2.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@ import { SettingsMigrator } from '..';
import { MarkpilotSettings1_1_0 } from '../versions/1.1.0';
import { MarkpilotSettings1_2_0 } from '../versions/1.2.0';

export const migrateVersion1_1_0_toVersion1_2_0: SettingsMigrator = (
settings: MarkpilotSettings1_1_0,
) => {
const apiKey = settings.apiKey as string;
delete settings.apiKey;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const newSettings: MarkpilotSettings1_2_0 = structuredClone(settings) as any;
newSettings.providers = {
openai: {
apiKey,
export const migrateVersion1_1_0_toVersion1_2_0: SettingsMigrator<
MarkpilotSettings1_1_0,
MarkpilotSettings1_2_0
> = (settings) => {
const newSettings: MarkpilotSettings1_2_0 = {
version: '1.2.0',
providers: {
openai: {
apiKey: settings.apiKey,
},
openrouter: {
apiKey: undefined,
},
ollama: {
apiUrl: undefined,
},
},
openrouter: {
apiKey: undefined,
completions: {
...settings.completions,
provider: 'openai',
ignoredFiles: [],
ignoredTags: [],
},
ollama: {
apiUrl: undefined,
chat: {
...settings.chat,
provider: 'openai',
},
cache: {
enabled: true, // Enable cache by default.
},
usage: settings.usage,
};
newSettings.completions.provider = 'openai';
newSettings.completions.ignoredFiles = [];
newSettings.completions.ignoredTags = [];
newSettings.chat.provider = 'openai';
// Update if default models are still selected.
if (settings.completions.model === 'gpt-3.5-turbo-instruct') {
newSettings.completions.model = 'gpt-3.5-turbo';
Expand Down
12 changes: 4 additions & 8 deletions src/settings/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import Markpilot from '../main';
import { migrateVersion1_1_0_toVersion1_2_0 } from './migrators/1.1.0-1.2.0';

export class SettingsMigrationsRunner {
nextMigrators: Record<string, SettingsMigrator> = {
migrators: Record<string, SettingsMigrator<object, object>> = {
'1.1.0': migrateVersion1_1_0_toVersion1_2_0,
};
nextVersions: Record<string, string> = {
'1.1.0': '1.2.0',
};

constructor(private plugin: Markpilot) {}

Expand All @@ -19,16 +16,15 @@ export class SettingsMigrationsRunner {
// NOTE:
// An infinite loop would also work because of the break statement
// but we take the safe path here.
const maxIterations = Object.keys(this.nextMigrators).length;
const maxIterations = Object.keys(this.migrators).length;
for (let i = 0; i < maxIterations + 1; i++) {
// Settings versions and migrations were introduced from version 1.1.0.
const version = settings.version ?? '1.1.0';
const migrator = this.nextMigrators[version];
const migrator = this.migrators[version];
if (migrator === undefined) {
break;
}
settings = migrator(settings);
settings.version = this.nextVersions[version];
settings = migrator(structuredClone(settings));
}

this.plugin.settings = settings;
Expand Down

0 comments on commit 0022218

Please sign in to comment.