|
| 1 | +import type { RuntimeCore } from "./runtimeCore"; |
| 2 | + |
| 3 | +export type WorkspaceContainer = Awaited<ReturnType<RuntimeCore["exportLedger"]>>; |
| 4 | + |
| 5 | +export type WorkspaceSummary = { |
| 6 | + providerId: string; |
| 7 | + workspaceId: string; |
| 8 | + pointer: string; |
| 9 | + head: string; |
| 10 | + commitCount: number; |
| 11 | + entryCount: number; |
| 12 | +}; |
| 13 | + |
| 14 | +type WorkspaceLifecycleRuntime = Pick< |
| 15 | + RuntimeCore, |
| 16 | + "createLedger" | "reopen" | "exportLedger" | "importLedger" |
| 17 | +> & { |
| 18 | + storage: Pick<RuntimeCore["storage"], "getActiveRef">; |
| 19 | +}; |
| 20 | + |
| 21 | +export function shortWorkspaceHead(value: string): string { |
| 22 | + return value.slice(0, 12); |
| 23 | +} |
| 24 | + |
| 25 | +export async function readWorkspaceSummary( |
| 26 | + runtime: WorkspaceLifecycleRuntime, |
| 27 | +): Promise<WorkspaceSummary> { |
| 28 | + const [activeRef, ledger] = await Promise.all([ |
| 29 | + runtime.storage.getActiveRef(), |
| 30 | + runtime.exportLedger(), |
| 31 | + ]); |
| 32 | + |
| 33 | + return { |
| 34 | + providerId: activeRef.providerId, |
| 35 | + workspaceId: activeRef.workspaceId, |
| 36 | + pointer: activeRef.pointer, |
| 37 | + head: ledger.head, |
| 38 | + commitCount: Object.keys(ledger.commits).length, |
| 39 | + entryCount: Object.keys(ledger.entries).length, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export async function createLocalWorkspace( |
| 44 | + runtime: Pick<RuntimeCore, "createLedger">, |
| 45 | + metadata: Record<string, unknown> = {}, |
| 46 | +): Promise<void> { |
| 47 | + await runtime.createLedger({ |
| 48 | + workspaceMode: "local", |
| 49 | + ...metadata, |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +export async function reopenCurrentWorkspace( |
| 54 | + runtime: Pick<RuntimeCore, "reopen">, |
| 55 | +): Promise<void> { |
| 56 | + await runtime.reopen(); |
| 57 | +} |
| 58 | + |
| 59 | +export async function exportWorkspaceContainer( |
| 60 | + runtime: Pick<RuntimeCore, "exportLedger">, |
| 61 | +): Promise<WorkspaceContainer> { |
| 62 | + return await runtime.exportLedger(); |
| 63 | +} |
| 64 | + |
| 65 | +export async function importWorkspaceContainer( |
| 66 | + runtime: Pick<RuntimeCore, "importLedger">, |
| 67 | + container: WorkspaceContainer, |
| 68 | +): Promise<void> { |
| 69 | + await runtime.importLedger(container); |
| 70 | +} |
| 71 | + |
| 72 | +export function buildWorkspaceExportFile( |
| 73 | + container: WorkspaceContainer, |
| 74 | + prefix = "concord-workspace", |
| 75 | +): { |
| 76 | + filename: string; |
| 77 | + content: string; |
| 78 | +} { |
| 79 | + return { |
| 80 | + filename: `${prefix}-${shortWorkspaceHead(container.head)}.json`, |
| 81 | + content: JSON.stringify(container, null, 2), |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +export function parseWorkspaceImportJson(content: string): WorkspaceContainer { |
| 86 | + return JSON.parse(content) as WorkspaceContainer; |
| 87 | +} |
0 commit comments