|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execSync } from 'node:child_process'; |
| 3 | +import { readFileSync, writeFileSync } from 'node:fs'; |
| 4 | + |
| 5 | +function run(cmd, options = {}) { |
| 6 | + return execSync(cmd, { encoding: 'utf8', ...options }).trim(); |
| 7 | +} |
| 8 | + |
| 9 | +const bump = process.argv[2]; |
| 10 | +if (!['major', 'minor', 'patch'].includes(bump)) { |
| 11 | + console.error('Usage: node release.mjs <major|minor|patch>'); |
| 12 | + process.exit(1); |
| 13 | +} |
| 14 | + |
| 15 | +async function main() { |
| 16 | + const latestTag = run('git describe --tags --abbrev=0'); |
| 17 | + const commitLines = run(`git log ${latestTag}..HEAD --pretty=format:%s`)\ |
| 18 | + .split('\n') |
| 19 | + .filter(Boolean); |
| 20 | + |
| 21 | + const entries = []; |
| 22 | + for (const line of commitLines) { |
| 23 | + const match = line.match(/^(.*) \(#(\d+)\)$/); |
| 24 | + if (!match) continue; |
| 25 | + const [, description, pr] = match; |
| 26 | + try { |
| 27 | + const res = await fetch(`https://api.github.com/repos/plhery/node-twitter-api-v2/pulls/${pr}`); |
| 28 | + const json = await res.json(); |
| 29 | + const user = json.user?.login || 'unknown'; |
| 30 | + entries.push(`- ${description} #${pr} (@${user})`); |
| 31 | + } catch { |
| 32 | + entries.push(`- ${description} #${pr}`); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + if (entries.length === 0) { |
| 37 | + console.error('No commit entries found.'); |
| 38 | + process.exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + const pkg = JSON.parse(readFileSync('package.json', 'utf8')); |
| 42 | + const [maj, min, pat] = pkg.version.split('.').map(Number); |
| 43 | + let newVersion; |
| 44 | + switch (bump) { |
| 45 | + case 'major': |
| 46 | + newVersion = `${maj + 1}.0.0`; |
| 47 | + break; |
| 48 | + case 'minor': |
| 49 | + newVersion = `${maj}.${min + 1}.0`; |
| 50 | + break; |
| 51 | + default: |
| 52 | + newVersion = `${maj}.${min}.${pat + 1}`; |
| 53 | + } |
| 54 | + |
| 55 | + pkg.version = newVersion; |
| 56 | + writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n'); |
| 57 | + |
| 58 | + const changelog = readFileSync('changelog.md', 'utf8'); |
| 59 | + const newChangelog = `${newVersion}\n------\n${entries.join('\n')}\n\n${changelog}`; |
| 60 | + writeFileSync('changelog.md', newChangelog); |
| 61 | + |
| 62 | + execSync('npm install --package-lock-only', { stdio: 'inherit' }); |
| 63 | + |
| 64 | + run('git add package.json package-lock.json changelog.md'); |
| 65 | + run(`git commit -m "upgrade to ${newVersion}"`); |
| 66 | + |
| 67 | + console.log(`Release ${newVersion} ready.`); |
| 68 | +} |
| 69 | + |
| 70 | +await main(); |
0 commit comments