Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto
Something went wrong. Try again.
3.9 kB · 104 lines
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105import { execFileSync } from 'node:child_process'import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'import { tmpdir } from 'node:os'import { join } from 'node:path'import { afterEach, describe, expect, it } from 'vitest'
const script = join(import.meta.dirname, 'version-bump.mjs')
let dirs = []afterEach(() => { for (const dir of dirs) rmSync(dir, { recursive: true, force: true }) dirs = []})
const git = (dir, ...args) => execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', ...args], { cwd: dir, encoding: 'utf8' })
function repo(version = '0.1.0') { const dir = mkdtempSync(join(tmpdir(), 'version-bump-')) dirs.push(dir) const json = (data) => JSON.stringify(data, null, 2) + '\n' writeFileSync(join(dir, 'package.json'), json({ name: 't', version })) writeFileSync( join(dir, 'package-lock.json'), json({ name: 't', version, lockfileVersion: 3, packages: { '': { name: 't', version } } }), ) mkdirSync(join(dir, 'public')) writeFileSync( join(dir, 'public', 'manifest.json'), `{\n "manifest_version": 3,\n "version": "${version}",\n "matches": ["http://*/*", "https://*/*"]\n}\n`, ) git(dir, 'init', '-q') git(dir, 'add', '-A') git(dir, 'commit', '-q', '-m', 'seed') return dir}
const commit = (dir, message) => git(dir, 'commit', '-q', '--allow-empty', '-m', message)const run = (dir) => execFileSync(process.execPath, [script], { cwd: dir, encoding: 'utf8' })
const versions = (dir) => ({ pkg: JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')).version, lock: JSON.parse(readFileSync(join(dir, 'package-lock.json'), 'utf8')), manifest: JSON.parse(readFileSync(join(dir, 'public', 'manifest.json'), 'utf8')).version, staged: JSON.parse(git(dir, 'show', ':public/manifest.json')).version,})
describe('version-bump.mjs', () => { it('stages the highest bump the commits since the last version change call for', () => { const dir = repo('0.1.2') commit(dir, 'fix: close the leak') commit(dir, 'feat: add a thing') commit(dir, 'chore: tidy up') run(dir) const v = versions(dir) expect(v.pkg).toBe('0.2.0') expect(v.lock.version).toBe('0.2.0') expect(v.lock.packages[''].version).toBe('0.2.0') expect(v.manifest).toBe('0.2.0') expect(v.staged).toBe('0.2.0') // hand formatting survives: the inline array is not exploded expect(readFileSync(join(dir, 'public', 'manifest.json'), 'utf8')).toContain( '"matches": ["http://*/*", "https://*/*"]', ) })
it('stages a major bump on ! and on a BREAKING CHANGE footer', () => { const bang = repo() commit(bang, 'feat!: drop the old storage schema') run(bang) expect(versions(bang).pkg).toBe('1.0.0')
const footer = repo() commit(footer, 'fix: rework storage\n\nBREAKING CHANGE: old records are dropped') run(footer) expect(versions(footer).pkg).toBe('1.0.0') })
it('does nothing when no commit since the anchor releases', () => { const dir = repo() commit(dir, 'chore: tidy up') commit(dir, "Merge branch 'claude/thing'") commit(dir, 'not a conventional header') expect(run(dir)).toContain('nothing to do') expect(versions(dir).pkg).toBe('0.1.0') })
it('anchors on the last version change, not the whole history', () => { const dir = repo() commit(dir, 'feat: first release worth of work') run(dir) git(dir, 'commit', '-q', '-m', 'chore(release): v0.2.0') commit(dir, 'fix: a follow-up') run(dir) expect(versions(dir).pkg).toBe('0.2.1') })
it('refuses to run over pending edits to the release files', () => { const dir = repo() commit(dir, 'feat: add a thing') writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 't', version: '0.1.0', extra: true }, null, 2)) expect(() => run(dir)).toThrow() })})