skillhub/cli/src/stores/sync-workspace-store.ts
mrLi 84feb38931 feat(cli): add namespace workspace sync
Signed-off-by: mrLi <50289372+15258725278@users.noreply.github.com>
2026-08-18 18:24:11 +08:00

39 lines
1.1 KiB
TypeScript

import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { pathExists } from '../platform/paths'
export interface NamespaceSyncStateSkill {
version: string
fingerprint: string
}
export interface NamespaceSyncState {
registry: string
namespace: string
lastSyncAt: string
skills: Record<string, NamespaceSyncStateSkill>
}
export class SyncWorkspaceStore {
readonly path: string
constructor(rootDir: string) {
this.path = join(rootDir, '.skillhub', 'namespace-sync.json')
}
async read(): Promise<NamespaceSyncState | null> {
if (!(await pathExists(this.path))) return null
return JSON.parse(await readFile(this.path, 'utf8')) as NamespaceSyncState
}
async write(state: NamespaceSyncState): Promise<void> {
await mkdir(dirname(this.path), { recursive: true })
const tempPath = `${this.path}.${process.pid}.${Date.now()}.tmp`
try {
await writeFile(tempPath, JSON.stringify(state, null, 2))
await rename(tempPath, this.path)
} finally {
await rm(tempPath, { force: true }).catch(() => {})
}
}
}