@kadena/hardhat-chainweb
Version:
Hardhat plugin for Kadena's Chainweb network
52 lines • 2.11 kB
JavaScript
;
/* *************************************************************************** */
/* TODO */
Object.defineProperty(exports, "__esModule", { value: true });
exports.runHardHatNetwork = runHardHatNetwork;
const child_process_1 = require("child_process");
const logger_1 = require("./logger");
const sleep_1 = require("./sleep");
/* *************************************************************************** */
/* Run Hardhat Network */
async function runHardHatNetwork(port, logger) {
const child = (0, child_process_1.spawn)('npx', ['hardhat', 'node', '--port', port.toString()], {
detached: false, // FIXME not sure why this does not work as it should ...
});
let isClosed = false;
const kill = (signal) => {
if (!isClosed) {
isClosed = child.kill(signal);
}
};
const stdoutBuffer = (0, logger_1.streamLogger)(child.stdout, logger.info);
const stderrBuffer = (0, logger_1.streamLogger)(child.stderr, logger.error);
child.on('close', (exitCode) => {
isClosed = true;
if (stdoutBuffer.length > 0) {
logger.info(stdoutBuffer);
}
if (stderrBuffer.length > 0) {
logger.error(stderrBuffer);
}
if (exitCode === null) {
logger.info(`terminated with code ${exitCode}`);
}
else if (exitCode != 0) {
logger.error(`failed with code ${exitCode}`);
throw new Error(`hardhat ${port} failed with code ${exitCode}`);
}
});
await new Promise((resolve) => {
child.on('spawn', resolve);
});
// kill child on exit
process.on('exit', () => kill(0));
process.on('SIGINT', () => kill('SIGINT'));
process.on('uncaughtException', () => kill('SIGABRT'));
// FIXME wait for proper messages and return an event if this triggered
// actually, we may just block runHardHatNetwork until it's ready...
logger.info(`wait 2 second for hardhat network to start`);
await (0, sleep_1.sleep)(2000);
return { kill, pid: child.pid };
}
//# sourceMappingURL=runHardhatNetwork.js.map