Repository: onejs/one
File: packages/vxrn/src/utils/patches.ts (TypeScript)

Code before:

          if (FSExtra.existsSync(nodeModuleDir)) {
            if (typeof version === 'string') {
              const pkgJSON = await FSExtra.readJSON(join(nodeModuleDir, 'package.json'))
              if (!semver.satisfies(pkgJSON.version, version)) {
                return
              }
            }

            for (const file in patch.patchFiles) {
              if (file === 'optimize' || file === 'version') {
                continue
              }

              const filesToApply = file.includes('*') ? globDir(nodeModuleDir, file) : [file]

              await Promise.all(
                filesToApply.map(async (relativePath) => {
                  try {
                    const fullPath = join(nodeModuleDir, relativePath)

                    if (!process.env.VXRN_FORCE_PATCH && isAlreadyPatchedMap.get(fullPath)) {
                      // if the file is already patched, skip it
                      return
                    }

                    let contentsIn = await (async () => {
                      if (pathsBeingPatched.has(fullPath)) {
                        // If the file has been patched during the current run,
                        // we should always start from the already patched file
                        return await FSExtra.readFile(fullPath, 'utf-8')
                      }

                      if (isAlreadyPatchedMap.get(fullPath)) {
                        // If a original file exists, we should start from it
                        // If we can reach here, basically it means
                        // VXRN_FORCE_PATCH is set
                        return await FSExtra.readFile(getOgFilePath(fullPath), 'utf-8')
                      }

                      return await FSExtra.readFile(fullPath, 'utf-8')
                    })()

                    const write = async (contents: string) => {
                      const possibleOrigContents = contentsIn
                      // update contentsIn so the next patch gets the new value if it runs multiple
                      contentsIn = contents
                      const alreadyPatchedPreviouslyInCurrentRun = pathsBeingPatched.has(fullPath)
                      pathsBeingPatched.add(fullPath)
                      await Promise.all(
                        [

Review comment (suggestion):
idk if this may regress my fix because we want to start from the previously patched contents not the original one. My issue you can see replicated in chat app as we have a custom reanimated patch that must run after the patch we apply internally. maybe it works but not sure

Code after:
                return
              }
            }

            for (const file in patch.patchFiles) {
              if (file === 'optimize' || file === 'version') {
                continue
              }

              const filesToApply = file.includes('*') ? globDir(nodeModuleDir, file) : [file]

              await Promise.all(
                filesToApply.map(async (relativePath) => {
                  try {
                    const fullPath = join(nodeModuleDir, relativePath)

                    if (!process.env.VXRN_FORCE_PATCH && getIsAlreadyPatched(fullPath)) {
                      // if the file is already patched, skip it
                      return
                    }

                    let contentsIn = await (async () => {
                      if (pathsBeingPatched.has(fullPath)) {
                        // If the file has been patched during the current run,
                        // we should always start from the already patched file
                        return await FSExtra.readFile(fullPath, 'utf-8')
                      }

                      if (getIsAlreadyPatched(fullPath)) {
                        // If a original file exists, we should start from it
                        // If we can reach here, basically it means
                        // VXRN_FORCE_PATCH is set
                        return await FSExtra.readFile(getOgFilePath(fullPath), 'utf-8')
                      }

                      return await FSExtra.readFile(fullPath, 'utf-8')
                    })()

                    const write = async (contents: string) => {
                      const possibleOrigContents = contentsIn
                      // update contentsIn so the next patch gets the new value if it runs multiple
                      contentsIn = contents
                      const alreadyPatchedPreviouslyInCurrentRun = pathsBeingPatched.has(fullPath)
                      pathsBeingPatched.add(fullPath)
                      await Promise.all(
                        [
                          !alreadyPatchedPreviouslyInCurrentRun /* only write ogfile if this is the first patch, otherwise contentsIn will be already patched content */ &&
                            !getIsAlreadyPatched(
                              fullPath
                            ) /* an ogfile must already be there, no need to write */ &&
                            FSExtra.writeFile(getOgFilePath(fullPath), possibleOrigContents),