perplexity-hackathon-LawMitra / perplexity_hackathon / demo / scripts / coordinator.js
coordinator.js
Raw
import { spawn } from 'child_process';
import WaitOn from 'wait-on';

const waitOn = new WaitOn();

async function startServer() {
  return new Promise((resolve) => {
    const server = spawn('npm', ['run', 'demo:server'], {
      stdio: 'inherit',
      shell: true
    });

    // Wait for server to be ready
    waitOn({
      resources: ['tcp:3001'],
      timeout: 10000,
    }).then(() => {
      console.log('Server is ready');
      resolve(server);
    });
  });
}

async function startScenario(scenario, delay = 0) {
  if (delay > 0) {
    await new Promise(resolve => setTimeout(resolve, delay * 1000));
  }
  
  const proc = spawn('npm', ['run', `demo:${scenario}`], {
    stdio: 'inherit',
    shell: true
  });

  return proc;
}

async function runDemo() {
  try {
    const server = await startServer();
    
    // Start scenarios with delays
    const user = await startScenario('user');
    const ngo = await startScenario('ngo', 2);
    const advocate = await startScenario('advocate', 4);

    // Handle process termination
    process.on('SIGINT', () => {
      server.kill();
      user.kill();
      ngo.kill();
      advocate.kill();
      process.exit();
    });

  } catch (error) {
    console.error('Error running demo:', error);
    process.exit(1);
  }
}

runDemo();