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