MCPcopy
hub / github.com/upstash/context7 / appendRule

Function appendRule

packages/cli/src/__tests__/setup.test.ts:552–576  ·  view source on GitHub ↗
(filePath: string, existing?: string)

Source from the content-addressed store, hash-verified

550 const ruleContent = "Use ctx7 CLI for docs.\n";
551
552 async function appendRule(filePath: string, existing?: string): Promise<string> {
553 if (existing !== undefined) {
554 await writeFile(filePath, existing, "utf-8");
555 }
556
557 const escapedMarker = marker.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
558 const section = `${marker}\n${ruleContent}${marker}`;
559
560 let content = "";
561 try {
562 content = await readFile(filePath, "utf-8");
563 } catch {}
564
565 if (content.includes(marker)) {
566 const regex = new RegExp(`${escapedMarker}\\n[\\s\\S]*?${escapedMarker}`);
567 await writeFile(filePath, content.replace(regex, section), "utf-8");
568 } else {
569 const separator =
570 content.length > 0 && !content.endsWith("\n") ? "\n\n" : content.length > 0 ? "\n" : "";
571 await mkdir(join(filePath, ".."), { recursive: true });
572 await writeFile(filePath, content + separator + section + "\n", "utf-8");
573 }
574
575 return readFile(filePath, "utf-8");
576 }
577
578 test("creates new file cleanly", async () => {
579 const result = await appendRule(join(tempDir, "AGENTS.md"));

Callers 1

setup.test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected