mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-02 18:10:18 +05:30
feat(migration): add v4-to-v5 settings migration runner with backup
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
class MigrationRunner {
|
||||
constructor({ dir, transform }) {
|
||||
this.dir = dir;
|
||||
this.transform = transform;
|
||||
this.file = path.join(dir, 'settings.json');
|
||||
this.backup = path.join(dir, 'settings.v4.bak.json');
|
||||
}
|
||||
|
||||
run() {
|
||||
if (!fs.existsSync(this.file)) {
|
||||
this._writeDefaults();
|
||||
return 'fresh';
|
||||
}
|
||||
const raw = JSON.parse(fs.readFileSync(this.file, 'utf-8'));
|
||||
if (raw && raw['migration.version'] === 5) {
|
||||
return 'skipped';
|
||||
}
|
||||
try {
|
||||
const v5 = this.transform(raw);
|
||||
fs.copyFileSync(this.file, this.backup);
|
||||
fs.writeFileSync(this.file, JSON.stringify({ ...v5, 'migration.version': 5 }, null, 2));
|
||||
return 'migrated';
|
||||
} catch (err) {
|
||||
console.error('[migration-runner] transform failed:', err.message);
|
||||
return 'failed';
|
||||
}
|
||||
}
|
||||
|
||||
_writeDefaults() {
|
||||
const v5 = this.transform({});
|
||||
fs.writeFileSync(this.file, JSON.stringify({ ...v5, 'migration.version': 5 }, null, 2));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { MigrationRunner };
|
||||
@@ -0,0 +1,26 @@
|
||||
import { z } from 'zod';
|
||||
import { settingsSchema } from '@/lib/validators';
|
||||
|
||||
export const v4SettingsSchema = z.object({
|
||||
theme: z.enum(['light', 'dark', 'auto']).default('auto'),
|
||||
customCss: z.string().optional().nullable(),
|
||||
recentFiles: z.array(z.string()).default([]),
|
||||
editorFontSize: z.number().min(10).max(28).default(14),
|
||||
keyBindings: z.record(z.string(), z.string()).optional(),
|
||||
snippets: z.array(z.unknown()).default([]),
|
||||
});
|
||||
|
||||
export function migrateV4ToV5(v4: unknown): z.infer<typeof settingsSchema> {
|
||||
const parsed = v4SettingsSchema.parse(v4);
|
||||
const defaults = settingsSchema.parse({});
|
||||
return {
|
||||
...defaults,
|
||||
...parsed,
|
||||
theme: parsed.theme === 'auto' ? 'system' : parsed.theme,
|
||||
customCssPath: parsed.customCss ?? null,
|
||||
recentFiles: parsed.recentFiles,
|
||||
editorFontSize: parsed.editorFontSize,
|
||||
userBindings: parsed.keyBindings ?? defaults.userBindings ?? {},
|
||||
snippets: parsed.snippets,
|
||||
};
|
||||
}
|
||||
@@ -6,7 +6,7 @@ export const settingsSchema = z.object({
|
||||
lineNumbers: z.boolean().default(true),
|
||||
wordWrap: z.boolean().default(true),
|
||||
minimap: z.boolean().default(true),
|
||||
theme: z.enum(['light', 'dark', 'auto']).default('auto'),
|
||||
theme: z.enum(['light', 'dark', 'system']).default('system'),
|
||||
accentColor: z.enum(['brand', 'blue', 'green', 'purple', 'orange']).default('brand'),
|
||||
fontFamily: z.enum(['system', 'jetbrains', 'fira']).default('system'),
|
||||
pdfFormat: z.enum(['letter', 'a4', 'legal']).default('a4'),
|
||||
@@ -21,6 +21,7 @@ export const settingsSchema = z.object({
|
||||
welcomeDismissed: z.boolean().default(false),
|
||||
editorFontSize: z.number().min(10).max(28).default(14),
|
||||
customCssPath: z.string().nullable().default(null),
|
||||
userBindings: z.record(z.string(), z.string()).default({}),
|
||||
});
|
||||
export type Settings = z.infer<typeof settingsSchema>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user