|
| 1 | +import cp from "node:child_process"; |
| 2 | + |
| 3 | +import { assertFixable, chalk } from "@react-native-node-api/cli-utils"; |
| 4 | +import * as z from "zod"; |
| 5 | + |
| 6 | +import type { TargetName } from "./targets.js"; |
| 7 | + |
| 8 | +const CargoResolvedMetadata = z.object({ |
| 9 | + packages: z.array(z.object({ id: z.string(), name: z.string() })), |
| 10 | + resolve: z |
| 11 | + .object({ |
| 12 | + nodes: z.array( |
| 13 | + z.object({ id: z.string(), features: z.array(z.string()).optional() }), |
| 14 | + ), |
| 15 | + }) |
| 16 | + .nullable(), |
| 17 | +}); |
| 18 | + |
| 19 | +export type CargoResolvedMetadata = z.infer<typeof CargoResolvedMetadata>; |
| 20 | + |
| 21 | +export function getResolvedFeatures( |
| 22 | + metadata: CargoResolvedMetadata, |
| 23 | + packageName: string, |
| 24 | +): Set<string> { |
| 25 | + const ids = new Set( |
| 26 | + metadata.packages |
| 27 | + .filter((pkg) => pkg.name === packageName) |
| 28 | + .map((pkg) => pkg.id), |
| 29 | + ); |
| 30 | + const nodes = metadata.resolve?.nodes ?? []; |
| 31 | + return new Set( |
| 32 | + nodes |
| 33 | + .filter((node) => ids.has(node.id)) |
| 34 | + .flatMap((node) => node.features ?? []), |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +function readResolvedMetadata( |
| 39 | + cwd: string, |
| 40 | + target: TargetName, |
| 41 | +): CargoResolvedMetadata { |
| 42 | + const output = cp.execFileSync( |
| 43 | + "cargo", |
| 44 | + ["metadata", "--format-version", "1", "--filter-platform", target], |
| 45 | + { cwd, encoding: "utf-8" }, |
| 46 | + ); |
| 47 | + return CargoResolvedMetadata.parse(JSON.parse(output)); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * The highest Node-API version implemented by the host. |
| 52 | + */ |
| 53 | +export const MAX_NODE_API_VERSION = 10; |
| 54 | + |
| 55 | +export function getNodeApiVersion(features: Iterable<string>): number { |
| 56 | + let result = 0; |
| 57 | + for (const feature of features) { |
| 58 | + const match = /^napi(\d+)$/.exec(feature); |
| 59 | + if (match) { |
| 60 | + result = Math.max(result, parseInt(match[1], 10)); |
| 61 | + } |
| 62 | + } |
| 63 | + return result; |
| 64 | +} |
| 65 | + |
| 66 | +export function assertNapiFeatures(cwd: string, targets: Iterable<TargetName>) { |
| 67 | + for (const target of targets) { |
| 68 | + const features = getResolvedFeatures( |
| 69 | + readResolvedMetadata(cwd, target), |
| 70 | + "napi-sys", |
| 71 | + ); |
| 72 | + const treeCommand = chalk.blue( |
| 73 | + `cargo tree -e features -i napi-sys --target ${target}`, |
| 74 | + ); |
| 75 | + const skipFlag = chalk.blue("--skip-feature-check"); |
| 76 | + assertFixable( |
| 77 | + !features.has("dyn-symbols"), |
| 78 | + `The "dyn-symbols" feature of napi-sys is enabled when building for ${target}, which loads Node-API functions at runtime instead of linking them against weak-node-api`, |
| 79 | + { |
| 80 | + instructions: [ |
| 81 | + `Set ${chalk.blue("default-features = false")} on the napi dependency in Cargo.toml and enable the "napiN" features you need explicitly.`, |
| 82 | + `Run ${treeCommand} to see which dependency enables it, or pass ${skipFlag} to build anyway.`, |
| 83 | + ].join("\n"), |
| 84 | + }, |
| 85 | + ); |
| 86 | + const version = getNodeApiVersion(features); |
| 87 | + assertFixable( |
| 88 | + version <= MAX_NODE_API_VERSION, |
| 89 | + `The "napi${version}" feature of napi-sys is enabled when building for ${target}, but React Native Node-API supports Node-API versions up to ${MAX_NODE_API_VERSION}`, |
| 90 | + { |
| 91 | + instructions: [ |
| 92 | + `Enable at most the "napi${MAX_NODE_API_VERSION}" feature on the napi dependency in Cargo.toml.`, |
| 93 | + `Run ${treeCommand} to see which dependency enables it, or pass ${skipFlag} to build anyway.`, |
| 94 | + ].join("\n"), |
| 95 | + }, |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
0 commit comments