diff --git a/android/autolink.mjs b/android/autolink.mjs index ed4bc4088..6fec35696 100644 --- a/android/autolink.mjs +++ b/android/autolink.mjs @@ -1,13 +1,12 @@ // @ts-check -import * as crypto from "node:crypto"; +import { getCurrentState } from "@rnx-kit/tools-react-native/cache"; +import { loadContext } from "@rnx-kit/tools-react-native/context"; import * as fs from "node:fs"; import * as path from "node:path"; import { - findFile, isMain, readJSONFile, readTextFile, - requireTransitive, writeTextFile, } from "../scripts/helpers.js"; @@ -33,45 +32,6 @@ function ensureDirForFile(p) { fs.mkdirSync(path.dirname(p), { recursive: true, mode: 0o755 }); } -/** - * @param {crypto.Hash} hash - * @param {string[]} files - * @param {string} projectRoot - * @param {"all" | "first-only"} mode - */ -function updateHash(hash, files, projectRoot, mode) { - for (const file of files) { - const p = findFile(file, projectRoot); - if (p) { - hash.update(fs.readFileSync(p)); - if (mode === "first-only") { - break; - } - } - } -} - -/** - * @param {string} projectRoot - * @returns {string} - */ -function getCurrentState(projectRoot) { - const sha2 = crypto.createHash("sha256"); - - const configFiles = ["package.json", "react-native.config.js"]; - updateHash(sha2, configFiles, projectRoot, "all"); - - const lockfiles = [ - "yarn.lock", - "package-lock.json", - "pnpm-lock.yaml", - "bun.lockb", - ]; - updateHash(sha2, lockfiles, projectRoot, "first-only"); - - return sha2.digest("hex"); -} - /** * @param {Config} config * @returns {AndroidDependencies} @@ -132,19 +92,9 @@ function loadConfig(json, projectRoot) { return readJSONFile(json); } - /** @type {import("@react-native-community/cli")} */ - const { loadConfig } = requireTransitive( - ["react-native", "@react-native-community/cli"], - projectRoot - ); - - // The signature of `loadConfig` changed in 14.0.0: - // https://github.com/react-native-community/cli/commit/b787c89edb781bb788576cd615d2974fc81402fc - const argc = loadConfig.length; - // @ts-expect-error TS2345: Argument of type X is not assignable to parameter of type Y - const config = loadConfig(argc === 1 ? { projectRoot } : projectRoot); - + const config = loadContext(projectRoot); const prunedConfig = pruneDependencies(config); + ensureDirForFile(json); writeTextFile(json, JSON.stringify(prunedConfig, undefined, 2) + "\n"); writeTextFile(stateFile, state); diff --git a/package.json b/package.json index 4427f4e48..f064b7598 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ }, "dependencies": { "@rnx-kit/react-native-host": "^0.5.0", + "@rnx-kit/tools-react-native": "^2.0.0", "ajv": "^8.0.0", "cliui": "^8.0.0", "fast-xml-parser": "^4.0.0", diff --git a/yarn.lock b/yarn.lock index a651a9e03..b405e47f5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12380,6 +12380,7 @@ __metadata: "@react-native-community/template": "npm:^0.75.0" "@rnx-kit/eslint-plugin": "npm:^0.8.0" "@rnx-kit/react-native-host": "npm:^0.5.0" + "@rnx-kit/tools-react-native": "npm:^2.0.0" "@rnx-kit/tsconfig": "npm:^2.0.0" "@types/js-yaml": "npm:^4.0.5" "@types/mustache": "npm:^4.0.0"