Files
hope-events/backend/src/index.js
T
joshuaandClaude Opus 5.5 4ca896130b TypeScript backend and /api/v2 foundations (1.51.0)
- tsconfig (allowJs, strict .ts), npm run build -> dist/, typecheck, dev via tsx, ts-jest.
- src/index.js is now a launcher (dist build when it matches, else tsx); server moved to app.js.
- /api/v2: defineRoute with zod -> request validation, response stripping, OpenAPI 3.1 at
  /docs/v2; one error shape and one paged-list shape. First endpoints: events, event, me,
  my registrations.
- deploy.sh/install.sh build the backend; rollback to older releases skips the build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
2026-10-29 08:48:54 +02:00

35 lines
1.4 KiB
JavaScript

// Starts the backend (the server itself is app.js).
//
// Since 1.51.0 part of the backend is TypeScript. Production runs the compiled build in dist/
// (`npm run build`, done by scripts/deploy.sh), but every install's PM2 config points here, at
// src/index.js — so with plain Node this hands over to dist/ when its build matches this
// version, and otherwise compiles the TypeScript on the fly with tsx (e.g. right after an
// update made by an older deploy script, which didn't build). Under tsx or Jest, which load
// .ts files themselves, and when this file is the one in dist/, it just loads app.js.
const fs = require('fs');
const path = require('path');
function builtApp() {
const dist = path.join(__dirname, '..', 'dist');
try {
const built = JSON.parse(fs.readFileSync(path.join(dist, 'build.json'), 'utf8'));
if (built.version === require('../package.json').version) return path.join(dist, 'app.js');
} catch {
// no build yet
}
return null;
}
if (path.basename(__dirname) === 'src' && !require.extensions['.ts']) {
const built = builtApp();
if (built) {
module.exports = require(built);
} else {
console.warn('[startup] No build for this version in dist/ — compiling TypeScript as it loads (run `npm run build`).');
require('tsx/cjs');
module.exports = require('./app');
}
} else {
module.exports = require('./app');
}