From e8d1cb1c16abbbf37cbd7155c5f30d945cfbe316 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Mon, 21 Oct 2024 20:26:41 +0300 Subject: [PATCH] localnet-check task --- hardhat.config.ts | 1 + packages/tasks/src/check.ts | 32 ++++++++++++++++++++++++++++++++ packages/tasks/src/index.ts | 1 + 3 files changed, 34 insertions(+) create mode 100644 packages/tasks/src/check.ts diff --git a/hardhat.config.ts b/hardhat.config.ts index e2c90f2..ef8d1c4 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -1,5 +1,6 @@ import "./packages/tasks/src/localnet"; import "./packages/tasks/src/stop"; +import "./packages/tasks/src/check"; import { HardhatUserConfig } from "hardhat/config"; diff --git a/packages/tasks/src/check.ts b/packages/tasks/src/check.ts new file mode 100644 index 0000000..4b9d568 --- /dev/null +++ b/packages/tasks/src/check.ts @@ -0,0 +1,32 @@ +import { task, types } from "hardhat/config"; +import fs from "fs"; +import ansis from "ansis"; + +const LOCALNET_PID_FILE = "./localnet.pid"; + +const localnetCheck = async (args: any) => { + await new Promise((resolve) => setTimeout(resolve, args.delay * 1000)); + + if (!fs.existsSync(LOCALNET_PID_FILE)) { + console.log(ansis.red("Localnet is not running (PID file missing).")); + process.exit(1); + } + + const pid = fs.readFileSync(LOCALNET_PID_FILE, "utf-8").trim(); + + try { + process.kill(Number(pid), 0); + console.log(ansis.green(`Localnet is running (PID: ${pid}).`)); + process.exit(0); + } catch (err) { + console.log(ansis.yellow(`Localnet process (PID: ${pid}) is not running.`)); + process.exit(1); + } +}; + +export const localnetCheckTask = task( + "localnet-check", + "Check if localnet is running" +) + .addParam("delay", "Seconds to wait before checking localnet", 3, types.int) + .setAction(localnetCheck); diff --git a/packages/tasks/src/index.ts b/packages/tasks/src/index.ts index 34737e4..87f5205 100644 --- a/packages/tasks/src/index.ts +++ b/packages/tasks/src/index.ts @@ -1,2 +1,3 @@ export { localnetTask } from "./localnet"; export { localnetStopTask } from "./stop"; +export { localnetCheckTask } from "./check";