* Add (or no-op if present) a `[projects." "] trust_level = "trusted"` * entry to `~/.codex/config.toml`. Uses a minimal append-or-rewrite strategy * — we don't bring in a TOML library because the section grammar is simple * and we only ever touch one section per workspace. * * If the proje
(cwd: string)
| 386 | * what value it has (the user may have set `read_only` deliberately). |
| 387 | */ |
| 388 | async function ensureTrustedProject(cwd: string): Promise<void> { |
| 389 | const abs = resolve(cwd); |
| 390 | const configPath = join(homedir(), '.codex', 'config.toml'); |
| 391 | |
| 392 | let existing = ''; |
| 393 | try { |
| 394 | existing = await readFile(configPath, 'utf8'); |
| 395 | } catch (err) { |
| 396 | if (!isENOENT(err)) throw err; |
| 397 | await mkdir(dirname(configPath), { recursive: true }); |
| 398 | } |
| 399 | |
| 400 | // Match either single- or triple-bracket [projects."<path>"] headers. |
| 401 | const headerEsc = abs.replace(/[\\"]/g, (c) => `\\${c}`); |
| 402 | const headerRe = new RegExp( |
| 403 | `^\\[projects\\."${headerEsc.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"\\]\\s*$`, |
| 404 | 'm', |
| 405 | ); |
| 406 | if (headerRe.test(existing)) return; // already configured — don't clobber |
| 407 | |
| 408 | const block = `\n[projects."${headerEsc}"]\ntrust_level = "trusted"\n`; |
| 409 | const next = existing.endsWith('\n') || existing.length === 0 ? existing + block : existing + '\n' + block; |
| 410 | await writeFile(configPath, next, 'utf8'); |
| 411 | } |
| 412 | |
| 413 | function isENOENT(err: unknown): boolean { |
| 414 | return typeof err === 'object' && err !== null && (err as { code?: string }).code === 'ENOENT'; |