UNPKG

1.68 kBJavaScriptView Raw
1const { spawn } = require("child_process");
2const waitPort = require("wait-port");
3const isPortFree = require("is-port-free");
4const findFreePorts = require("find-free-ports");
5const { info } = require("simple-output");
6const { promisifyProcess } = require("./utils/process");
7const { completeHandler, errorHandler } = require("./utils/handlers");
8
9async function getOptions() {
10 let command1, command2;
11 const program = require("commander")
12 .arguments("<cmd1> <cmd2>")
13 .option("-p, --port <port>", "process port")
14 .option("-t, --timeout <timeout>", "process timeout")
15 .action((cmd1, cmd2) => ((command1 = cmd1), (command2 = cmd2)))
16 .parse(process.argv);
17
18 const host = "localhost";
19 const port = parseInt(program.port) || (await findFreePorts())[0];
20
21 return { host, port, command1, command2 };
22}
23
24function startProcess(command) {
25 info(`Start npm process - ${command}...`);
26
27 const process = spawn("npm", ["run", command], { stdio: "inherit" });
28 process.on("exit", () => process.kill());
29
30 return promisifyProcess(process);
31}
32
33function connectToPort(options) {
34 const waitPortPromise = waitPort(options).catch(e => {
35 console.log(e);
36 throw new Error("First process didn't start in time");
37 });
38 console.log("\n");
39
40 return waitPortPromise;
41}
42
43(async function main() {
44 const options = await getOptions();
45
46 await isPortFree(options.port).catch(() => {
47 throw new Error(`Port ${options.port} is already in use`);
48 });
49
50 const processPromise = startProcess(options.command1);
51
52 await connectToPort(options);
53 await Promise.race([processPromise, startProcess(options.command2)]);
54
55 completeHandler();
56})().catch(errorHandler);