#!/usr/bin/env node

// LocalLendables v0.88 Production Server
// Uses tsx to run TypeScript without compilation

process.env.NODE_ENV = process.env.NODE_ENV || 'production';
process.env.NODE_OPTIONS = (process.env.NODE_OPTIONS || '') + ' --max-old-space-size=512';

import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

console.log('🚀 Starting LocalLendables v0.88...');

const serverPath = join(__dirname, 'server', 'index.ts');
const tsx = spawn('npx', ['tsx', serverPath], {
  stdio: 'inherit',
  env: { ...process.env }
});

tsx.on('error', (error) => {
  console.error('❌ Server failed:', error);
  process.exit(1);
});

tsx.on('exit', (code) => {
  process.exit(code);
});

process.on('SIGTERM', () => tsx.kill('SIGTERM'));
process.on('SIGINT', () => tsx.kill('SIGINT'));
