const { spawn } = require("child_process");
const path = require("path");
const isWin = /^win/.test(process.platform);
const spawnNpmCommand = !isWin ? "npm" : "cmd";
const spawnNpmArgs = !isWin ? [] : ["/s", "/c", "npm"];

function setupLogs(spawnedProcess: any) {
  spawnedProcess.stdout.on("data", (data: any) => {
    console.log(`stdout: ${data}`);
  });

  spawnedProcess.stderr.on("data", (data: any) => {
    if (!data.toString().toLowerCase().includes("warning")) {
      console.error(`stderr: ${data}`);
    }
  });

  spawnedProcess.on("close", (code: number) => {
    console.log(`child process exited with code ${code}`);
  });
}

const npm = spawn(spawnNpmCommand, [...spawnNpmArgs, "run", "start:client"], {
  cwd: path.join(__dirname, ".."),
});
setupLogs(npm);
