schema-pull.ts42 lines · main
| 1 | import { mkdir, writeFile } from 'node:fs/promises'; |
| 2 | import { join } from 'node:path'; |
| 3 | |
| 4 | import { apiCall } from './api-client.js'; |
| 5 | |
| 6 | export interface PullOpts { |
| 7 | apiOrigin: string; |
| 8 | bearer: string; |
| 9 | projectId: string; |
| 10 | cwd: string; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Pulls the project's live schema from the api and writes it to disk as |
| 15 | * briven/schema.ts in the caller's cwd. Also seeds briven/functions/ |
| 16 | * with a README pointing at the (future) `briven pull functions` command. |
| 17 | */ |
| 18 | export async function pullSchemaToDisk(opts: PullOpts): Promise<void> { |
| 19 | const resp = await apiCall<{ schemaTs: string }>( |
| 20 | `/v1/projects/${opts.projectId}/studio/schema-export`, |
| 21 | { |
| 22 | apiOrigin: opts.apiOrigin, |
| 23 | bearer: opts.bearer, |
| 24 | }, |
| 25 | ); |
| 26 | const dir = join(opts.cwd, 'briven'); |
| 27 | await mkdir(dir, { recursive: true }); |
| 28 | await writeFile(join(dir, 'schema.ts'), resp.schemaTs); |
| 29 | const fnDir = join(dir, 'functions'); |
| 30 | await mkdir(fnDir, { recursive: true }); |
| 31 | await writeFile( |
| 32 | join(fnDir, 'README.md'), |
| 33 | [ |
| 34 | '# your functions live on briven', |
| 35 | '', |
| 36 | 'this directory is intentionally empty. your existing functions still serve traffic.', |
| 37 | 'a future `briven pull functions` will recover their source. for now, write new ones', |
| 38 | 'here — every .ts file in this directory becomes an invokable function.', |
| 39 | '', |
| 40 | ].join('\n'), |
| 41 | ); |
| 42 | } |