import { spawn } from 'node:child_process';
import path from 'path';

const mode = process.argv[2] || 'development';
const watch = process.argv.includes('--watch');
const args = watch
  ? [path.join('node_modules', 'nodemon', 'bin', 'nodemon.js'), path.join('server', 'index.js')]
  : [path.join('server', 'index.js')];

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

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