|
| 1 | +// @ts-check |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import * as fs from "node:fs"; |
| 4 | +import * as util from "node:util"; |
| 5 | + |
| 6 | +const ADO_PUBLISH_PIPELINE = ".ado/templates/npm-publish.yml"; |
| 7 | +const NX_CONFIG_FILE = "nx.json"; |
| 8 | + |
| 9 | +const NPM_TAG_NEXT = "next"; |
| 10 | +const NPM_TAG_NIGHTLY = "nightly"; |
| 11 | + |
| 12 | +/** |
| 13 | + * @typedef {typeof import("../../nx.json")} NxConfig |
| 14 | + * @typedef {{ tag?: string; update?: boolean; }} Options |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Exports a variable, `publish_react_native_macos`, to signal that we want to |
| 19 | + * enable publishing on Azure Pipelines. |
| 20 | + * |
| 21 | + * Note that pipelines need to read this variable separately and do the actual |
| 22 | + * work to publish bits. |
| 23 | + */ |
| 24 | +function enablePublishingOnAzurePipelines() { |
| 25 | + console.log(`##vso[task.setvariable variable=publish_react_native_macos]1`); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Logs an error message to the console. |
| 30 | + * @param {string} message |
| 31 | + */ |
| 32 | +function error(message) { |
| 33 | + console.error("❌", message); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Returns whether the given branch is considered main branch. |
| 38 | + * @param {string} branch |
| 39 | + */ |
| 40 | +function isMainBranch(branch) { |
| 41 | + // There is currently no good way to consistently get the main branch. We |
| 42 | + // hardcode the value for now. |
| 43 | + return branch === "main"; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Returns whether the given branch is considered a stable branch. |
| 48 | + * @param {string} branch |
| 49 | + */ |
| 50 | +function isStableBranch(branch) { |
| 51 | + return /^\d+\.\d+-stable$/.test(branch); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Loads Nx configuration. |
| 56 | + * @param {string} configFile |
| 57 | + * @returns {NxConfig} |
| 58 | + */ |
| 59 | +function loadNxConfig(configFile) { |
| 60 | + const nx = fs.readFileSync(configFile, { encoding: "utf-8" }); |
| 61 | + return JSON.parse(nx); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Returns a numerical value for a given version string. |
| 66 | + * @param {string} version |
| 67 | + * @returns {number} |
| 68 | + */ |
| 69 | +function versionToNumber(version) { |
| 70 | + const [major, minor] = version.split("-")[0].split("."); |
| 71 | + return Number(major) * 1000 + Number(minor); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Returns the currently checked out branch. Note that this function prefers |
| 76 | + * predefined CI environment variables over local clone. |
| 77 | + * @returns {string} |
| 78 | + */ |
| 79 | +function getCurrentBranch() { |
| 80 | + // https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#build-variables-devops-services |
| 81 | + const adoSourceBranchName = process.env["BUILD_SOURCEBRANCHNAME"]; |
| 82 | + if (adoSourceBranchName) { |
| 83 | + return adoSourceBranchName.replace(/^refs\/heads\//, ""); |
| 84 | + } |
| 85 | + |
| 86 | + // Depending on how the repo was cloned, HEAD may not exist. We only use this |
| 87 | + // method as fallback. |
| 88 | + const { stdout } = spawnSync("git", ["rev-parse", "--abbrev-ref", "HEAD"]); |
| 89 | + return stdout.toString().trim(); |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Returns the latest published version of `react-native-macos` from npm. |
| 94 | + * @returns {number} |
| 95 | + */ |
| 96 | +function getLatestVersion() { |
| 97 | + const { stdout } = spawnSync("npm", ["view", "react-native-macos@latest", "version"]); |
| 98 | + return versionToNumber(stdout.toString().trim()); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Returns the npm tag and prerelease identifier for the specified branch. |
| 103 | + * |
| 104 | + * @privateRemarks |
| 105 | + * Note that the current implementation treats minor versions as major. If |
| 106 | + * upstream ever decides to change the versioning scheme, we will need to make |
| 107 | + * changes accordingly. |
| 108 | + * |
| 109 | + * @param {string} branch |
| 110 | + * @param {Options} options |
| 111 | + * @returns {{ npmTag: string; prerelease?: string; }} |
| 112 | + */ |
| 113 | +function getTagForStableBranch(branch, { tag }) { |
| 114 | + if (!isStableBranch(branch)) { |
| 115 | + throw new Error("Expected a stable branch"); |
| 116 | + } |
| 117 | + |
| 118 | + const latestVersion = getLatestVersion(); |
| 119 | + const currentVersion = versionToNumber(branch); |
| 120 | + |
| 121 | + // Patching latest version |
| 122 | + if (currentVersion === latestVersion) { |
| 123 | + return { npmTag: "latest" }; |
| 124 | + } |
| 125 | + |
| 126 | + // Patching an older stable version |
| 127 | + if (currentVersion < latestVersion) { |
| 128 | + return { npmTag: "v" + branch }; |
| 129 | + } |
| 130 | + |
| 131 | + // Publishing a new latest version |
| 132 | + if (tag === "latest") { |
| 133 | + return { npmTag: tag }; |
| 134 | + } |
| 135 | + |
| 136 | + // Publishing a release candidate |
| 137 | + return { npmTag: NPM_TAG_NEXT, prerelease: "rc" }; |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Verifies the configuration and enables publishing on CI. |
| 142 | + * @param {NxConfig} config |
| 143 | + * @param {string} currentBranch |
| 144 | + * @param {string} tag |
| 145 | + * @param {string} [prerelease] |
| 146 | + * @returns {asserts config is NxConfig["release"]} |
| 147 | + */ |
| 148 | +function enablePublishing(config, currentBranch, tag, prerelease) { |
| 149 | + /** @type {string[]} */ |
| 150 | + const errors = []; |
| 151 | + |
| 152 | + const { defaultBase, release } = config; |
| 153 | + |
| 154 | + // `defaultBase` determines what we diff against when looking for tags or |
| 155 | + // released version and must therefore be set to either the main branch or one |
| 156 | + // of the stable branches. |
| 157 | + if (currentBranch !== defaultBase) { |
| 158 | + errors.push(`'defaultBase' must be set to '${currentBranch}'`); |
| 159 | + config.defaultBase = currentBranch; |
| 160 | + } |
| 161 | + |
| 162 | + // Determines whether we need to add "nightly" or "rc" to the version string. |
| 163 | + const { currentVersionResolverMetadata, preid } = release.version.generatorOptions; |
| 164 | + if (preid !== prerelease) { |
| 165 | + errors.push(`'release.version.generatorOptions.preid' must be set to '${prerelease || ""}'`); |
| 166 | + if (prerelease) { |
| 167 | + release.version.generatorOptions.preid = prerelease; |
| 168 | + } else { |
| 169 | + // @ts-expect-error `preid` is optional |
| 170 | + release.version.generatorOptions.preid = undefined; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // What the published version should be tagged as e.g., "latest" or "nightly". |
| 175 | + if (currentVersionResolverMetadata.tag !== tag) { |
| 176 | + errors.push(`'release.version.generatorOptions.currentVersionResolverMetadata.tag' must be set to '${tag}'`); |
| 177 | + release.version.generatorOptions.currentVersionResolverMetadata.tag = tag; |
| 178 | + } |
| 179 | + |
| 180 | + if (errors.length > 0) { |
| 181 | + errors.forEach(error); |
| 182 | + throw new Error("Nx Release is not correctly configured for the current branch"); |
| 183 | + } |
| 184 | + |
| 185 | + enablePublishingOnAzurePipelines(); |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * @param {string} file |
| 190 | + * @param {string} tag |
| 191 | + * @returns {boolean} |
| 192 | + */ |
| 193 | +function verifyPublishPipeline(file, tag) { |
| 194 | + const data = fs.readFileSync(file, { encoding: "utf-8" }); |
| 195 | + const m = data.match(/publishTag: '(\w*?)'/); |
| 196 | + if (!m) { |
| 197 | + error(`${file}: Could not find npm publish tag`); |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + if (m[1] !== tag) { |
| 202 | + error(`${file}: 'publishTag' needs to be set to '${tag}'`); |
| 203 | + return false; |
| 204 | + } |
| 205 | + |
| 206 | + return true; |
| 207 | +} |
| 208 | + |
| 209 | +/** |
| 210 | + * @param {Options} options |
| 211 | + * @returns {number} |
| 212 | + */ |
| 213 | +function main(options) { |
| 214 | + const branch = getCurrentBranch(); |
| 215 | + if (!branch) { |
| 216 | + error("Could not get current branch"); |
| 217 | + return 1; |
| 218 | + } |
| 219 | + |
| 220 | + if (!verifyPublishPipeline(ADO_PUBLISH_PIPELINE, options.tag || NPM_TAG_NEXT)) { |
| 221 | + return 1; |
| 222 | + } |
| 223 | + |
| 224 | + const config = loadNxConfig(NX_CONFIG_FILE); |
| 225 | + try { |
| 226 | + if (isMainBranch(branch)) { |
| 227 | + enablePublishing(config, branch, NPM_TAG_NIGHTLY, NPM_TAG_NIGHTLY); |
| 228 | + } else if (isStableBranch(branch)) { |
| 229 | + const { npmTag, prerelease } = getTagForStableBranch(branch, options); |
| 230 | + enablePublishing(config, branch, npmTag, prerelease); |
| 231 | + } |
| 232 | + } catch (e) { |
| 233 | + if (options.update) { |
| 234 | + const fd = fs.openSync(NX_CONFIG_FILE, "w"); |
| 235 | + fs.writeSync(fd, JSON.stringify(config, undefined, 2)); |
| 236 | + fs.writeSync(fd, "\n"); |
| 237 | + fs.closeSync(fd) |
| 238 | + } else { |
| 239 | + console.error(`${e}`); |
| 240 | + } |
| 241 | + return 1; |
| 242 | + } |
| 243 | + |
| 244 | + return 0; |
| 245 | +} |
| 246 | + |
| 247 | +const { values } = util.parseArgs({ |
| 248 | + args: process.argv.slice(2), |
| 249 | + options: { |
| 250 | + tag: { |
| 251 | + type: "string", |
| 252 | + default: NPM_TAG_NEXT, |
| 253 | + }, |
| 254 | + update: { |
| 255 | + type: "boolean", |
| 256 | + default: false, |
| 257 | + }, |
| 258 | + }, |
| 259 | + strict: true, |
| 260 | +}); |
| 261 | + |
| 262 | +process.exitCode = main(values); |
0 commit comments