diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..50c4863 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,3 @@ +#!/bin/sh +# Bump the app version (src/version.js) when a commit changes the app. +exec node "$(git rev-parse --show-toplevel)/bump-version.js" --staged diff --git a/README.md b/README.md index a732e96..10c8baa 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,9 @@ The passphrase is prompted for (hidden) or read from `FW_PASSPHRASE`. downscaled and re-encoded in the browser) - Visual sequences (numbered pictures with optional text, in any section) are described in `docs/MAINTENANCE.md`. +- `src/version.js` the version shown at the top (`Ver YYMMDDX`); `npm run bump` advances it by hand; + a pre-commit hook (`npm run hooks` once per clone) does it on every commit + that changes the app - `src/print.js` print helpers (secret masking, header layout) - `src/app.js` + `src/style.css` the UI, including the Print header panel - `src/template.js` the HTML skeleton shared by `build.js` and in-page Save diff --git a/build.js b/build.js index aaddecb..6ff4804 100644 --- a/build.js +++ b/build.js @@ -17,7 +17,7 @@ const root = dirname(fileURLToPath(import.meta.url)); const read = (p) => readFileSync(join(root, 'src', p), 'utf8'); // Modules are concatenated (ES `import` lines and `export` keywords dropped) ahead of app.js into one scope. -const MODULES = ['crypto.js', 'images.js', 'model.js', 'ops.js', 'print.js', 'template.js']; +const MODULES = ['crypto.js', 'images.js', 'model.js', 'ops.js', 'print.js', 'template.js', 'version.js']; export function bundleJs() { return MODULES.map((f) => read(f).replace(/^import .*$/gm, '').replace(/^export /gm, '')).concat(read('app.js')).join('\n'); } @@ -79,4 +79,4 @@ async function main() { if (args.data) console.log(`Note: ${args.data} is still plaintext. Keep it out of git and delete it when done.`); } -main().catch((e) => { console.error(e.message); process.exit(1); }); +if (process.argv[1] === fileURLToPath(import.meta.url)) main().catch((e) => { console.error(e.message); process.exit(1); }); diff --git a/bump-version.js b/bump-version.js new file mode 100644 index 0000000..744ba71 --- /dev/null +++ b/bump-version.js @@ -0,0 +1,58 @@ +// 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); } +} diff --git a/docs/MAINTENANCE.md b/docs/MAINTENANCE.md index 2c9203d..a53487f 100644 --- a/docs/MAINTENANCE.md +++ b/docs/MAINTENANCE.md @@ -111,6 +111,25 @@ not part of a record or item, so it sits exactly where you put it. older file in a newer build (see below) still works. **Cancel** and the other editing rules work the same as for records. +## Version number +The top of the page shows the app's version after the "Last update" date, +for example **Ver 261109B**: `261109` is the date (YY MM DD) and the letter is +the iteration that day (A first, B second, up to Z). It is the version of the +*app code*, not of your data, so a file saved from a newer build shows the +newer number. After upgrading an old file (below), check that it changed. + +For the developer: the bump is automatic. A git pre-commit hook +(`.githooks/pre-commit`) moves `src/version.js` to today's date with the next +letter (or A on a new day) in any commit that changes `src/` or `build.js`. +Docs-only and test-only commits don't bump, and a commit that already changes +the version by hand is left alone. Past Z (26 in a day) it warns and keeps Z. +- Once per clone, switch the hook on: `npm run hooks` (sets + `core.hooksPath` to `.githooks`). Until then commits are not bumped. +- `npm run bump` still bumps by hand if you need it. +- The hook adds `src/version.js` to the commit being made. It works with + `git add ... && git commit` and `git commit -a`; with `git commit ` + the bump is left out of that commit, so use the first form. + ## Upgrading the app (new version, same data) Your data is stored encrypted inside each `final-wishes.html`; the app code sits beside it in the same file. A file therefore keeps running the version it diff --git a/package.json b/package.json index a24c529..9f72501 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "scripts": { "build": "node build.js", "test": "node --test", - "seal": "node build.js --data data/seed.json" + "seal": "node build.js --data data/seed.json", + "bump": "node bump-version.js", + "hooks": "git config core.hooksPath .githooks" }, "engines": { "node": ">=20" diff --git a/src/app.js b/src/app.js index 542984a..1ce1f28 100644 --- a/src/app.js +++ b/src/app.js @@ -669,6 +669,7 @@ h('header', { class: 'top' }, h('h1', {}, state.doc.meta.title || 'Final Wishes'), h('span', { class: 'muted' }, 'Last update: ' + state.doc.meta.lastUpdated), + h('span', { class: 'muted', id: 'version', title: 'Version of this app' }, 'Ver ' + APP_VERSION), h('span', { id: 'dirty', class: 'badge tbd', hidden: !state.dirty }, 'Unsaved changes'), h('span', { id: 'status', class: 'muted', role: 'status' }, state.status), btn('Save', () => save(), state.dirty ? 'primary' : ''), diff --git a/src/version.js b/src/version.js new file mode 100644 index 0000000..8b225bf --- /dev/null +++ b/src/version.js @@ -0,0 +1,25 @@ +// The version of the app code, shown at the top of the page: "Ver YYMMDDX". +// YYMMDD is the date, X the iteration that day (A first, B second, ...). +// Bump with `npm run bump` (see bump-version.js). build.js strips `export`. + +export const APP_VERSION = '261109A'; + +const VERSION_RE = /^(\d{6})([A-Z])$/; +export const isVersion = (v) => typeof v === 'string' && VERSION_RE.test(v); + +// YYMMDD for a Date, in local time. +export function versionDate(date) { + const p = (n) => String(n).padStart(2, '0'); + return p(date.getFullYear() % 100) + p(date.getMonth() + 1) + p(date.getDate()); +} + +// The version that follows `current` when released on `date`: the next letter +// on the same day, or A on a new day. +export function nextVersion(current, date) { + if (!isVersion(current)) throw new Error('Not a version: ' + current); + const day = versionDate(date); + if (!current.startsWith(day)) return day + 'A'; + const letter = current[6]; + if (letter === 'Z') throw new Error('Already 26 versions on ' + day + '; wait until tomorrow.'); + return day + String.fromCharCode(letter.charCodeAt(0) + 1); +} diff --git a/test/build.test.js b/test/build.test.js index 6fa70a8..d10a347 100644 --- a/test/build.test.js +++ b/test/build.test.js @@ -74,3 +74,28 @@ test('backupName and passphraseProblem', () => { assert.ok(passphraseProblem('elevenchars')); assert.equal(passphraseProblem('twelve chars!'), ''); }); + +// ---- version ---- +import { APP_VERSION, isVersion, nextVersion, versionDate } from '../src/version.js'; + +test('APP_VERSION is a valid Ver YYMMDDX', () => { + assert.equal(isVersion(APP_VERSION), true); + assert.equal(isVersion('261109B'), true); + for (const bad of ['', '26110B', '2611091', '261109b', '261109AA', null, 5]) assert.equal(isVersion(bad), false, String(bad)); +}); + +test('nextVersion: next letter same day, A on a new day, error past Z', () => { + const d = new Date(2026, 10, 9, 15, 0); // 9 Nov 2026 local + assert.equal(versionDate(d), '261109'); + assert.equal(nextVersion('261109A', d), '261109B'); + assert.equal(nextVersion('261109Y', d), '261109Z'); + assert.throws(() => nextVersion('261109Z', d), /26 versions/); + assert.equal(nextVersion('261108D', d), '261109A'); + assert.equal(nextVersion('261109C', new Date(2027, 0, 2)), '270102A'); + assert.throws(() => nextVersion('nope', d), /Not a version/); +}); + +test('the built page carries the version', async () => { + const { bundleJs } = await import('../build.js'); // importing must not run a build + assert.ok(bundleJs().includes(`APP_VERSION = '${APP_VERSION}'`)); +}); diff --git a/test/bump.test.js b/test/bump.test.js new file mode 100644 index 0000000..f660f6b --- /dev/null +++ b/test/bump.test.js @@ -0,0 +1,54 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { execFileSync, spawnSync } from 'node:child_process'; +import { mkdtempSync, mkdirSync, copyFileSync, writeFileSync, readFileSync, chmodSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { needsBump, isAppPath } from '../bump-version.js'; +import { versionDate } from '../src/version.js'; + +const repo = join(dirname(fileURLToPath(import.meta.url)), '..'); + +test('needsBump: only app changes, and only if the version was not already bumped', () => { + assert.equal(needsBump(['src/app.js'], '261109A', '261109A'), true); + assert.equal(needsBump(['build.js'], '261109A', '261109A'), true); + assert.equal(needsBump(['README.md', 'docs/MAINTENANCE.md', 'test/x.test.js', 'OVERVIEW.md'], '261109A', '261109A'), false); + assert.equal(needsBump(['src/version.js'], '261109A', '261109A'), false); + assert.equal(needsBump(['src/app.js', 'src/version.js'], '261109A', '261109B'), false); // already bumped by hand + assert.equal(needsBump(['src/app.js'], null, '261109A'), false); // first commit of the file + assert.equal(isAppPath('src/style.css'), true); +}); + +const hasGit = spawnSync('git', ['--version']).status === 0; +test('pre-commit hook bumps the version in the commit that changes the app', { skip: !hasGit && 'git not found' }, () => { + const dir = mkdtempSync(join(tmpdir(), 'fw-bump-')); + try { + const run = (...a) => execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', ...a], { cwd: dir, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }); + mkdirSync(join(dir, 'src')); mkdirSync(join(dir, '.githooks')); + for (const f of ['bump-version.js', 'package.json', 'src/version.js', '.githooks/pre-commit']) copyFileSync(join(repo, f), join(dir, f)); + chmodSync(join(dir, '.githooks/pre-commit'), 0o755); + const today = versionDate(new Date()); + const setVersion = (v) => writeFileSync(join(dir, 'src/version.js'), readFileSync(join(dir, 'src/version.js'), 'utf8').replace(/APP_VERSION = '[^']+'/, `APP_VERSION = '${v}'`)); + const version = () => /APP_VERSION = '([^']+)'/.exec(run('show', 'HEAD:src/version.js'))[1]; + run('init', '-q'); run('config', 'core.hooksPath', '.githooks'); + setVersion('200101A'); + writeFileSync(join(dir, 'src/app.js'), '1'); writeFileSync(join(dir, 'README.md'), 'a'); + run('add', '-A'); run('commit', '-q', '-m', 'first'); + assert.equal(version(), '200101A'); // first commit of the file is left alone + + writeFileSync(join(dir, 'README.md'), 'b'); run('add', '-A'); run('commit', '-q', '-m', 'docs only'); + assert.equal(version(), '200101A'); // docs do not bump + + writeFileSync(join(dir, 'src/app.js'), '2'); run('add', '-A'); run('commit', '-q', '-m', 'app change'); + assert.equal(version(), today + 'A'); // new day starts at A, and the bump is inside that commit + assert.equal(run('status', '--porcelain').trim(), ''); + + writeFileSync(join(dir, 'src/app.js'), '3'); run('add', '-A'); run('commit', '-q', '-m', 'app change 2'); + assert.equal(version(), today + 'B'); + + setVersion(today + 'Z'); run('add', '-A'); run('commit', '-q', '-m', 'hand bump'); + writeFileSync(join(dir, 'src/app.js'), '4'); run('add', '-A'); run('commit', '-q', '-m', 'overflow'); + assert.equal(version(), today + 'Z'); // past Z it warns and keeps Z, the commit still succeeds + } finally { rmSync(dir, { recursive: true, force: true }); } +});