UNPKG

open-agreements

Version:

Open-source legal template filling CLI and library

147 lines • 4.66 kB
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; import { homedir } from 'node:os'; import { join } from 'node:path'; import { randomUUID } from 'node:crypto'; import { z } from 'zod'; import { appendJsonl, readJsonl } from './jsonl-stores.js'; import { ClosingChecklistSchema } from './schemas.js'; const STATE_ROOT = join(homedir(), '.open-agreements', 'checklists'); export const ChecklistStateSchema = z.object({ checklist_id: z.string().min(1), revision: z.number().int().nonnegative(), checklist: ClosingChecklistSchema, }); function stateRoot(root) { return root ?? STATE_ROOT; } function checklistDir(uuid, root) { return join(stateRoot(root), uuid); } function statePath(uuid, root) { return join(checklistDir(uuid, root), 'state.json'); } function metaPath(uuid, root) { return join(checklistDir(uuid, root), 'meta.json'); } function historyPath(uuid, root) { return join(checklistDir(uuid, root), 'history.jsonl'); } function writeJson(path, value) { writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`, 'utf-8'); } function readJson(path) { return JSON.parse(readFileSync(path, 'utf-8')); } export function createChecklist(dealName, initialData, opts) { const uuid = randomUUID(); const dir = checklistDir(uuid, opts?.root); mkdirSync(dir, { recursive: true }); const now = new Date().toISOString().slice(0, 10); const checklist = initialData ?? { deal_name: dealName, updated_at: now, documents: {}, checklist_entries: {}, action_items: {}, issues: {}, }; const state = { checklist_id: uuid, revision: 0, checklist, }; const meta = { deal_name: dealName, created_at: now, uuid, }; writeJson(statePath(uuid, opts?.root), state); writeJson(metaPath(uuid, opts?.root), meta); return { uuid, dir }; } export function listChecklists(opts) { const root = stateRoot(opts?.root); if (!existsSync(root)) return []; const entries = readdirSync(root, { withFileTypes: true }); const results = []; for (const entry of entries) { if (!entry.isDirectory()) continue; const mp = metaPath(entry.name, opts?.root); const sp = statePath(entry.name, opts?.root); if (!existsSync(mp) || !existsSync(sp)) continue; try { const meta = readJson(mp); const state = readJson(sp); results.push({ ...meta, revision: state.revision, updated_at: state.checklist.updated_at, }); } catch { // Skip corrupt entries } } return results.sort((a, b) => a.deal_name.localeCompare(b.deal_name)); } export function resolveChecklist(nameOrId, opts) { const root = stateRoot(opts?.root); if (!existsSync(root)) return null; // Exact UUID match const exactDir = checklistDir(nameOrId, opts?.root); if (existsSync(join(exactDir, 'state.json'))) return nameOrId; // Fuzzy match by deal name const entries = readdirSync(root, { withFileTypes: true }); const needle = nameOrId.toLowerCase(); for (const entry of entries) { if (!entry.isDirectory()) continue; const mp = metaPath(entry.name, opts?.root); if (!existsSync(mp)) continue; try { const meta = readJson(mp); if (meta.deal_name.toLowerCase() === needle) return entry.name; } catch { // Skip } } // Substring match as fallback for (const entry of entries) { if (!entry.isDirectory()) continue; const mp = metaPath(entry.name, opts?.root); if (!existsSync(mp)) continue; try { const meta = readJson(mp); if (meta.deal_name.toLowerCase().includes(needle)) return entry.name; } catch { // Skip } } return null; } export function getChecklistState(uuid, opts) { const path = statePath(uuid, opts?.root); return ChecklistStateSchema.parse(readJson(path)); } export function saveChecklistState(uuid, state, opts) { writeJson(statePath(uuid, opts?.root), state); } export async function appendHistory(uuid, record, opts) { await appendJsonl(historyPath(uuid, opts?.root), record); } export function getHistory(uuid, opts) { return readJsonl(historyPath(uuid, opts?.root)); } //# sourceMappingURL=state-manager.js.map