Skip to content

Commit

Permalink
versioning scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
haarg committed Oct 12, 2024
1 parent d52820c commit 9f5e3b9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "mocha",
"build": "./build-release.mjs"
"build": "./build-release.mjs",
"preversion": "npm test",
"version": "npm run build && ./prep-changelog && git add -A dist CHANGELOG.md",
"postversion": "git push origin HEAD && git push origin v$(npm pkg get version --workspaces=false | tr -d \\\") && git push -f origin $(./retag)"
},
"repository": {
"type": "git",
Expand Down
27 changes: 27 additions & 0 deletions prep-changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

import { readFile, writeFile, rename } from 'node:fs/promises';
import process from 'node:process';

async function main() {
const version = JSON.parse(await readFile('./package.json', { encoding: 'utf8' })).version;
const changesFile = 'CHANGELOG.md';
const changes = await readFile(changesFile, { encoding: 'utf8' });
const unreleasedRx = /^## Unreleased\n/m;
if (!changes.match(unreleasedRx)) {
console.error("Unable to find Unreleased section!");
process.exit(1);
}
if (changes.match(new RegExp(`^## ${version.replace('.', '\\.')} `, 'm'))) {
console.error(`${version} section already exists!`);
process.exit(1);
}
const date = new Date();
const dateString = [date.getUTCFullYear(), date.getUTCMonth()+1, date.getUTCDate()].map(d => d.toString().padStart(2, '0')).join('-');
const newChanges = changes.replace(/^## Unreleased\n/m, `## ${version} (${dateString})\n`);
const newFile = `CHANGELOG.md.${process.pid}`;
await writeFile(newFile, newChanges, { encoding: 'utf8' });
await rename(newFile, changesFile);
}

main();
42 changes: 42 additions & 0 deletions retag
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env node

import process from 'node:process';
import child_process from 'node:child_process';
import { promisify } from 'node:util';
import { readFile } from 'node:fs/promises';

const execFile = promisify(child_process.execFile);

async function main() {
let version;
if (process.argv.length == 2) {
const content = await readFile('./package.json', { encoding: 'utf8' });
version = JSON.parse(content).version;
}
else if (process.argv.length == 3) {
version = process.argv[2];
}
else {
throw new Error(`Invalid args ${process.argv.join(', ')}`);
}

const res = version.match(/^v?((([0-9]+)\.[0-9]+)\.[0-9]+)$/);
if (res === null) {
throw new Error(`Invalid version ${version}`);
}

const [ , patch, minor, major ] = res;

const { stdout, stderr } = await execFile('git', ['rev-parse', '--verify', '--quiet', `v${patch}`]);
if (stderr.length) {
process.stderr.write(stderr);
}
const tagRef = stdout.trim();

for (const tag of [minor, major]) {
await execFile('git', ['update-ref', `refs/tags/v${tag}`, tagRef]);
process.stdout.write(`v${tag}\n`);
}
}

main();

0 comments on commit 9f5e3b9

Please sign in to comment.