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A0B,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACtE,IAAI,KAAK,GAAG,CAAC,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC,IAAI,KAAK,GAAG,GAAG,EAAE;QACzC,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC;QACxC,qGAAqG;QACrG,sHAAsH;QACtH,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC5B;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AATD,0DASC;AAED,SAAgB,4BAA4B,CAC1C,QAAgB,EAChB,GAAW,EACX,OAAe;IAEf,MAAM,OAAO,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;IAErC,qGAAqG;IACrG,OAAO,GAAG,OAAO,qBAAqB,GAAG,oCAAoC,OAAO,EAAE,CAAC;AACzF,CAAC;AATD,oEASC;AAED,SAAgB,UAAU,CAAC,GAAW;IACpC,0DAA0D;IAC1D,MAAM,IAAI,GAAG,gBAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACjE,OAAO,QAAQ,IAAI,EAAE,CAAC;AACxB,CAAC;AAJD,gCAIC","sourcesContent":["/**\n * Get line indexes for the generated section of a file.\n *\n * @param src\n */\nimport crypto from 'crypto';\n\nfunction getGeneratedSectionIndexes(\n src: string,\n tag: string\n): { contents: string[]; start: number; end: number } {\n const contents = src.split('\\n');\n const start = contents.findIndex(line => line.includes(`@generated begin ${tag}`));\n const end = contents.findIndex(line => line.includes(`@generated end ${tag}`));\n\n return { contents, start, end };\n}\n\nexport type MergeResults = {\n contents: string;\n didClear: boolean;\n didMerge: boolean;\n};\n\n/**\n * Merge the contents of two files together and add a generated header.\n *\n * @param src contents of the original file\n * @param newSrc new contents to merge into the original file\n * @param identifier used to update and remove merges\n * @param anchor regex to where the merge should begin\n * @param offset line offset to start merging at (<1 for behind the anchor)\n * @param comment comment style `//` or `#`\n */\nexport function mergeContents({\n src,\n newSrc,\n tag,\n anchor,\n offset,\n comment,\n}: {\n src: string;\n newSrc: string;\n tag: string;\n anchor: string | RegExp;\n offset: number;\n comment: string;\n}): MergeResults {\n const header = createGeneratedHeaderComment(newSrc, tag, comment);\n if (!src.includes(header)) {\n // Ensure the old generated contents are removed.\n const sanitizedTarget = removeGeneratedContents(src, tag);\n return {\n contents: addLines(sanitizedTarget ?? src, anchor, offset, [\n header,\n ...newSrc.split('\\n'),\n `${comment} @generated end ${tag}`,\n ]),\n didMerge: true,\n didClear: !!sanitizedTarget,\n };\n }\n return { contents: src, didClear: false, didMerge: false };\n}\n\nexport function removeContents({ src, tag }: { src: string; tag: string }): MergeResults {\n // Ensure the old generated contents are removed.\n const sanitizedTarget = removeGeneratedContents(src, tag);\n return {\n contents: sanitizedTarget ?? src,\n didMerge: false,\n didClear: !!sanitizedTarget,\n };\n}\n\nfunction addLines(content: string, find: string | RegExp, offset: number, toAdd: string[]) {\n const lines = content.split('\\n');\n\n let lineIndex = lines.findIndex(line => line.match(find));\n if (lineIndex < 0) {\n const error = new Error(`Failed to match \"${find}\" in contents:\\n${content}`);\n // @ts-ignore\n error.code = 'ERR_NO_MATCH';\n throw error;\n }\n for (const newLine of toAdd) {\n lines.splice(lineIndex + offset, 0, newLine);\n lineIndex++;\n }\n\n return lines.join('\\n');\n}\n\n/**\n * Removes the generated section from a file, returns null when nothing can be removed.\n * This sways heavily towards not removing lines unless it's certain that modifications were not made manually.\n *\n * @param src\n */\nexport function removeGeneratedContents(src: string, tag: string): string | null {\n const { contents, start, end } = getGeneratedSectionIndexes(src, tag);\n if (start > -1 && end > -1 && start < end) {\n contents.splice(start, end - start + 1);\n // TODO: We could in theory check that the contents we're removing match the hash used in the header,\n // this would ensure that we don't accidentally remove lines that someone added or removed from the generated section.\n return contents.join('\\n');\n }\n return null;\n}\n\nexport function createGeneratedHeaderComment(\n contents: string,\n tag: string,\n comment: string\n): string {\n const hashKey = createHash(contents);\n\n // Everything after the `${tag} ` is unversioned and can be freely modified without breaking changes.\n return `${comment} @generated begin ${tag} - expo prebuild (DO NOT MODIFY) ${hashKey}`;\n}\n\nexport function createHash(src: string): string {\n // this doesn't need to be secure, the shorter the better.\n const hash = crypto.createHash('sha1').update(src).digest('hex');\n return `sync-${hash}`;\n}\n"]}