MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / replaceOrAppendMarkedSection

Function replaceOrAppendMarkedSection

src/installer/targets/shared.ts:142–174  ·  view source on GitHub ↗
(
  filePath: string,
  body: string,
  startMarker: string,
  endMarker: string,
)

Source from the content-addressed store, hash-verified

140 * existing block already matches `body`.
141 */
142export function replaceOrAppendMarkedSection(
143 filePath: string,
144 body: string,
145 startMarker: string,
146 endMarker: string,
147): 'created' | 'updated' | 'appended' | 'unchanged' {
148 if (!fs.existsSync(filePath)) {
149 atomicWriteFileSync(filePath, body + '\n');
150 return 'created';
151 }
152
153 const content = fs.readFileSync(filePath, 'utf-8');
154 const startIdx = content.indexOf(startMarker);
155 const endIdx = content.indexOf(endMarker);
156
157 if (startIdx !== -1 && endIdx > startIdx) {
158 const existingBlock = content.substring(startIdx, endIdx + endMarker.length);
159 if (existingBlock === body) {
160 return 'unchanged';
161 }
162 const before = content.substring(0, startIdx);
163 const after = content.substring(endIdx + endMarker.length);
164 atomicWriteFileSync(filePath, before + body + after);
165 return 'updated';
166 }
167
168 // No markers — append. Preserve existing content with a separating
169 // blank line.
170 const trimmed = content.trimEnd();
171 const sep = trimmed.length > 0 ? '\n\n' : '';
172 atomicWriteFileSync(filePath, trimmed + sep + body + '\n');
173 return 'appended';
174}
175
176/**
177 * Upsert the CodeGraph instructions block into an agent instructions

Callers 1

upsertInstructionsEntryFunction · 0.85

Calls 1

atomicWriteFileSyncFunction · 0.85

Tested by

no test coverage detected