1/** 2 * Find lines matched by startRegex and endRegexp and remove all the 3 * lines between them. Note, it will remove entire line matched by startRegex 4 * and endRegexp even if pattern does not match entire line 5 * 6 * This function supports removing multiple sections in one file and 7 * handles correctly nested ones 8 */ 9export function deleteLinesBetweenTags( 10 startRegex: RegExp | string, 11 endRegex: RegExp | string, 12 fileContent: string 13): string { 14 const lines = fileContent.split(/\r?\n/); 15 let insideTags = 0; 16 const filteredLines = lines.filter((line) => { 17 if (line.match(startRegex)) { 18 insideTags += 1; 19 } 20 21 const shouldDelete = insideTags > 0; 22 23 if (line.match(endRegex)) { 24 insideTags -= 1; 25 } 26 return !shouldDelete; 27 }); 28 return filteredLines.join('\n'); 29} 30