( filePath: string, startMarker: string, endMarker: string, )
| 203 | * the markers weren't present, `kept` when the file didn't exist. |
| 204 | */ |
| 205 | export function removeMarkedSection( |
| 206 | filePath: string, |
| 207 | startMarker: string, |
| 208 | endMarker: string, |
| 209 | ): 'removed' | 'not-found' | 'kept' { |
| 210 | if (!fs.existsSync(filePath)) return 'kept'; |
| 211 | |
| 212 | let content: string; |
| 213 | try { |
| 214 | content = fs.readFileSync(filePath, 'utf-8'); |
| 215 | } catch { |
| 216 | return 'kept'; |
| 217 | } |
| 218 | |
| 219 | const startIdx = content.indexOf(startMarker); |
| 220 | const endIdx = content.indexOf(endMarker); |
| 221 | if (startIdx === -1 || endIdx <= startIdx) return 'not-found'; |
| 222 | |
| 223 | const before = content.substring(0, startIdx).trimEnd(); |
| 224 | const after = content.substring(endIdx + endMarker.length).trimStart(); |
| 225 | const joined = before + (before && after ? '\n\n' : '') + after; |
| 226 | |
| 227 | if (joined.trim() === '') { |
| 228 | try { fs.unlinkSync(filePath); } catch { /* ignore */ } |
| 229 | } else { |
| 230 | atomicWriteFileSync(filePath, joined.trim() + '\n'); |
| 231 | } |
| 232 | return 'removed'; |
| 233 | } |
no test coverage detected