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

changeset.ts

32 lines1.1 KB

1import { readFile, readdir, writeFile } from 'node:fs/promises'
2import { resolve } from 'node:path'
3​
4/**
5 * Create or update the `sync-models` changeset: a patch for each changed
6 * package. An existing one keeps its packages, so each sync step can add its
7 * own.
8 */
9export async function writeSyncChangeset(
10 root: string,
11 changedPackages: ReadonlySet<string>,
12): Promise<void> {
13 const dir = resolve(root, '.changeset')
14 const existing = (await readdir(dir)).find(
15 (file) => file.startsWith('sync-models') && file.endsWith('.md'),
16 )
17 const path = resolve(dir, existing ?? 'sync-models.md')
18 const packages = new Set(changedPackages)
19 if (existing)
20 for (const match of (await readFile(path, 'utf-8')).matchAll(
21 /'([^']+)':\s*patch/g,
22 ))
23 packages.add(match[1]!)
24 const lines = [...packages].sort().map((pkg) => `'${pkg}': patch`)
25 await writeFile(
26 path,
27 `---\n${lines.join('\n')}\n---\n\nUpdate model metadata\n`,
28 'utf-8',
29 )
30 console.log(`\nChangeset ${existing ? 'updated' : 'created'}: ${path}`)
31 console.log(` Packages: ${[...packages].sort().join(', ')}`)