1{"version":3,"file":"generateCode.js","names":["getGeneratedSectionIndexes","src","tag","contents","split","start","findIndex","line","includes","end","mergeContents","newSrc","anchor","offset","comment","header","createGeneratedHeaderComment","sanitizedTarget","removeGeneratedContents","addLines","didMerge","didClear","removeContents","content","find","toAdd","lines","lineIndex","match","error","Error","code","newLine","splice","join","hashKey","createHash","hash","crypto","update","digest"],"sources":["../../src/utils/generateCode.ts"],"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"],"mappings":";;;;;;;;;;;AAKA;EAAA;;EAAA;IAAA;EAAA;;EAAA;AAAA;;;;AALA;AACA;AACA;AACA;AACA;AAGA,SAASA,0BAAT,CACEC,GADF,EAEEC,GAFF,EAGsD;EACpD,MAAMC,QAAQ,GAAGF,GAAG,CAACG,KAAJ,CAAU,IAAV,CAAjB;EACA,MAAMC,KA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