|
| 1 | +#!/usr/bin/env node |
| 2 | +/* eslint-disable no-console */ |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | +const { execSync } = require('child_process') |
| 6 | + |
| 7 | +const ROOT = path.resolve(__dirname, '..') |
| 8 | +const PKG_PATH = path.join(ROOT, 'package.json') |
| 9 | +const LOCK_PATH = path.join(ROOT, 'package-lock.json') |
| 10 | + |
| 11 | +function escapeRegExp(str) { |
| 12 | + return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
| 13 | +} |
| 14 | + |
| 15 | +function parseArgs(argv) { |
| 16 | + const args = argv.slice(2) |
| 17 | + const out = { |
| 18 | + bump: 'patch', // patch|minor|major|x.y.z |
| 19 | + publish: false, |
| 20 | + tag: false, |
| 21 | + stageAll: false |
| 22 | + } |
| 23 | + |
| 24 | + for (let i = 0; i < args.length; i++) { |
| 25 | + const a = args[i] |
| 26 | + if (a === '--bump' || a === '-b') { |
| 27 | + out.bump = args[i + 1] |
| 28 | + i++ |
| 29 | + continue |
| 30 | + } |
| 31 | + if (a === '--publish') { |
| 32 | + out.publish = true |
| 33 | + continue |
| 34 | + } |
| 35 | + if (a === '--tag') { |
| 36 | + out.tag = true |
| 37 | + continue |
| 38 | + } |
| 39 | + if (a === '--all') { |
| 40 | + out.stageAll = true |
| 41 | + continue |
| 42 | + } |
| 43 | + if (a === '--help' || a === '-h') { |
| 44 | + out.help = true |
| 45 | + continue |
| 46 | + } |
| 47 | + // allow: node scripts/release.js patch |
| 48 | + if (!a.startsWith('-') && !out._freeBumpSet) { |
| 49 | + out.bump = a |
| 50 | + out._freeBumpSet = true |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return out |
| 55 | +} |
| 56 | + |
| 57 | +function bumpSemver(oldVersion, bump) { |
| 58 | + const m = oldVersion.match(/^(\d+)\.(\d+)\.(\d+)$/) |
| 59 | + if (!m) { |
| 60 | + throw new Error(`Unsupported version format: ${oldVersion} (expected x.y.z)`) |
| 61 | + } |
| 62 | + let major = Number(m[1]) |
| 63 | + let minor = Number(m[2]) |
| 64 | + let patch = Number(m[3]) |
| 65 | + |
| 66 | + if (/^\d+\.\d+\.\d+$/.test(bump)) { |
| 67 | + return bump |
| 68 | + } |
| 69 | + |
| 70 | + if (bump === 'patch') patch += 1 |
| 71 | + else if (bump === 'minor') { |
| 72 | + minor += 1 |
| 73 | + patch = 0 |
| 74 | + } else if (bump === 'major') { |
| 75 | + major += 1 |
| 76 | + minor = 0 |
| 77 | + patch = 0 |
| 78 | + } else { |
| 79 | + throw new Error(`Unsupported bump: ${bump} (use patch|minor|major|x.y.z)`) |
| 80 | + } |
| 81 | + |
| 82 | + return `${major}.${minor}.${patch}` |
| 83 | +} |
| 84 | + |
| 85 | +function updatePackageJsonVersion(oldVersion, newVersion) { |
| 86 | + const pkg = JSON.parse(fs.readFileSync(PKG_PATH, 'utf8')) |
| 87 | + if (pkg.version !== oldVersion) { |
| 88 | + throw new Error(`package.json version mismatch: expected ${oldVersion}, got ${pkg.version}`) |
| 89 | + } |
| 90 | + pkg.version = newVersion |
| 91 | + fs.writeFileSync(PKG_PATH, JSON.stringify(pkg, null, 2) + '\n', 'utf8') |
| 92 | +} |
| 93 | + |
| 94 | +function updatePackageLockVersion(oldVersion, newVersion) { |
| 95 | + if (!fs.existsSync(LOCK_PATH)) return |
| 96 | + let lock = fs.readFileSync(LOCK_PATH, 'utf8') |
| 97 | + |
| 98 | + const topNeedle = `"name": "hydrogen-js-sdk",\n "version": "${oldVersion}",` |
| 99 | + const topReplace = `"name": "hydrogen-js-sdk",\n "version": "${newVersion}",` |
| 100 | + |
| 101 | + const pkgNeedle = `"name": "hydrogen-js-sdk",\n "version": "${oldVersion}",` |
| 102 | + const pkgReplace = `"name": "hydrogen-js-sdk",\n "version": "${newVersion}",` |
| 103 | + |
| 104 | + let replaced = 0 |
| 105 | + if (lock.includes(topNeedle)) { |
| 106 | + lock = lock.replace(topNeedle, topReplace) |
| 107 | + replaced++ |
| 108 | + } |
| 109 | + if (lock.includes(pkgNeedle)) { |
| 110 | + lock = lock.replace(pkgNeedle, pkgReplace) |
| 111 | + replaced++ |
| 112 | + } |
| 113 | + |
| 114 | + if (replaced !== 2) { |
| 115 | + // fallback: try a generic targeted replace (still safer than rewriting whole lock file) |
| 116 | + const escapedOld = escapeRegExp(oldVersion) |
| 117 | + const r1 = new RegExp(`("name"\\s*:\\s*"hydrogen-js-sdk"\\s*,\\s*\\n\\s*"version"\\s*:\\s*")${escapedOld}(")`) |
| 118 | + const r2 = new RegExp(`("name"\\s*:\\s*"hydrogen-js-sdk"[\\s\\S]*?\\n\\s*"version"\\s*:\\s*")${escapedOld}(")`) |
| 119 | + |
| 120 | + const before = lock |
| 121 | + lock = lock.replace(r1, `$1${newVersion}$2`) |
| 122 | + lock = lock.replace(r2, `$1${newVersion}$2`) |
| 123 | + if (lock === before) { |
| 124 | + throw new Error(`Failed to update package-lock.json version (${oldVersion} -> ${newVersion}).`) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + fs.writeFileSync(LOCK_PATH, lock, 'utf8') |
| 129 | +} |
| 130 | + |
| 131 | +function run(cmd, opts = {}) { |
| 132 | + execSync(cmd, { stdio: 'inherit', cwd: ROOT, ...opts }) |
| 133 | +} |
| 134 | + |
| 135 | +function git(cmd) { |
| 136 | + return execSync(cmd, { stdio: 'pipe', cwd: ROOT }).toString('utf8').trim() |
| 137 | +} |
| 138 | + |
| 139 | +function gitIsInsideRepo() { |
| 140 | + try { |
| 141 | + execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore', cwd: ROOT }) |
| 142 | + return true |
| 143 | + } catch { |
| 144 | + return false |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +function main() { |
| 149 | + const args = parseArgs(process.argv) |
| 150 | + if (args.help) { |
| 151 | + console.log(` |
| 152 | +Usage: |
| 153 | + node scripts/release.js --bump patch|minor|major|x.y.z [--tag] [--publish] [--all] |
| 154 | +
|
| 155 | +Examples: |
| 156 | + npm run release -- --bump patch |
| 157 | + npm run release -- --bump minor --tag |
| 158 | + npm run release -- --bump 2.7.3 --publish |
| 159 | + npm run release -- --bump patch --all |
| 160 | +`.trim()) |
| 161 | + process.exit(0) |
| 162 | + } |
| 163 | + |
| 164 | + if (!gitIsInsideRepo()) { |
| 165 | + throw new Error('Not a git repository. release script needs git.') |
| 166 | + } |
| 167 | + |
| 168 | + const pkg = JSON.parse(fs.readFileSync(PKG_PATH, 'utf8')) |
| 169 | + const oldVersion = pkg.version |
| 170 | + const newVersion = bumpSemver(oldVersion, args.bump) |
| 171 | + |
| 172 | + console.log(`Releasing: ${oldVersion} -> ${newVersion}`) |
| 173 | + |
| 174 | + updatePackageJsonVersion(oldVersion, newVersion) |
| 175 | + updatePackageLockVersion(oldVersion, newVersion) |
| 176 | + |
| 177 | + // Build dist with the bumped version |
| 178 | + run('npm run build') |
| 179 | + |
| 180 | + // Stage + commit (commit message includes the new version) |
| 181 | + if (args.stageAll) run('git add -A') |
| 182 | + else run('git add package.json package-lock.json dist') |
| 183 | + |
| 184 | + const stagedNames = git('git diff --cached --name-only') |
| 185 | + if (!stagedNames) { |
| 186 | + console.log('No staged changes to commit.') |
| 187 | + process.exit(0) |
| 188 | + } |
| 189 | + |
| 190 | + run(`git commit -m "chore(release): v${newVersion}"`) |
| 191 | + |
| 192 | + if (args.tag) { |
| 193 | + // Avoid failing when tag already exists. |
| 194 | + try { |
| 195 | + run(`git tag v${newVersion}`) |
| 196 | + } catch (e) { |
| 197 | + console.log(`Skip git tag v${newVersion} (tag may already exist).`) |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + if (args.publish) { |
| 202 | + run('npm publish --access public') |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +main() |
| 207 | + |
0 commit comments