recent project

This commit is contained in:
2026-05-29 10:29:59 +10:00
parent 07f22bdaa1
commit c6f55ddfe1
7 changed files with 187 additions and 43 deletions

View File

@@ -2,18 +2,17 @@ import { join, basename } from 'path'
import { homedir } from 'os'
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
export interface RecentProject {
path: string
title: string
}
export interface GlobalConfig {
apiKey?: string
projectPath?: string
projectTitle?: string
fontSize?: number
theme?: 'dark' | 'light'
// Manuscript metadata
authorName?: string
penName?: string
authorAddress?: string
authorEmail?: string
authorPhone?: string
recentProjects?: RecentProject[]
}
const CONFIG_DIR = join(homedir(), '.hohoff')
@@ -22,7 +21,12 @@ const CONFIG_FILE = join(CONFIG_DIR, 'config.json')
let _config: GlobalConfig = {}
try {
_config = JSON.parse(readFileSync(CONFIG_FILE, 'utf-8'))
const raw = JSON.parse(readFileSync(CONFIG_FILE, 'utf-8'))
// Migrate old string[] recentProjects to RecentProject[]
if (Array.isArray(raw.recentProjects) && typeof raw.recentProjects[0] === 'string') {
raw.recentProjects = (raw.recentProjects as string[]).map((p) => ({ path: p, title: basename(p) }))
}
_config = raw
} catch {
// first run or file missing — use fallbacks
}
@@ -35,8 +39,8 @@ export const getDraftRoot = (): string =>
export const getApiKey = (): string | undefined =>
_config.apiKey ?? process.env.ANTHROPIC_API_KEY
export const getProjectTitle = (): string =>
_config.projectTitle?.trim() || basename(getDraftRoot())
export const getProjectTitle = (projectTitle?: string): string =>
projectTitle?.trim() || basename(getDraftRoot())
export function readGlobalConfig(): GlobalConfig {
return { ..._config }
@@ -47,3 +51,19 @@ export function writeGlobalConfig(updates: Partial<GlobalConfig>): void {
mkdirSync(CONFIG_DIR, { recursive: true })
writeFileSync(CONFIG_FILE, JSON.stringify(_config, null, 2), 'utf-8')
}
export function addRecentProject(path: string, title: string): void {
const existing = _config.recentProjects ?? []
const deduped = [{ path, title }, ...existing.filter((p) => p.path !== path)].slice(0, 10)
writeGlobalConfig({ recentProjects: deduped })
}
export function updateRecentProjectTitle(path: string, title: string): void {
const existing = _config.recentProjects ?? []
const updated = existing.map((p) => p.path === path ? { path, title } : p)
writeGlobalConfig({ recentProjects: updated })
}
export function getRecentProjects(): RecentProject[] {
return _config.recentProjects ?? []
}