|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { appendFileSync, readFileSync, writeFileSync } from 'node:fs'; |
| 4 | +import { resolve } from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | + |
| 7 | +const channels = new Set(['stable', 'beta', 'nightly']); |
| 8 | +const events = new Set(['push', 'workflow_call', 'workflow_dispatch']); |
| 9 | +const versionPattern = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*))*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/u; |
| 10 | + |
| 11 | +function desktopChannel(value) { |
| 12 | + if (typeof value !== 'string' || !channels.has(value)) { |
| 13 | + throw new Error('Desktop release channel must be stable, beta, or nightly.'); |
| 14 | + } |
| 15 | + return value; |
| 16 | +} |
| 17 | + |
| 18 | +function parseDesktopVersion(value) { |
| 19 | + const match = typeof value === 'string' ? versionPattern.exec(value) : null; |
| 20 | + if (match === null) { |
| 21 | + throw new Error(`Invalid desktop release version: ${String(value)}`); |
| 22 | + } |
| 23 | + const major = Number(match[1]); |
| 24 | + const minor = Number(match[2]); |
| 25 | + const patch = Number(match[3]); |
| 26 | + if (![major, minor, patch].every(Number.isSafeInteger)) { |
| 27 | + throw new Error(`Desktop release version is outside the safe integer range: ${value}`); |
| 28 | + } |
| 29 | + return { major, minor, patch, prerelease: match[4] }; |
| 30 | +} |
| 31 | + |
| 32 | +function desktopVersion(value) { |
| 33 | + parseDesktopVersion(value); |
| 34 | + return value; |
| 35 | +} |
| 36 | + |
| 37 | +function versionChannel(version) { |
| 38 | + const prerelease = parseDesktopVersion(version).prerelease; |
| 39 | + if (prerelease === undefined) return 'stable'; |
| 40 | + const channel = prerelease.split('.')[0]; |
| 41 | + if (channel === 'beta' || channel === 'nightly') return channel; |
| 42 | + throw new Error(`Unsupported desktop release channel in ${version}.`); |
| 43 | +} |
| 44 | + |
| 45 | +export function desktopReleaseChannel(version) { |
| 46 | + return versionChannel(version); |
| 47 | +} |
| 48 | + |
| 49 | +function normalizedCommitCount(value) { |
| 50 | + const count = typeof value === 'number' ? value : Number(value); |
| 51 | + if (!Number.isSafeInteger(count) || count < 1 || String(count) !== String(value)) { |
| 52 | + throw new Error(`Invalid main commit count: ${String(value)}`); |
| 53 | + } |
| 54 | + return count; |
| 55 | +} |
| 56 | + |
| 57 | +export function desktopManifestName(channel, platform) { |
| 58 | + const resolvedChannel = desktopChannel(channel); |
| 59 | + if (platform !== 'mac' && platform !== 'win') throw new Error(`Unsupported desktop platform: ${platform}`); |
| 60 | + const prefix = resolvedChannel === 'stable' ? 'latest' : resolvedChannel; |
| 61 | + return platform === 'mac' ? `${prefix}-mac.yml` : `${prefix}.yml`; |
| 62 | +} |
| 63 | + |
| 64 | +export function nightlyDesktopVersion(packageVersion, commitCount) { |
| 65 | + const parsed = parseDesktopVersion(packageVersion); |
| 66 | + const patch = parsed.prerelease === undefined ? parsed.patch + 1 : parsed.patch; |
| 67 | + if (!Number.isSafeInteger(patch)) throw new Error(`Cannot derive a Nightly version from ${packageVersion}.`); |
| 68 | + const next = `${parsed.major}.${parsed.minor}.${patch}`; |
| 69 | + return `${next}-nightly.${normalizedCommitCount(commitCount)}`; |
| 70 | +} |
| 71 | + |
| 72 | +function previewVersion(packageVersion, channel, commitCount) { |
| 73 | + if (channel === 'nightly') return nightlyDesktopVersion(packageVersion, commitCount); |
| 74 | + if (versionChannel(packageVersion) === channel) return packageVersion; |
| 75 | + const parsed = parseDesktopVersion(packageVersion); |
| 76 | + const patch = parsed.prerelease === undefined ? parsed.patch + 1 : parsed.patch; |
| 77 | + if (!Number.isSafeInteger(patch)) throw new Error(`Cannot derive a ${channel} version from ${packageVersion}.`); |
| 78 | + const next = `${parsed.major}.${parsed.minor}.${patch}`; |
| 79 | + if (channel === 'stable') return next; |
| 80 | + return `${next}-beta.${normalizedCommitCount(commitCount)}`; |
| 81 | +} |
| 82 | + |
| 83 | +export function resolveDesktopRelease(options) { |
| 84 | + if (typeof options !== 'object' || options === null) throw new Error('Desktop release options are required.'); |
| 85 | + const eventName = options.eventName; |
| 86 | + if (typeof eventName !== 'string' || !events.has(eventName)) { |
| 87 | + throw new Error(`Unsupported desktop release event: ${String(eventName)}`); |
| 88 | + } |
| 89 | + const packageVersion = desktopVersion(options.packageVersion); |
| 90 | + let channel; |
| 91 | + let version; |
| 92 | + let publish; |
| 93 | + |
| 94 | + if (eventName === 'push') { |
| 95 | + channel = versionChannel(packageVersion); |
| 96 | + if (channel === 'nightly') { |
| 97 | + throw new Error('Nightly desktop releases must come from the scheduled workflow.'); |
| 98 | + } |
| 99 | + version = packageVersion; |
| 100 | + publish = true; |
| 101 | + const expectedTag = `desktop-v${version}`; |
| 102 | + if (options.tagName !== expectedTag) { |
| 103 | + throw new Error(`Tag ${String(options.tagName)} does not match apps/desktop/package.json (${version}).`); |
| 104 | + } |
| 105 | + } else if (eventName === 'workflow_call') { |
| 106 | + if (options.publishNightly !== true) { |
| 107 | + throw new Error('Reusable desktop releases require explicit Nightly publishing permission.'); |
| 108 | + } |
| 109 | + channel = 'nightly'; |
| 110 | + version = nightlyDesktopVersion(packageVersion, options.commitCount); |
| 111 | + publish = true; |
| 112 | + } else { |
| 113 | + channel = desktopChannel(options.requestedChannel); |
| 114 | + version = previewVersion(packageVersion, channel, options.commitCount); |
| 115 | + publish = false; |
| 116 | + } |
| 117 | + |
| 118 | + return { |
| 119 | + channel, |
| 120 | + feedChannel: channel === 'stable' ? 'latest' : channel, |
| 121 | + macManifest: desktopManifestName(channel, 'mac'), |
| 122 | + prerelease: channel !== 'stable', |
| 123 | + publish, |
| 124 | + releaseTag: `v${version}`, |
| 125 | + version, |
| 126 | + winManifest: desktopManifestName(channel, 'win'), |
| 127 | + }; |
| 128 | +} |
| 129 | + |
| 130 | +export function configureDesktopPackage(value, version, channel) { |
| 131 | + const resolvedVersion = desktopVersion(version); |
| 132 | + const resolvedChannel = desktopChannel(channel); |
| 133 | + if (versionChannel(resolvedVersion) !== resolvedChannel) { |
| 134 | + throw new Error(`Desktop version ${resolvedVersion} does not belong to the ${resolvedChannel} channel.`); |
| 135 | + } |
| 136 | + if (typeof value !== 'object' || value === null || Array.isArray(value)) { |
| 137 | + throw new Error('Desktop package metadata must be an object.'); |
| 138 | + } |
| 139 | + const build = value.build; |
| 140 | + if (typeof build !== 'object' || build === null || Array.isArray(build)) { |
| 141 | + throw new Error('Desktop package build metadata must be an object.'); |
| 142 | + } |
| 143 | + const publish = build.publish; |
| 144 | + if (!Array.isArray(publish) || publish.length === 0) { |
| 145 | + throw new Error('Desktop package must define a publish provider.'); |
| 146 | + } |
| 147 | + const provider = publish[0]; |
| 148 | + if (typeof provider !== 'object' || provider === null || Array.isArray(provider) || provider.provider !== 'github') { |
| 149 | + throw new Error('Desktop release publishing must use the GitHub provider.'); |
| 150 | + } |
| 151 | + return { |
| 152 | + ...value, |
| 153 | + version: resolvedVersion, |
| 154 | + build: { |
| 155 | + ...build, |
| 156 | + publish: [ |
| 157 | + { |
| 158 | + ...provider, |
| 159 | + channel: resolvedChannel === 'stable' ? 'latest' : resolvedChannel, |
| 160 | + releaseType: resolvedChannel === 'stable' ? 'release' : 'prerelease', |
| 161 | + }, |
| 162 | + ...publish.slice(1), |
| 163 | + ], |
| 164 | + }, |
| 165 | + }; |
| 166 | +} |
| 167 | + |
| 168 | +function writeOutputs(result) { |
| 169 | + const lines = [ |
| 170 | + `version=${result.version}`, |
| 171 | + `tag=${result.releaseTag}`, |
| 172 | + `channel=${result.channel}`, |
| 173 | + `feed_channel=${result.feedChannel}`, |
| 174 | + `mac_manifest=${result.macManifest}`, |
| 175 | + `win_manifest=${result.winManifest}`, |
| 176 | + `publish=${String(result.publish)}`, |
| 177 | + `prerelease=${String(result.prerelease)}`, |
| 178 | + ]; |
| 179 | + const output = process.env.GITHUB_OUTPUT; |
| 180 | + if (output === undefined || output === '') { |
| 181 | + for (const line of lines) process.stdout.write(`${line}\n`); |
| 182 | + } else { |
| 183 | + appendFileSync(output, `${lines.join('\n')}\n`); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +function main() { |
| 188 | + const [command, ...args] = process.argv.slice(2); |
| 189 | + if (command === 'resolve' && args.length === 6) { |
| 190 | + const [eventName, packageVersion, requestedChannel, tagName, commitCount, publishNightlyValue] = args; |
| 191 | + if (!['', 'false', 'true'].includes(publishNightlyValue)) { |
| 192 | + throw new Error(`Invalid Nightly publishing permission: ${publishNightlyValue}`); |
| 193 | + } |
| 194 | + writeOutputs(resolveDesktopRelease({ |
| 195 | + eventName, |
| 196 | + packageVersion, |
| 197 | + publishNightly: publishNightlyValue === 'true', |
| 198 | + requestedChannel: requestedChannel === '' ? undefined : requestedChannel, |
| 199 | + tagName: tagName === '' ? undefined : tagName, |
| 200 | + commitCount, |
| 201 | + })); |
| 202 | + return; |
| 203 | + } |
| 204 | + if (command === 'configure' && args.length === 3) { |
| 205 | + const [path, version, channel] = args; |
| 206 | + const packagePath = resolve(path); |
| 207 | + const configured = configureDesktopPackage(JSON.parse(readFileSync(packagePath, 'utf8')), version, channel); |
| 208 | + writeFileSync(packagePath, `${JSON.stringify(configured, null, 2)}\n`, 'utf8'); |
| 209 | + return; |
| 210 | + } |
| 211 | + throw new Error('Usage: desktop-release.mjs resolve <event> <package-version> <channel> <tag> <commit-count> <publish-nightly> | configure <package-json> <version> <channel>'); |
| 212 | +} |
| 213 | + |
| 214 | +if (process.argv[1] !== undefined && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { |
| 215 | + try { |
| 216 | + main(); |
| 217 | + } catch (error) { |
| 218 | + process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`); |
| 219 | + process.exitCode = 1; |
| 220 | + } |
| 221 | +} |
0 commit comments