Skip to content

Commit 70b945b

Browse files
committed
feat(sdk): append project references to AGENTS.md only when missing
1 parent d415db8 commit 70b945b

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

packages/sdk/src/agents-md.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import { readFileSync, writeFileSync, existsSync } from "node:fs";
88
import { join } from "node:path";
99

10+
const referencesSectionRegex = /^## Project References\n(?:.*\n)*?(?=^## |$)/m;
11+
1012
export interface InstalledReference {
1113
/** Dependency name */
1214
dependency: string;
@@ -44,12 +46,11 @@ export function appendReferencesSection(filePath: string, references: InstalledR
4446
const content = existsSync(filePath) ? readFileSync(filePath, "utf-8") : "";
4547
const referencesMarkdown = generateReferencesTable(references);
4648

47-
const sectionRegex = /^## Project References\n(?:.*\n)*?(?=^## |$)/m;
48-
const match = content.match(sectionRegex);
49+
const match = content.match(referencesSectionRegex);
4950

5051
let updatedContent: string;
5152
if (match) {
52-
updatedContent = content.replace(sectionRegex, referencesMarkdown);
53+
updatedContent = content.replace(referencesSectionRegex, referencesMarkdown);
5354
} else {
5455
updatedContent = content.trim() + "\n\n" + referencesMarkdown;
5556
}
@@ -58,19 +59,29 @@ export function appendReferencesSection(filePath: string, references: InstalledR
5859
}
5960

6061
/**
61-
* Update AGENTS.md and agent-specific files with project references.
62-
* Creates files if they don't exist.
62+
* Update AGENTS.md with project references if the section is missing.
6363
*
6464
* @param projectRoot - Project root directory
6565
* @param references - Array of installed references to document
6666
*/
6767
export function updateAgentFiles(projectRoot: string, references: InstalledReference[]): void {
6868
const agentsMdPath = join(projectRoot, "AGENTS.md");
69-
const claudeMdPath = join(projectRoot, "CLAUDE.md");
69+
const referencesMarkdown = generateReferencesTable(references);
7070

71-
appendReferencesSection(agentsMdPath, references);
71+
if (!existsSync(agentsMdPath)) {
72+
writeFileSync(agentsMdPath, referencesMarkdown, "utf-8");
73+
return;
74+
}
7275

73-
if (existsSync(claudeMdPath)) {
74-
appendReferencesSection(claudeMdPath, references);
76+
const content = readFileSync(agentsMdPath, "utf-8");
77+
if (referencesSectionRegex.test(content)) {
78+
return;
7579
}
80+
81+
const separator = content.endsWith("\n\n") ? "" : content.endsWith("\n") ? "\n" : "\n\n";
82+
const updatedContent =
83+
content.trim().length === 0
84+
? referencesMarkdown
85+
: `${content}${separator}${referencesMarkdown}`;
86+
writeFileSync(agentsMdPath, updatedContent, "utf-8");
7687
}

0 commit comments

Comments
 (0)