|
| 1 | +import { chalk } from "@react-native-node-api/cli-utils"; |
| 2 | +import semver from "semver"; |
| 3 | +import { parse as parseToml } from "smol-toml"; |
| 4 | + |
| 5 | +import { MAX_NODE_API_VERSION, getNodeApiVersion } from "./features.js"; |
| 6 | +import { isRecord } from "./json.js"; |
| 7 | + |
| 8 | +export type CrateVersions = { |
| 9 | + napi: string; |
| 10 | + "napi-derive": string; |
| 11 | + "napi-build": string; |
| 12 | +}; |
| 13 | + |
| 14 | +const CRATES = [ |
| 15 | + { crate: "napi", table: "dependencies" }, |
| 16 | + { crate: "napi-derive", table: "dependencies" }, |
| 17 | + { crate: "napi-build", table: "build-dependencies" }, |
| 18 | +] satisfies { crate: keyof CrateVersions; table: string }[]; |
| 19 | + |
| 20 | +/** |
| 21 | + * Cargo separates comparators with commas, which semver expects as spaces. |
| 22 | + */ |
| 23 | +function toSemverRange(requirement: string) { |
| 24 | + return semver.validRange(requirement.replaceAll(",", " ")); |
| 25 | +} |
| 26 | + |
| 27 | +export function isCrateVersion(requirement: string) { |
| 28 | + return toSemverRange(requirement) !== null; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Whether a declared version requirement allows only versions below the wanted |
| 33 | + * one. Requirements which aren't versions at all are never considered outdated. |
| 34 | + */ |
| 35 | +export function isOutdated(declared: string, wanted: string) { |
| 36 | + const declaredRange = toSemverRange(declared); |
| 37 | + const wantedRange = toSemverRange(wanted); |
| 38 | + if (!declaredRange || !wantedRange || declaredRange === "*") { |
| 39 | + return false; |
| 40 | + } |
| 41 | + return semver.gt( |
| 42 | + semver.minVersion(wantedRange) ?? "0.0.0", |
| 43 | + semver.minVersion(declaredRange) ?? "0.0.0", |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Replace a version in the text of a manifest, as serializing a parsed manifest |
| 49 | + * would lose its comments and formatting. |
| 50 | + */ |
| 51 | +function replaceCrateVersion({ |
| 52 | + contents, |
| 53 | + table, |
| 54 | + crate, |
| 55 | + version, |
| 56 | +}: { |
| 57 | + contents: string; |
| 58 | + table: string; |
| 59 | + crate: string; |
| 60 | + version: string; |
| 61 | +}) { |
| 62 | + let section = ""; |
| 63 | + const lines = contents.split("\n").map((line) => { |
| 64 | + const header = line.match(/^\s*\[([^[\]]+)\]/); |
| 65 | + if (header) { |
| 66 | + section = header[1].trim(); |
| 67 | + return line; |
| 68 | + } |
| 69 | + const inTable = section === table && line.match(`^\\s*"?${crate}"?\\s*=`); |
| 70 | + const inCrateTable = |
| 71 | + section === `${table}.${crate}` && line.match(/^\s*version\s*=/); |
| 72 | + if (!inTable && !inCrateTable) { |
| 73 | + return line; |
| 74 | + } |
| 75 | + const pattern = inTable |
| 76 | + ? /(=\s*(?:\{[^}]*?\bversion\s*=\s*)?)(["'])[^"']*\2/ |
| 77 | + : /(=\s*)(["'])[^"']*\2/; |
| 78 | + return line.replace(pattern, `$1$2${version}$2`); |
| 79 | + }); |
| 80 | + return lines.join("\n"); |
| 81 | +} |
| 82 | + |
| 83 | +function getCrateDeclaration( |
| 84 | + manifest: Record<string, unknown>, |
| 85 | + table: string, |
| 86 | + crate: string, |
| 87 | +) { |
| 88 | + const dependencies = manifest[table]; |
| 89 | + return isRecord(dependencies) ? dependencies[crate] : undefined; |
| 90 | +} |
| 91 | + |
| 92 | +function getStringArray(value: unknown) { |
| 93 | + return Array.isArray(value) |
| 94 | + ? value.filter((item) => typeof item === "string") |
| 95 | + : []; |
| 96 | +} |
| 97 | + |
| 98 | +function inspectFeatures(dependencies: Record<string, unknown>) { |
| 99 | + const result: string[] = []; |
| 100 | + const napi = dependencies["napi"]; |
| 101 | + if (isRecord(napi) || typeof napi === "string") { |
| 102 | + if (!isRecord(napi) || napi["default-features"] !== false) { |
| 103 | + result.push( |
| 104 | + `Cargo.toml doesn't declare ${chalk.blue("default-features = false")} on ${chalk.blue("napi")}, which ferric build rejects`, |
| 105 | + ); |
| 106 | + } |
| 107 | + const features = isRecord(napi) ? getStringArray(napi["features"]) : []; |
| 108 | + if (getNodeApiVersion(features) > MAX_NODE_API_VERSION) { |
| 109 | + result.push( |
| 110 | + `Cargo.toml enables a ${chalk.blue("napi")} feature above the Node-API version ${MAX_NODE_API_VERSION} supported by the host`, |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + const napiSys = dependencies["napi-sys"]; |
| 115 | + if (isRecord(napiSys) || typeof napiSys === "string") { |
| 116 | + const version = isRecord(napiSys) ? napiSys["version"] : napiSys; |
| 117 | + if (typeof version === "string" && version.startsWith("=")) { |
| 118 | + result.push( |
| 119 | + `Cargo.toml pins ${chalk.blue(`napi-sys ${version}`)}, which is likely to conflict with newer ${chalk.blue("napi")} versions: remove the pin`, |
| 120 | + ); |
| 121 | + } |
| 122 | + const features = isRecord(napiSys) |
| 123 | + ? getStringArray(napiSys["features"]) |
| 124 | + : []; |
| 125 | + if (features.includes("dyn-symbols")) { |
| 126 | + result.push( |
| 127 | + `Cargo.toml enables the ${chalk.blue("dyn-symbols")} feature of ${chalk.blue("napi-sys")}, which ferric build rejects`, |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | + return result; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Bump outdated crates of an existing manifest in place and report anything else |
| 136 | + * it needs to build into a Node-API module. Everything but those versions is |
| 137 | + * left to the developer. |
| 138 | + */ |
| 139 | +export function updateCargoManifest( |
| 140 | + contents: string, |
| 141 | + crateVersions: CrateVersions, |
| 142 | +) { |
| 143 | + let manifest: unknown; |
| 144 | + try { |
| 145 | + manifest = parseToml(contents); |
| 146 | + } catch (error) { |
| 147 | + return { |
| 148 | + contents, |
| 149 | + notices: [ |
| 150 | + `Cargo.toml could not be parsed: ${error instanceof Error ? error.message : String(error)}`, |
| 151 | + ], |
| 152 | + }; |
| 153 | + } |
| 154 | + if (!isRecord(manifest)) { |
| 155 | + return { contents, notices: ["Cargo.toml doesn't declare a package"] }; |
| 156 | + } |
| 157 | + const parsed = manifest; |
| 158 | + const notices: string[] = []; |
| 159 | + const lib = parsed["lib"]; |
| 160 | + const crateType = isRecord(lib) ? lib["crate-type"] : undefined; |
| 161 | + if (!Array.isArray(crateType) || !crateType.includes("cdylib")) { |
| 162 | + notices.push( |
| 163 | + `Cargo.toml doesn't declare ${chalk.blue('crate-type = ["cdylib"]')} in its ${chalk.blue("[lib]")} section, which Node-API modules are built as`, |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + let updated = contents; |
| 168 | + for (const { crate, table } of CRATES) { |
| 169 | + const wanted = crateVersions[crate]; |
| 170 | + const declaration = getCrateDeclaration(parsed, table, crate); |
| 171 | + if (declaration === undefined) { |
| 172 | + notices.push( |
| 173 | + `Cargo.toml is missing ${chalk.blue(crate)} from its ${chalk.blue(`[${table}]`)}`, |
| 174 | + ); |
| 175 | + continue; |
| 176 | + } |
| 177 | + const declared = isRecord(declaration) |
| 178 | + ? declaration["version"] |
| 179 | + : declaration; |
| 180 | + if (typeof declared !== "string") { |
| 181 | + notices.push( |
| 182 | + `Cargo.toml doesn't declare ${chalk.blue(crate)} with a version: make sure it resolves to ${chalk.blue(wanted)} or newer`, |
| 183 | + ); |
| 184 | + } else if (isOutdated(declared, wanted)) { |
| 185 | + const replaced = replaceCrateVersion({ |
| 186 | + contents: updated, |
| 187 | + table, |
| 188 | + crate, |
| 189 | + version: wanted, |
| 190 | + }); |
| 191 | + const result: unknown = parseToml(replaced); |
| 192 | + const resolved = isRecord(result) |
| 193 | + ? getCrateDeclaration(result, table, crate) |
| 194 | + : undefined; |
| 195 | + const version = isRecord(resolved) ? resolved["version"] : resolved; |
| 196 | + if (version === wanted) { |
| 197 | + updated = replaced; |
| 198 | + } else { |
| 199 | + notices.push( |
| 200 | + `Couldn't update ${chalk.blue(crate)} in Cargo.toml: change ${chalk.blue(declared)} to ${chalk.blue(wanted)}`, |
| 201 | + ); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + const dependencies = parsed["dependencies"]; |
| 207 | + if (isRecord(dependencies)) { |
| 208 | + notices.push(...inspectFeatures(dependencies)); |
| 209 | + } |
| 210 | + return { contents: updated, notices }; |
| 211 | +} |
0 commit comments