UNPKG

@moonsong-labs/moonwall-cli

Version:

Testing framework for the Moon family of projects

70 lines (68 loc) 2.19 kB
// src/internal/localNode.ts import { spawn } from "child_process"; import chalk from "chalk"; import Debug from "debug"; import { existsSync } from "fs"; var debugNode = Debug("global:node"); async function launchNode(cmd, args, name) { if (cmd.includes("moonbeam") && !existsSync(cmd)) { throw new Error( `No binary file found at location: ${cmd} Are you sure your ${chalk.bgWhiteBright.blackBright( "moonwall.config.json" )} file has the correct "binPath" in launchSpec?` ); } let runningNode; const onProcessExit = () => { runningNode && runningNode.kill(); }; const onProcessInterrupt = () => { runningNode && runningNode.kill(); }; process.once("exit", onProcessExit); process.once("SIGINT", onProcessInterrupt); runningNode = spawn(cmd, args); runningNode.once("exit", () => { process.removeListener("exit", onProcessExit); process.removeListener("SIGINT", onProcessInterrupt); debugNode(`Exiting dev node: ${name}`); }); runningNode.on("error", (err) => { if (err.errno == "ENOENT") { console.error( `\x1B[31mMissing Moonbeam binary at(${cmd}). Please compile the Moonbeam project\x1B[0m` ); } else { console.error(err); } process.exit(1); }); const binaryLogs = []; await new Promise((resolve, reject) => { const timer = setTimeout(() => { console.error(chalk.redBright("Failed to start Moonbeam Test Node.")); console.error(`Command: ${cmd} ${args.join(" ")}`); console.error(`Logs:`); console.error(binaryLogs.map((chunk) => chunk.toString()).join("\n")); reject("Failed to launch node"); }, 1e4); const onData = async (chunk) => { debugNode(chunk.toString()); binaryLogs.push(chunk); if (chunk.toString().match(/Development Service Ready/) || chunk.toString().match(/ RPC listening on port/)) { clearTimeout(timer); runningNode.stderr.off("data", onData); runningNode.stdout.off("data", onData); resolve(); } }; runningNode.stderr.on("data", onData); runningNode.stdout.on("data", onData); }); return runningNode; } export { launchNode };