|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Disk layout helpers — see ADR-0008 §10 PR-4 / packages/metadata-fs README. |
| 5 | + * |
| 6 | + * <root>/<type>/<name>.json — canonical body |
| 7 | + * <root>/.objectstack/.log/<branch>.jsonl — append-only change log |
| 8 | + */ |
| 9 | + |
| 10 | +import path from 'node:path'; |
| 11 | +import type { MetadataType } from '@objectstack/metadata-core'; |
| 12 | + |
| 13 | +export interface FsLayout { |
| 14 | + /** Absolute path to the metadata root. */ |
| 15 | + root: string; |
| 16 | + /** Branch name (e.g. "main"). */ |
| 17 | + branch: string; |
| 18 | +} |
| 19 | + |
| 20 | +export function itemPath(layout: FsLayout, type: MetadataType, name: string): string { |
| 21 | + return path.join(layout.root, type, `${name}.json`); |
| 22 | +} |
| 23 | + |
| 24 | +export function typeDir(layout: FsLayout, type: MetadataType): string { |
| 25 | + return path.join(layout.root, type); |
| 26 | +} |
| 27 | + |
| 28 | +export function logDir(layout: FsLayout): string { |
| 29 | + return path.join(layout.root, '.objectstack', '.log'); |
| 30 | +} |
| 31 | + |
| 32 | +export function logFile(layout: FsLayout): string { |
| 33 | + return path.join(logDir(layout), `${layout.branch}.jsonl`); |
| 34 | +} |
| 35 | + |
| 36 | +/** Parse a path like ".../view/case_grid.json" into {type, name}. */ |
| 37 | +export function parseItemPath( |
| 38 | + layout: FsLayout, |
| 39 | + absPath: string, |
| 40 | +): { type: string; name: string } | null { |
| 41 | + const rel = path.relative(layout.root, absPath); |
| 42 | + if (rel.startsWith('..') || rel.startsWith('.objectstack')) return null; |
| 43 | + const segments = rel.split(path.sep); |
| 44 | + if (segments.length !== 2) return null; |
| 45 | + const type = segments[0]!; |
| 46 | + const file = segments[1]!; |
| 47 | + if (!file.endsWith('.json')) return null; |
| 48 | + const name = file.slice(0, -'.json'.length); |
| 49 | + return { type, name }; |
| 50 | +} |
0 commit comments