MCPcopy
hub / github.com/Fission-AI/OpenSpec / updateFileWithMarkers

Method updateFileWithMarkers

src/utils/file-system.ts:240–276  ·  view source on GitHub ↗
(
    filePath: string,
    content: string,
    startMarker: string,
    endMarker: string
  )

Source from the content-addressed store, hash-verified

238 }
239
240 static async updateFileWithMarkers(
241 filePath: string,
242 content: string,
243 startMarker: string,
244 endMarker: string
245 ): Promise<void> {
246 let existingContent = '';
247
248 if (await this.fileExists(filePath)) {
249 existingContent = await this.readFile(filePath);
250
251 const startIndex = findMarkerIndex(existingContent, startMarker);
252 const endIndex = startIndex !== -1
253 ? findMarkerIndex(existingContent, endMarker, startIndex + startMarker.length)
254 : findMarkerIndex(existingContent, endMarker);
255
256 if (startIndex !== -1 && endIndex !== -1) {
257 if (endIndex < startIndex) {
258 throw new Error(
259 `Invalid marker state in ${filePath}. End marker appears before start marker.`
260 );
261 }
262
263 const before = existingContent.substring(0, startIndex);
264 const after = existingContent.substring(endIndex + endMarker.length);
265 existingContent = before + startMarker + '\n' + content + '\n' + endMarker + after;
266 } else if (startIndex === -1 && endIndex === -1) {
267 existingContent = startMarker + '\n' + content + '\n' + endMarker + '\n\n' + existingContent;
268 } else {
269 throw new Error(`Invalid marker state in ${filePath}. Found start: ${startIndex !== -1}, Found end: ${endIndex !== -1}`);
270 }
271 } else {
272 existingContent = startMarker + '\n' + content + '\n' + endMarker;
273 }
274
275 await this.writeFile(filePath, existingContent);
276 }
277
278 static async ensureWritePermissions(dirPath: string): Promise<boolean> {
279 try {

Callers 3

configureBashrcMethod · 0.80
configureZshrcMethod · 0.80

Calls 4

fileExistsMethod · 0.95
readFileMethod · 0.95
writeFileMethod · 0.95
findMarkerIndexFunction · 0.85

Tested by

no test coverage detected