// 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'); }