Adds src/version.js, bump-version.js and a pre-commit hook that bumps the version in commits that change the app. Closes #15 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
59 lines
2.8 KiB
JavaScript
59 lines
2.8 KiB
JavaScript
// Moves src/version.js to the next version (today's date, next letter).
|
|
// npm run bump bump now
|
|
// node bump-version.js --staged
|
|
// used by the git pre-commit hook: bump only if the staged
|
|
// changes touch the app and the version was not already bumped
|
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { dirname, join } from 'node:path';
|
|
import { nextVersion } from './src/version.js';
|
|
|
|
const root = dirname(fileURLToPath(import.meta.url));
|
|
const versionFile = join(root, 'src', 'version.js');
|
|
const VERSION_LINE = /APP_VERSION = '([^']+)'/;
|
|
|
|
// Paths that change what the built app does.
|
|
export const isAppPath = (p) => (p.startsWith('src/') && p !== 'src/version.js') || p === 'build.js';
|
|
|
|
// A commit gets a new version when it touches the app and the version in the
|
|
// commit is still the one in HEAD (a new or hand-edited version counts as bumped).
|
|
export function needsBump(stagedPaths, headVersion, stagedVersion) {
|
|
return stagedPaths.some(isAppPath) && headVersion === stagedVersion;
|
|
}
|
|
|
|
const git = (...args) => execFileSync('git', args, { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
const versionIn = (text) => { const m = VERSION_LINE.exec(text); return m ? m[1] : null; };
|
|
const tryGit = (...args) => { try { return git(...args); } catch { return null; } };
|
|
|
|
function writeVersion(from, to) {
|
|
const src = readFileSync(versionFile, 'utf8');
|
|
const out = src.replace(`APP_VERSION = '${from}'`, `APP_VERSION = '${to}'`);
|
|
if (out === src) throw new Error('Could not find APP_VERSION in src/version.js');
|
|
writeFileSync(versionFile, out);
|
|
}
|
|
|
|
function main(argv) {
|
|
if (argv.includes('--staged')) {
|
|
const staged = git('diff', '--cached', '--name-only').split('\n').filter(Boolean);
|
|
const stagedText = tryGit('show', ':src/version.js');
|
|
const headText = tryGit('show', 'HEAD:src/version.js');
|
|
if (stagedText === null) return;
|
|
const current = versionIn(stagedText);
|
|
if (!needsBump(staged, headText === null ? null : versionIn(headText), current)) return;
|
|
let next;
|
|
try { next = nextVersion(current, new Date()); } catch (e) { return console.warn('Version not bumped: ' + e.message); }
|
|
writeVersion(versionIn(readFileSync(versionFile, 'utf8')), next);
|
|
git('add', 'src/version.js');
|
|
return console.log(`Ver ${current} -> Ver ${next}`);
|
|
}
|
|
const current = versionIn(readFileSync(versionFile, 'utf8'));
|
|
const next = nextVersion(current, new Date());
|
|
writeVersion(current, next);
|
|
console.log(`Ver ${current} -> Ver ${next}`);
|
|
}
|
|
|
|
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
try { main(process.argv.slice(2)); } catch (e) { console.error(e.message); process.exit(1); }
|
|
}
|