|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Release wrapper. Bumps version, commits, tags, pushes — and verifies a |
| 4 | + * CHANGELOG entry exists for the new version *before* doing any of that, so |
| 5 | + * the GitHub Actions workflow can use it as the release body. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * npm run release patch # 1.3.38 → 1.3.39 |
| 9 | + * npm run release minor # 1.3.38 → 1.4.0 |
| 10 | + * npm run release major # 1.3.38 → 2.0.0 |
| 11 | + * npm run release 1.4.2 # explicit version |
| 12 | + */ |
| 13 | + |
| 14 | +import { execSync } from 'node:child_process'; |
| 15 | +import { readFileSync } from 'node:fs'; |
| 16 | +import { fileURLToPath } from 'node:url'; |
| 17 | +import { dirname, join } from 'node:path'; |
| 18 | + |
| 19 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 20 | +const repoRoot = join(__dirname, '..'); |
| 21 | + |
| 22 | +const arg = process.argv[2]; |
| 23 | +if (!arg) { |
| 24 | + console.error('Usage: npm run release <patch|minor|major|x.y.z>'); |
| 25 | + process.exit(1); |
| 26 | +} |
| 27 | + |
| 28 | +// ── 1. Working tree must be clean ────────────────────────────────────────── |
| 29 | +const dirty = execSync('git status --porcelain', { cwd: repoRoot, encoding: 'utf8' }).trim(); |
| 30 | +if (dirty) { |
| 31 | + console.error('❌ Working tree has uncommitted changes:\n'); |
| 32 | + console.error(dirty); |
| 33 | + console.error('\nCommit or stash before releasing.'); |
| 34 | + process.exit(1); |
| 35 | +} |
| 36 | + |
| 37 | +// ── 2. Compute next version ──────────────────────────────────────────────── |
| 38 | +const pkg = JSON.parse(readFileSync(join(repoRoot, 'package.json'), 'utf8')); |
| 39 | +const current = pkg.version; |
| 40 | + |
| 41 | +function bump(version, kind) { |
| 42 | + const m = version.match(/^(\d+)\.(\d+)\.(\d+)$/); |
| 43 | + if (!m) throw new Error(`Cannot parse current version: ${version}`); |
| 44 | + let [, major, minor, patch] = m.map(Number); |
| 45 | + if (kind === 'major') { major++; minor = 0; patch = 0; } |
| 46 | + else if (kind === 'minor') { minor++; patch = 0; } |
| 47 | + else if (kind === 'patch') { patch++; } |
| 48 | + else throw new Error(`Unknown bump kind: ${kind}`); |
| 49 | + return `${major}.${minor}.${patch}`; |
| 50 | +} |
| 51 | + |
| 52 | +let next; |
| 53 | +if (/^\d+\.\d+\.\d+$/.test(arg)) { |
| 54 | + next = arg; |
| 55 | +} else if (['patch', 'minor', 'major'].includes(arg)) { |
| 56 | + next = bump(current, arg); |
| 57 | +} else { |
| 58 | + console.error(`❌ Invalid version arg: ${arg} (expected patch|minor|major|x.y.z)`); |
| 59 | + process.exit(1); |
| 60 | +} |
| 61 | + |
| 62 | +console.log(`Releasing ${current} → ${next}\n`); |
| 63 | + |
| 64 | +// ── 3. Verify CHANGELOG has section for the new version ──────────────────── |
| 65 | +const changelog = readFileSync(join(repoRoot, 'CHANGELOG.md'), 'utf8'); |
| 66 | +const sectionPattern = new RegExp(`^## \\[${next.replace(/\./g, '\\.')}\\]`, 'm'); |
| 67 | +if (!sectionPattern.test(changelog)) { |
| 68 | + const today = new Date().toISOString().slice(0, 10); |
| 69 | + console.error(`❌ CHANGELOG.md is missing a section for v${next}.\n`); |
| 70 | + console.error(`Add this near the top of CHANGELOG.md (above the previous version):\n`); |
| 71 | + console.error(` ## [${next}] — ${today}\n`); |
| 72 | + console.error(` ### Added`); |
| 73 | + console.error(` - …\n`); |
| 74 | + console.error(` ### Fixed`); |
| 75 | + console.error(` - …\n`); |
| 76 | + console.error(`Then commit it and re-run the release.`); |
| 77 | + process.exit(1); |
| 78 | +} |
| 79 | +console.log(`✓ CHANGELOG.md has section for v${next}`); |
| 80 | + |
| 81 | +// ── 4. Run tests ─────────────────────────────────────────────────────────── |
| 82 | +console.log('\nRunning tests…'); |
| 83 | +try { |
| 84 | + execSync('npm test', { cwd: repoRoot, stdio: 'inherit' }); |
| 85 | +} catch { |
| 86 | + console.error('\n❌ Tests failed. Aborting release.'); |
| 87 | + process.exit(1); |
| 88 | +} |
| 89 | + |
| 90 | +// ── 5. Bump version (creates commit + tag) ───────────────────────────────── |
| 91 | +// `npm version <x.y.z>` mutates package.json, commits with message "x.y.z", |
| 92 | +// and tags as v<x.y.z>. We pass the explicit version so npm doesn't compute it. |
| 93 | +console.log(`\nBumping package.json and tagging…`); |
| 94 | +execSync(`npm version ${next} -m "%s"`, { cwd: repoRoot, stdio: 'inherit' }); |
| 95 | + |
| 96 | +// ── 6. Push commit and tag ───────────────────────────────────────────────── |
| 97 | +console.log('\nPushing commit…'); |
| 98 | +execSync('git push', { cwd: repoRoot, stdio: 'inherit' }); |
| 99 | +console.log('Pushing tag…'); |
| 100 | +execSync(`git push origin v${next}`, { cwd: repoRoot, stdio: 'inherit' }); |
| 101 | + |
| 102 | +console.log(`\n✅ v${next} released.`); |
| 103 | +console.log(`\nWatch the workflow:`); |
| 104 | +console.log(` gh run watch -R VladoIvankovic/Codeep`); |
| 105 | +console.log(`Or: https://github.com/VladoIvankovic/Codeep/actions`); |
0 commit comments