Skip to content

Commit

Permalink
Use localnet.json containing both PID and addresses (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Nov 8, 2024
1 parent a49f37f commit be6e0c0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
9 changes: 5 additions & 4 deletions packages/tasks/src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { task, types } from "hardhat/config";
import fs from "fs";
import ansis from "ansis";

const LOCALNET_PID_FILE = "./localnet.pid";
const LOCALNET_JSON_FILE = "./localnet.json";

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)."));
if (!fs.existsSync(LOCALNET_JSON_FILE)) {
console.log(ansis.red("Localnet is not running (JSON file missing)."));
process.exit(1);
}

const pid = fs.readFileSync(LOCALNET_PID_FILE, "utf-8").trim();
const jsonData = JSON.parse(fs.readFileSync(LOCALNET_JSON_FILE, "utf-8"));
const pid = jsonData.pid;

try {
process.kill(Number(pid), 0);
Expand Down
12 changes: 8 additions & 4 deletions packages/tasks/src/localnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ansis from "ansis";
import fs from "fs";
import { confirm } from "@inquirer/prompts";

const LOCALNET_PID_FILE = "./localnet.pid";
const LOCALNET_JSON_FILE = "./localnet.json";

const killProcessOnPort = async (port: number, forceKill: boolean) => {
try {
Expand Down Expand Up @@ -83,8 +83,8 @@ const localnet = async (args: any) => {
if (anvilProcess) {
anvilProcess.kill();
}
if (fs.existsSync(LOCALNET_PID_FILE)) {
fs.unlinkSync(LOCALNET_PID_FILE);
if (fs.existsSync(LOCALNET_JSON_FILE)) {
fs.unlinkSync(LOCALNET_JSON_FILE);
}
};

Expand Down Expand Up @@ -130,7 +130,11 @@ const localnet = async (args: any) => {

console.table(zetaAddresses);

fs.writeFileSync(LOCALNET_PID_FILE, process.pid.toString(), "utf-8");
fs.writeFileSync(
LOCALNET_JSON_FILE,
JSON.stringify({ pid: process.pid, addresses: addr }, null, 2),
"utf-8"
);
} catch (error: any) {
console.error(ansis.red`Error initializing localnet: ${error}`);
cleanup();
Expand Down
15 changes: 8 additions & 7 deletions packages/tasks/src/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { task } from "hardhat/config";
import fs from "fs";
import ansis from "ansis";

const LOCALNET_PID_FILE = "./localnet.pid";
const LOCALNET_JSON_FILE = "./localnet.json";

const localnetStop = async (args: any) => {
if (!fs.existsSync(LOCALNET_PID_FILE)) {
console.log(ansis.red("Localnet is not running or PID file is missing."));
if (!fs.existsSync(LOCALNET_JSON_FILE)) {
console.log(ansis.red("Localnet is not running or JSON file is missing."));
return;
}

const pid = fs.readFileSync(LOCALNET_PID_FILE, "utf-8").trim();
const jsonData = JSON.parse(fs.readFileSync(LOCALNET_JSON_FILE, "utf-8"));
const pid = jsonData.pid;

try {
process.kill(Number(pid), 0); // check that the process is running
Expand All @@ -23,10 +24,10 @@ const localnetStop = async (args: any) => {
} catch (err) {
console.log(ansis.yellow(`Localnet process (PID: ${pid}) is not running.`));
try {
fs.unlinkSync(LOCALNET_PID_FILE);
console.log(ansis.green("Localnet PID file deleted."));
fs.unlinkSync(LOCALNET_JSON_FILE);
console.log(ansis.green("Localnet JSON file deleted."));
} catch (err) {
console.error(ansis.red(`Failed to delete PID file: ${err}`));
console.error(ansis.red(`Failed to delete JSON file: ${err}`));
}
}
};
Expand Down

0 comments on commit be6e0c0

Please sign in to comment.