()
| 142 | } |
| 143 | |
| 144 | function writeMcpEntry(): WriteResult['files'][number] { |
| 145 | const file = tomlConfigPath(); |
| 146 | const dir = path.dirname(file); |
| 147 | if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); |
| 148 | |
| 149 | const block = buildCodegraphBlock(); |
| 150 | // Single read — `existing === ''` derives both "is the file empty |
| 151 | // or absent" and "what was its content," avoiding a TOCTOU window |
| 152 | // between two `fs.existsSync` calls. |
| 153 | const existing = fs.existsSync(file) ? fs.readFileSync(file, 'utf-8') : ''; |
| 154 | const created = existing.length === 0; |
| 155 | const { content: nextContent, action } = upsertTomlTable(existing, TOML_HEADER, block); |
| 156 | |
| 157 | if (action === 'unchanged') { |
| 158 | return { path: file, action: 'unchanged' }; |
| 159 | } |
| 160 | atomicWriteFileSync(file, nextContent); |
| 161 | return { path: file, action: created ? 'created' : 'updated' }; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Strip the marker-delimited CodeGraph block from `~/.codex/AGENTS.md` |
no test coverage detected