|
| 1 | +#!/usr/bin/env node |
| 2 | +import { $, argv, echo, fs } from 'zx'; |
| 3 | +import { resolve } from 'node:path'; |
| 4 | + |
| 5 | +const NPM_DEFAULT_REGISTRY = 'https://registry.npmjs.org/'; |
| 6 | +const NPM_TAG_NEXT = 'next'; |
| 7 | + |
| 8 | +export type ReleaseState = 'STABLE_IS_LATEST' | 'STABLE_IS_NEW' | 'STABLE_IS_OLD'; |
| 9 | + |
| 10 | +export interface ReleaseStateInfo { |
| 11 | + state: ReleaseState; |
| 12 | + currentVersion: number; |
| 13 | + latestVersion: number; |
| 14 | + nextVersion: number; |
| 15 | +} |
| 16 | + |
| 17 | +export interface TagInfo { |
| 18 | + npmTags: string[]; |
| 19 | + prerelease?: string; |
| 20 | +} |
| 21 | + |
| 22 | +interface Options { |
| 23 | + 'mock-branch'?: string; |
| 24 | + 'skip-auth'?: boolean; |
| 25 | + tag?: string; |
| 26 | + verbose?: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Exports a variable, `publish_react_native_macos`, to signal that we want to |
| 31 | + * enable publishing on Azure Pipelines. |
| 32 | + */ |
| 33 | +function enablePublishingOnAzurePipelines() { |
| 34 | + echo(`##vso[task.setvariable variable=publish_react_native_macos]1`); |
| 35 | +} |
| 36 | + |
| 37 | +export function isMainBranch(branch: string): boolean { |
| 38 | + return branch === 'main'; |
| 39 | +} |
| 40 | + |
| 41 | +export function isStableBranch(branch: string): boolean { |
| 42 | + return /^\d+\.\d+-stable$/.test(branch); |
| 43 | +} |
| 44 | + |
| 45 | +export function versionToNumber(version: string): number { |
| 46 | + const [major, minor] = version.split('-')[0].split('.'); |
| 47 | + return Number(major) * 1000 + Number(minor); |
| 48 | +} |
| 49 | + |
| 50 | +function getTargetBranch(): string | undefined { |
| 51 | + // Azure Pipelines |
| 52 | + if (process.env['TF_BUILD'] === 'True') { |
| 53 | + const targetBranch = process.env['SYSTEM_PULLREQUEST_TARGETBRANCH']; |
| 54 | + return targetBranch?.replace(/^refs\/heads\//, ''); |
| 55 | + } |
| 56 | + |
| 57 | + // GitHub Actions |
| 58 | + if (process.env['GITHUB_ACTIONS'] === 'true') { |
| 59 | + return process.env['GITHUB_BASE_REF']; |
| 60 | + } |
| 61 | + |
| 62 | + return undefined; |
| 63 | +} |
| 64 | + |
| 65 | +async function getCurrentBranch(options: Options): Promise<string> { |
| 66 | + const targetBranch = getTargetBranch(); |
| 67 | + if (targetBranch) { |
| 68 | + return targetBranch; |
| 69 | + } |
| 70 | + |
| 71 | + // Azure DevOps Pipelines |
| 72 | + if (process.env['TF_BUILD'] === 'True') { |
| 73 | + const sourceBranch = process.env['BUILD_SOURCEBRANCHNAME']; |
| 74 | + if (sourceBranch) { |
| 75 | + return sourceBranch.replace(/^refs\/heads\//, ''); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // GitHub Actions |
| 80 | + if (process.env['GITHUB_ACTIONS'] === 'true') { |
| 81 | + const headRef = process.env['GITHUB_HEAD_REF']; |
| 82 | + if (headRef) return headRef; |
| 83 | + |
| 84 | + const ref = process.env['GITHUB_REF']; |
| 85 | + if (ref) return ref.replace(/^refs\/heads\//, ''); |
| 86 | + } |
| 87 | + |
| 88 | + if (options['mock-branch']) { |
| 89 | + return options['mock-branch']; |
| 90 | + } |
| 91 | + |
| 92 | + const result = await $`git rev-parse --abbrev-ref HEAD`; |
| 93 | + return result.stdout.trim(); |
| 94 | +} |
| 95 | + |
| 96 | +function getPublishedVersionSync(tag: 'latest' | 'next'): number { |
| 97 | + const result = $.sync`npm view react-native-macos@${tag} version`; |
| 98 | + return versionToNumber(result.stdout.trim()); |
| 99 | +} |
| 100 | + |
| 101 | +export function getReleaseState( |
| 102 | + branch: string, |
| 103 | + getVersion: (tag: 'latest' | 'next') => number = getPublishedVersionSync, |
| 104 | +): ReleaseStateInfo { |
| 105 | + if (!isStableBranch(branch)) { |
| 106 | + throw new Error('Expected a stable branch'); |
| 107 | + } |
| 108 | + |
| 109 | + const latestVersion = getVersion('latest'); |
| 110 | + const nextVersion = getVersion('next'); |
| 111 | + const currentVersion = versionToNumber(branch); |
| 112 | + |
| 113 | + let state: ReleaseState; |
| 114 | + if (currentVersion === latestVersion) { |
| 115 | + state = 'STABLE_IS_LATEST'; |
| 116 | + } else if (currentVersion < latestVersion) { |
| 117 | + state = 'STABLE_IS_OLD'; |
| 118 | + } else { |
| 119 | + state = 'STABLE_IS_NEW'; |
| 120 | + } |
| 121 | + |
| 122 | + return { state, currentVersion, latestVersion, nextVersion }; |
| 123 | +} |
| 124 | + |
| 125 | +export function getPublishTags( |
| 126 | + stateInfo: ReleaseStateInfo, |
| 127 | + branch: string, |
| 128 | + tag: string = NPM_TAG_NEXT, |
| 129 | +): TagInfo { |
| 130 | + const { state, currentVersion, nextVersion } = stateInfo; |
| 131 | + |
| 132 | + switch (state) { |
| 133 | + case 'STABLE_IS_LATEST': |
| 134 | + // Patching the current latest version |
| 135 | + return { npmTags: ['latest', branch] }; |
| 136 | + |
| 137 | + case 'STABLE_IS_OLD': |
| 138 | + // Patching an older stable version |
| 139 | + return { npmTags: [branch] }; |
| 140 | + |
| 141 | + case 'STABLE_IS_NEW': { |
| 142 | + if (tag === 'latest') { |
| 143 | + // Promoting this branch to latest |
| 144 | + const npmTags = ['latest', branch]; |
| 145 | + if (currentVersion > nextVersion) { |
| 146 | + npmTags.push(NPM_TAG_NEXT); |
| 147 | + } |
| 148 | + return { npmTags }; |
| 149 | + } |
| 150 | + |
| 151 | + // Publishing a release candidate |
| 152 | + if (currentVersion < nextVersion) { |
| 153 | + throw new Error( |
| 154 | + `Current version cannot be a release candidate because it is too old: ${currentVersion} < ${nextVersion}`, |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + return { npmTags: [NPM_TAG_NEXT], prerelease: 'rc' }; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +async function verifyNpmAuth(registry = NPM_DEFAULT_REGISTRY) { |
| 164 | + const whoami = await $`npm whoami --registry ${registry}`.nothrow(); |
| 165 | + if (whoami.exitCode !== 0) { |
| 166 | + const errText = whoami.stderr; |
| 167 | + const m = errText.match(/npm error code (\w+)/); |
| 168 | + const errorCode = m && m[1]; |
| 169 | + switch (errorCode) { |
| 170 | + case 'EINVALIDNPMTOKEN': |
| 171 | + throw new Error(`Invalid auth token for npm registry: ${registry}`); |
| 172 | + case 'ENEEDAUTH': |
| 173 | + throw new Error(`Missing auth token for npm registry: ${registry}`); |
| 174 | + default: |
| 175 | + throw new Error(errText); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +async function enablePublishing(tagInfo: TagInfo, options: Options) { |
| 181 | + const [primaryTag, ...additionalTags] = tagInfo.npmTags; |
| 182 | + |
| 183 | + // Output publishTag for subsequent pipeline steps |
| 184 | + echo(`##vso[task.setvariable variable=publishTag]${primaryTag}`); |
| 185 | + if (process.env['GITHUB_OUTPUT']) { |
| 186 | + fs.appendFileSync(process.env['GITHUB_OUTPUT'], `publishTag=${primaryTag}\n`); |
| 187 | + } |
| 188 | + |
| 189 | + // Output additional tags |
| 190 | + if (additionalTags.length > 0) { |
| 191 | + const tagsValue = additionalTags.join(','); |
| 192 | + echo(`##vso[task.setvariable variable=additionalTags]${tagsValue}`); |
| 193 | + if (process.env['GITHUB_OUTPUT']) { |
| 194 | + fs.appendFileSync(process.env['GITHUB_OUTPUT'], `additionalTags=${tagsValue}\n`); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if (options['skip-auth']) { |
| 199 | + echo('ℹ️ Skipped npm auth validation'); |
| 200 | + } else { |
| 201 | + await verifyNpmAuth(); |
| 202 | + } |
| 203 | + |
| 204 | + // Don't enable publishing in PRs |
| 205 | + if (!getTargetBranch()) { |
| 206 | + enablePublishingOnAzurePipelines(); |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +const isDirectRun = |
| 211 | + process.argv[1] != null && |
| 212 | + resolve(process.argv[1]) === new URL(import.meta.url).pathname; |
| 213 | + |
| 214 | +if (isDirectRun) { |
| 215 | + // Parse CLI args using zx's argv (minimist) |
| 216 | + const options: Options = { |
| 217 | + 'mock-branch': argv['mock-branch'] as string | undefined, |
| 218 | + 'skip-auth': Boolean(argv['skip-auth']), |
| 219 | + tag: typeof argv['tag'] === 'string' ? argv['tag'] : NPM_TAG_NEXT, |
| 220 | + verbose: Boolean(argv['verbose']), |
| 221 | + }; |
| 222 | + |
| 223 | + const branch = await getCurrentBranch(options); |
| 224 | + if (!branch) { |
| 225 | + echo('❌ Could not get current branch'); |
| 226 | + process.exit(1); |
| 227 | + } |
| 228 | + |
| 229 | + const log = options.verbose ? (msg: string) => echo(`ℹ️ ${msg}`) : () => {}; |
| 230 | + |
| 231 | + try { |
| 232 | + if (isMainBranch(branch)) { |
| 233 | + // Nightlies are currently disabled — skip publishing from main |
| 234 | + echo('ℹ️ On main branch — nightly publishing is currently disabled'); |
| 235 | + } else if (isStableBranch(branch)) { |
| 236 | + const stateInfo = getReleaseState(branch); |
| 237 | + log(`react-native-macos@latest: ${stateInfo.latestVersion}`); |
| 238 | + log(`react-native-macos@next: ${stateInfo.nextVersion}`); |
| 239 | + log(`Current version: ${stateInfo.currentVersion}`); |
| 240 | + log(`Release state: ${stateInfo.state}`); |
| 241 | + |
| 242 | + const tagInfo = getPublishTags(stateInfo, branch, options.tag); |
| 243 | + log(`Expected npm tags: ${tagInfo.npmTags.join(', ')}`); |
| 244 | + |
| 245 | + await enablePublishing(tagInfo, options); |
| 246 | + } else { |
| 247 | + echo(`ℹ️ Branch '${branch}' is not main or a stable branch — skipping`); |
| 248 | + } |
| 249 | + } catch (e) { |
| 250 | + echo(`❌ ${(e as Error).message}`); |
| 251 | + process.exit(1); |
| 252 | + } |
| 253 | +} |
0 commit comments