Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"test-space": "npm run mocha test/space.v2.test.ts",
"test-account": "npm run mocha test/account.*.test.ts",
"test-plugin": "npm run mocha test/plugin.test.ts",
"prepublish": "npm run build"
"prepublish": "npm run build",
"release": "node release.mjs"
},
"repository": "github:plhery/node-twitter-api-v2",
"author": "Paul-Louis Hery <[email protected]> (https://x.com/plhery)",
Expand Down
70 changes: 70 additions & 0 deletions release.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();