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