#!/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. * * Exits via process.exitCode rather than process.exit(): piped stdout is async * and exiting early truncates the failure message. */ 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?$/; class StepFailed extends Error {} 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 { throw new StepFailed(name); } } function main() { 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; try { 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']); } } catch (err) { if (!(err instanceof StepFailed)) throw err; console.error(`\npredeploy FAILED at ${err.message}.`); return 1; } if (!upToDate) { 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.'); return 0; } process.exitCode = main();