import { spawn } from 'node:child_process';

const mode = process.argv[2] || 'development';
const watch = process.argv.includes('--watch');
const command = watch ? 'nodemon' : 'node';
const args = watch ? ['server/index.js'] : ['server/index.js'];

const child = spawn(command, args, {
  stdio: 'inherit',
  shell: process.platform === 'win32',
  env: {
    ...process.env,
    NODE_ENV: mode
  }
});

child.on('exit', (code, signal) => {
  if (signal) process.kill(process.pid, signal);
  process.exit(code ?? 0);
});
