M0-M11 狩夜人笔记初始实现:11里程碑+86测试
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
// ConfigStore — 应用配置 JSON 持久化(vault meta、最近库)
|
||||
import nodePath from 'node:path';
|
||||
import { promises as fs } from 'node:fs';
|
||||
import type { VaultMeta } from '@hunter/shared';
|
||||
import { RECENT_VAULT_MAX } from '@hunter/shared';
|
||||
|
||||
export class ConfigStore {
|
||||
private file: string;
|
||||
private cache: { vault?: VaultMeta } | null = null;
|
||||
|
||||
constructor(userDataDir: string) {
|
||||
this.file = nodePath.join(userDataDir, 'config.json');
|
||||
}
|
||||
|
||||
private async read(): Promise<{ vault?: VaultMeta }> {
|
||||
if (this.cache) return this.cache;
|
||||
try {
|
||||
const raw = await fs.readFile(this.file, 'utf8');
|
||||
this.cache = JSON.parse(raw);
|
||||
} catch {
|
||||
this.cache = {};
|
||||
}
|
||||
return this.cache!;
|
||||
}
|
||||
|
||||
private async write(): Promise<void> {
|
||||
const dir = nodePath.dirname(this.file);
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
const tmp = this.file + '.tmp';
|
||||
await fs.writeFile(tmp, JSON.stringify(this.cache, null, 2), 'utf8');
|
||||
await fs.rename(tmp, this.file);
|
||||
}
|
||||
|
||||
async getVaultMeta(): Promise<VaultMeta> {
|
||||
const c = await this.read();
|
||||
return c.vault ?? { currentRoot: '', recents: [], defaultRoot: '' };
|
||||
}
|
||||
|
||||
async setCurrentRoot(root: string): Promise<VaultMeta> {
|
||||
const meta = await this.getVaultMeta();
|
||||
meta.currentRoot = root;
|
||||
// 加入最近库(去重置顶,截断到上限)
|
||||
meta.recents = [root, ...meta.recents.filter(r => r !== root)].slice(0, RECENT_VAULT_MAX);
|
||||
(await this.read()).vault = meta;
|
||||
await this.write();
|
||||
return meta;
|
||||
}
|
||||
|
||||
async setDefaultRoot(root: string): Promise<VaultMeta> {
|
||||
const meta = await this.getVaultMeta();
|
||||
meta.defaultRoot = root;
|
||||
(await this.read()).vault = meta;
|
||||
await this.write();
|
||||
return meta;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user