(configPath: string)
| 169 | } |
| 170 | |
| 171 | function updateCodexConfig(configPath: string): void { |
| 172 | const command = process.execPath; |
| 173 | const args = [fileURLToPath(new URL("../index.js", import.meta.url)), "serve"]; |
| 174 | |
| 175 | const sharedcontextSection = [ |
| 176 | "[mcp_servers.sharedcontext]", |
| 177 | `command = ${toTomlString(command)}`, |
| 178 | `args = [${args.map((arg) => toTomlString(arg)).join(", ")}]`, |
| 179 | ].join("\n"); |
| 180 | |
| 181 | mkdirSync(dirname(configPath), { recursive: true }); |
| 182 | |
| 183 | const existing = existsSync(configPath) ? readFileSync(configPath, "utf-8") : ""; |
| 184 | const normalized = existing.replace(/\r\n/g, "\n").trimEnd(); |
| 185 | const sectionRegex = /^\[mcp_servers\.sharedcontext\]\n[\s\S]*?(?=^\[[^\]]+\]\n?|$)/m; |
| 186 | |
| 187 | const nextContent = sectionRegex.test(normalized) |
| 188 | ? normalized.replace(sectionRegex, sharedcontextSection) |
| 189 | : normalized === "" |
| 190 | ? sharedcontextSection |
| 191 | : `${normalized}\n\n${sharedcontextSection}`; |
| 192 | |
| 193 | writeFileSync(configPath, `${nextContent}\n`, "utf-8"); |
| 194 | } |
| 195 | |
| 196 | function toTomlString(value: string): string { |
| 197 | const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"'); |
no test coverage detected