xref: /expo/tools/src/versioning/android/utils.ts (revision f194f574)
1*f194f574SWojciech Kozyra/**
2*f194f574SWojciech Kozyra * Find lines matched by startRegex and endRegexp and remove all the
3*f194f574SWojciech Kozyra * lines between them. Note, it will remove entire line matched by startRegex
4*f194f574SWojciech Kozyra * and endRegexp even if pattern does not match entire line
5*f194f574SWojciech Kozyra *
6*f194f574SWojciech Kozyra * This function supports removing multiple sections in one file and
7*f194f574SWojciech Kozyra * handles correctly nested ones
8*f194f574SWojciech Kozyra */
923e91912SWojciech Kozyraexport function deleteLinesBetweenTags(
1023e91912SWojciech Kozyra  startRegex: RegExp | string,
1123e91912SWojciech Kozyra  endRegex: RegExp | string,
1223e91912SWojciech Kozyra  fileContent: string
1323e91912SWojciech Kozyra): string {
1423e91912SWojciech Kozyra  const lines = fileContent.split(/\r?\n/);
1523e91912SWojciech Kozyra  let insideTags = 0;
1623e91912SWojciech Kozyra  const filteredLines = lines.filter((line) => {
1723e91912SWojciech Kozyra    if (line.match(startRegex)) {
1823e91912SWojciech Kozyra      insideTags += 1;
1923e91912SWojciech Kozyra    }
2023e91912SWojciech Kozyra
2123e91912SWojciech Kozyra    const shouldDelete = insideTags > 0;
2223e91912SWojciech Kozyra
2323e91912SWojciech Kozyra    if (line.match(endRegex)) {
2423e91912SWojciech Kozyra      insideTags -= 1;
2523e91912SWojciech Kozyra    }
2623e91912SWojciech Kozyra    return !shouldDelete;
2723e91912SWojciech Kozyra  });
2823e91912SWojciech Kozyra  return filteredLines.join('\n');
2923e91912SWojciech Kozyra}
30