|
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import { createInterface } from 'readline'; |
| 3 | +import { parse, gt, prerelease, SemVer } from 'semver'; |
| 4 | +import { readFileSync } from 'fs'; |
| 5 | +import { join } from 'path'; |
| 6 | + |
| 7 | +const REPO_URL = 'https://github.com/RocketChat/Rocket.Chat.Electron'; |
| 8 | + |
| 9 | +const getVersion = (): string => { |
| 10 | + const packageJson = JSON.parse( |
| 11 | + readFileSync(join(__dirname, '..', 'package.json'), 'utf-8') |
| 12 | + ); |
| 13 | + return packageJson.version; |
| 14 | +}; |
| 15 | + |
| 16 | +const getChannel = (version: SemVer): string => { |
| 17 | + const pre = prerelease(version); |
| 18 | + if (!pre || pre.length === 0) return 'stable'; |
| 19 | + if (pre[0] === 'alpha') return 'alpha'; |
| 20 | + if (pre[0] === 'beta') return 'beta'; |
| 21 | + if (pre[0] === 'rc' || pre[0] === 'candidate') return 'candidate'; |
| 22 | + return 'prerelease'; |
| 23 | +}; |
| 24 | + |
| 25 | +const exec = (cmd: string): string | null => { |
| 26 | + try { |
| 27 | + return execSync(cmd, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }).trim(); |
| 28 | + } catch { |
| 29 | + return null; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +const fetchTags = (): void => { |
| 34 | + console.log('Fetching tags from remote...'); |
| 35 | + execSync('git fetch --tags', { stdio: 'inherit' }); |
| 36 | +}; |
| 37 | + |
| 38 | +const normalizeTag = (tag: string): string => { |
| 39 | + // Strip leading 'v' if present for consistent comparison |
| 40 | + return tag.startsWith('v') ? tag.slice(1) : tag; |
| 41 | +}; |
| 42 | + |
| 43 | +const getExistingTags = (): string[] => { |
| 44 | + const output = exec('git tag -l'); |
| 45 | + if (output === null) { |
| 46 | + console.error(' Warning: Failed to list git tags'); |
| 47 | + return []; |
| 48 | + } |
| 49 | + if (!output) return []; |
| 50 | + // Return normalized tags (without 'v' prefix) for consistent comparison |
| 51 | + return output.split('\n').filter(Boolean).map(normalizeTag); |
| 52 | +}; |
| 53 | + |
| 54 | +const getLatestTagForChannel = (tags: string[], channel: string): SemVer | null => { |
| 55 | + const channelTags = tags |
| 56 | + .map((tag) => parse(tag)) |
| 57 | + .filter((v): v is SemVer => v !== null) |
| 58 | + .filter((v) => getChannel(v) === channel) |
| 59 | + .sort((a, b) => (gt(a, b) ? -1 : 1)); |
| 60 | + |
| 61 | + return channelTags[0] || null; |
| 62 | +}; |
| 63 | + |
| 64 | +const prompt = (question: string): Promise<string> => { |
| 65 | + const rl = createInterface({ |
| 66 | + input: process.stdin, |
| 67 | + output: process.stdout, |
| 68 | + }); |
| 69 | + |
| 70 | + return new Promise((resolve) => { |
| 71 | + rl.question(question, (answer) => { |
| 72 | + rl.close(); |
| 73 | + resolve(answer.trim().toLowerCase()); |
| 74 | + }); |
| 75 | + }); |
| 76 | +}; |
| 77 | + |
| 78 | +const main = async (): Promise<void> => { |
| 79 | + console.log('\n Release Tag Creator\n'); |
| 80 | + |
| 81 | + // 1. Read version from package.json |
| 82 | + const versionString = getVersion(); |
| 83 | + const version = parse(versionString); |
| 84 | + |
| 85 | + if (!version) { |
| 86 | + console.error(`Error: Invalid version in package.json: ${versionString}`); |
| 87 | + process.exit(1); |
| 88 | + } |
| 89 | + |
| 90 | + // 2. Detect channel |
| 91 | + const channel = getChannel(version); |
| 92 | + |
| 93 | + console.log(` Version: ${version.version}`); |
| 94 | + console.log(` Channel: ${channel}`); |
| 95 | + console.log(` Tag: ${version.version}`); |
| 96 | + console.log(''); |
| 97 | + |
| 98 | + // 3. Fetch tags |
| 99 | + fetchTags(); |
| 100 | + |
| 101 | + // 4. Check if tag already exists |
| 102 | + const existingTags = getExistingTags(); |
| 103 | + |
| 104 | + if (existingTags.includes(version.version)) { |
| 105 | + console.error(`\n Error: Tag ${version.version} already exists!`); |
| 106 | + console.error(` The version in package.json has already been released.`); |
| 107 | + console.error(` Please bump the version before creating a new release.\n`); |
| 108 | + process.exit(1); |
| 109 | + } |
| 110 | + |
| 111 | + console.log(` Tag does not exist yet`); |
| 112 | + |
| 113 | + // 5. Check if version is newer than latest in channel |
| 114 | + const latestInChannel = getLatestTagForChannel(existingTags, channel); |
| 115 | + |
| 116 | + if (latestInChannel && !gt(version, latestInChannel)) { |
| 117 | + console.warn(`\n Warning: Version ${version.version} is not greater than`); |
| 118 | + console.warn(` the latest ${channel} release (${latestInChannel.version}).`); |
| 119 | + console.warn(` This may be intentional, but please verify.\n`); |
| 120 | + } else if (latestInChannel) { |
| 121 | + console.log(` Latest ${channel}: ${latestInChannel.version}`); |
| 122 | + } else { |
| 123 | + console.log(` First ${channel} release`); |
| 124 | + } |
| 125 | + |
| 126 | + // 6. Show confirmation |
| 127 | + console.log('\n This will:'); |
| 128 | + console.log(` 1. Create git tag: ${version.version}`); |
| 129 | + console.log(` 2. Push tag to origin`); |
| 130 | + console.log(` 3. Trigger GitHub Actions build-release workflow\n`); |
| 131 | + |
| 132 | + const answer = await prompt(' Proceed? (y/N): '); |
| 133 | + |
| 134 | + if (answer !== 'y' && answer !== 'yes') { |
| 135 | + console.log('\n Aborted.\n'); |
| 136 | + process.exit(0); |
| 137 | + } |
| 138 | + |
| 139 | + // 7. Create and push tag |
| 140 | + console.log(`\n Creating tag ${version.version}...`); |
| 141 | + try { |
| 142 | + execSync(`git tag -- ${version.version}`, { stdio: 'inherit' }); |
| 143 | + } catch { |
| 144 | + console.error(` Error: Failed to create tag`); |
| 145 | + process.exit(1); |
| 146 | + } |
| 147 | + |
| 148 | + console.log(` Pushing tag to origin...`); |
| 149 | + try { |
| 150 | + execSync(`git push origin refs/tags/${version.version}`, { stdio: 'inherit' }); |
| 151 | + } catch { |
| 152 | + console.error(` Error: Failed to push tag`); |
| 153 | + console.error(` The local tag was created. You may need to push it manually.`); |
| 154 | + process.exit(1); |
| 155 | + } |
| 156 | + |
| 157 | + // 8. Success message |
| 158 | + console.log(`\n Tag created and pushed successfully!\n`); |
| 159 | + console.log(` Monitor build: ${REPO_URL}/actions`); |
| 160 | + console.log(` Releases: ${REPO_URL}/releases\n`); |
| 161 | +}; |
| 162 | + |
| 163 | +main().catch((error) => { |
| 164 | + console.error('Unexpected error:', error); |
| 165 | + process.exit(1); |
| 166 | +}); |
0 commit comments