project-config.ts48 lines · main
1import { readFile, writeFile } from 'node:fs/promises';
2import { resolve } from 'node:path';
3
4/**
5 * Project-local config read from the CWD. The authoritative format per
6 * CLAUDE.md §7.2 is `briven.config.ts`, but Phase 1 reads the simpler
7 * `briven.json` so the CLI can ship before the TS loader exists. Any
8 * `briven.config.ts` takes precedence once its parser lands.
9 */
10export const PROJECT_CONFIG_FILENAME = 'briven.json';
11
12export interface ProjectConfig {
13 name: string;
14 projectId?: string;
15 region?: string;
16}
17
18export async function readProjectConfig(
19 cwd: string = process.cwd(),
20): Promise<ProjectConfig | null> {
21 try {
22 const raw = await readFile(resolve(cwd, PROJECT_CONFIG_FILENAME), 'utf8');
23 const parsed = JSON.parse(raw) as ProjectConfig;
24 if (!parsed || typeof parsed.name !== 'string') return null;
25 return parsed;
26 } catch (err) {
27 if (isNotFound(err)) return null;
28 throw err;
29 }
30}
31
32export async function writeProjectConfig(
33 config: ProjectConfig,
34 cwd: string = process.cwd(),
35): Promise<string> {
36 const path = resolve(cwd, PROJECT_CONFIG_FILENAME);
37 await writeFile(path, `${JSON.stringify(config, null, 2)}\n`);
38 return path;
39}
40
41function isNotFound(err: unknown): boolean {
42 return (
43 typeof err === 'object' &&
44 err !== null &&
45 'code' in err &&
46 (err as { code: string }).code === 'ENOENT'
47 );
48}