Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

manifest.ts

87 lines2.7 KB

1/**
2 * The lockfile (PLAN.md task 12.3): `.manifest.json` in the out dir records
3 * what was pulled (selection), from where, and each target's schema content
4 * hash + ETag — so dev boots make ~zero requests, production builds verify
5 * offline, and `update` re-fetches conditionally.
6 */
7import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
8import { dirname, join } from 'node:path'
9​
10import type { OptionalStyle } from './pull.ts'
11import type { ResolvedTarget } from './selection.ts'
12import { KIND_LABEL } from './selection.ts'
13​
14export const MANIFEST_FILENAME = '.manifest.json'
15​
16export interface ManifestEntry {
17 /** File path relative to the out dir. */
18 path: string
19 /** Schema content hash (the JSON ETag; types ETags embed it). */
20 contentHash: string
21 /** Verbatim response ETag, replayed as If-None-Match. */
22 etag: string
24​
25export interface Manifest {
26 version: 1
27 baseUrl: string
28 selections: Array<string>
29 types: boolean
30 optionalStyle: OptionalStyle
31 /** Unix epoch seconds of the last pull that changed anything. */
32 fetchedAt: number
33 /** Keyed by target key (`provider/activity/endpointId#label`). */
34 entries: Record<string, ManifestEntry>
36​
37export function targetKey(target: ResolvedTarget): string {
38 return `${target.provider}/${target.activity}/${target.endpointId}#${KIND_LABEL[target.kind]}`
40​
41/** `anthropic/v1-messages.request.ts` — one file per endpoint+kind, slashes
42 * in the endpoint id flattened so the layout stays one dir per provider. */
43export function fileRelPath(target: ResolvedTarget): string {
44 const flat = target.endpointId
45 .replaceAll('/', '-')
46 .replace(/[^A-Za-z0-9._{}-]/g, '-')
47 return join(target.provider, `${flat}.${KIND_LABEL[target.kind]}.ts`)
49​
50export async function readManifest(outDir: string): Promise<Manifest | null> {
51 let text: string
52 try {
53 text = await readFile(join(outDir, MANIFEST_FILENAME), 'utf8')
54 } catch {
55 return null
56 }
57 const parsed = JSON.parse(text) as Omit<Manifest, 'version'> & {
58 version: number
59 }
60 if (parsed.version !== 1) {
61 throw new Error(
62 `Unsupported manifest version in ${join(outDir, MANIFEST_FILENAME)} — regenerate with \`modelschemas pull\`.`,
63 )
64 }
65 return { ...parsed, version: 1 }
67​
68export async function writeManifest(
69 outDir: string,
70 manifest: Manifest,
71): Promise<void> {
72 await atomicWrite(
73 join(outDir, MANIFEST_FILENAME),
74 `${JSON.stringify(manifest, null, 2)}\n`,
75 )
77​
78/** Write via a sibling temp file + rename so builds never see half a file. */
79export async function atomicWrite(
80 path: string,
81 content: string,
82): Promise<void> {
83 await mkdir(dirname(path), { recursive: true })
84 const tmp = `${path}.tmp-${process.pid.toString(36)}`
85 await writeFile(tmp, content, 'utf8')
86 await rename(tmp, path)