From 4b17ca00db1cc150655e1cc050b5c4432c5e02d7 Mon Sep 17 00:00:00 2001 From: Brian Hursey Date: Sat, 28 Nov 2026 15:02:00 +0000 Subject: [PATCH] Skip the pre-deploy build when only test files changed. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit predeploy was a shell chain that always rebuilt, so a test-only edit paid for a full Vite build. It is now scripts/predeploy.mjs: typecheck, tests and the render check always run, and the build is skipped when a content hash of the bundle inputs (src and public minus test files, plus the Vite/TS/package config) matches the last successful build. The signature lives in node_modules/.cache, so it is gitignored and never rsynced to the server. 🤖 Generated with Codebuff Co-Authored-By: Codebuff --- README.md | 2 +- package.json | 2 +- scripts/predeploy.mjs | 95 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 scripts/predeploy.mjs diff --git a/README.md b/README.md index 743fbd4..561299c 100644 --- a/README.md +++ b/README.md @@ -957,7 +957,7 @@ positions. Camera-lens defaults require both a normalised camera and lens name t | `npm run typecheck` | `tsc --noEmit` | | `npm test` | Vitest, one pass then exit | | `npm run check:render` | Headless Chrome check that the 16-bit sidecar and JPEG proxy upload in the same row order | -| `npm run predeploy` | Typecheck, unit suite, render check, then build — run this before pushing a deploy | +| `npm run predeploy` | Typecheck, unit suite, render check, then build — run this before pushing a deploy; the build is skipped when only test files changed since the last build | Conventions worth knowing before changing code: diff --git a/package.json b/package.json index a8ce924..89271f8 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "typecheck": "tsc --noEmit", "test": "vitest run", "check:render": "node scripts/render-check.mjs", - "predeploy": "npm run typecheck && npm test && npm run check:render && npm run build", + "predeploy": "node scripts/predeploy.mjs", "build:lens-db": "node scripts/build-lens-db.mjs" }, "dependencies": { diff --git a/scripts/predeploy.mjs b/scripts/predeploy.mjs new file mode 100644 index 0000000..df7c5ea --- /dev/null +++ b/scripts/predeploy.mjs @@ -0,0 +1,95 @@ +#!/usr/bin/env node +/** + * Pre-deploy gate: typecheck, unit suite, render check, then the production + * build -- skipped when nothing that feeds the bundle has changed since the + * last successful build, so editing only a test file does not rebuild dist. + * + * The signature is stored under node_modules/.cache: that directory is + * gitignored and excluded from the deploy rsync, so it is per-checkout state + * and never travels to the server. + */ +import { createHash } from 'node:crypto'; +import { execFileSync } from 'node:child_process'; +import { + existsSync, + mkdirSync, + readFileSync, + readdirSync, + statSync, + writeFileSync, +} from 'node:fs'; +import path from 'node:path'; + +const ROOT = process.cwd(); +const CACHE = path.join(ROOT, 'node_modules', '.cache', 'luminary-predeploy.json'); + +/** Vite inputs: everything the bundle is built from, plus the config it reads. */ +const INPUTS = [ + 'src', + 'public', + 'index.html', + 'vite.config.ts', + 'tsconfig.json', + 'package.json', + 'package-lock.json', +]; + +/** Test files never reach the bundle, so their edits must not force a rebuild. */ +const NOT_BUNDLED = /(^|\/)(__tests__|__mocks__|__fixtures__)(\/|$)|\.(test|spec)\.[cm]?[jt]sx?$/; + +function walk(target) { + if (!existsSync(target)) return []; + if (statSync(target).isFile()) return [target]; + return readdirSync(target, { withFileTypes: true }).flatMap((entry) => + walk(path.join(target, entry.name)), + ); +} + +function inputSignature() { + const files = INPUTS.flatMap((entry) => walk(entry)) + .map((file) => path.relative(ROOT, file).split(path.sep).join('/')) + .filter((file) => !NOT_BUNDLED.test(file)) + .sort(); + const hash = createHash('sha256'); + for (const file of files) { + hash.update(file); + hash.update('\0'); + hash.update(readFileSync(path.join(ROOT, file))); + hash.update('\0'); + } + return { count: files.length, digest: hash.digest('hex') }; +} + +function run(name, args) { + console.log(`\npredeploy: ${name}`); + try { + execFileSync('npm', args, { cwd: ROOT, stdio: 'inherit' }); + } catch { + console.error(`\npredeploy FAILED at ${name}.`); + process.exit(1); + } +} + +const signature = inputSignature(); +const previous = existsSync(CACHE) ? JSON.parse(readFileSync(CACHE, 'utf8')) : null; +const upToDate = + existsSync(path.join(ROOT, 'dist', 'index.html')) && previous?.digest === signature.digest; + +run('typecheck', ['run', 'typecheck']); +run('tests', ['run', 'test']); +run('render check', ['run', 'check:render']); + +if (upToDate) { + console.log( + `\npredeploy: build skipped - the ${signature.count} bundle inputs are unchanged since the last build.`, + ); +} else { + run('build', ['run', 'build']); + mkdirSync(path.dirname(CACHE), { recursive: true }); + writeFileSync( + CACHE, + `${JSON.stringify({ digest: signature.digest, count: signature.count, at: new Date().toISOString() }, null, 2)}\n`, + ); +} + +console.log('\npredeploy PASSED.');