Skip to content

Commit

Permalink
stop after init
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Sep 11, 2024
1 parent 2253d2b commit c7ec285
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions packages/tasks/src/localnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,21 @@ const killProcessOnPort = async (port: number, forceKill: boolean) => {
};

const localnet = async (args: any) => {
const port = args.port || 8545;
const forceKill = args.forceKill || false;

await killProcessOnPort(port, forceKill);
await killProcessOnPort(args.port, args.forceKill);

if (args.anvil !== "")
console.log(`Starting anvil on port ${port} with args: ${args.anvil}`);
console.log(`Starting anvil on port ${args.port} with args: ${args.anvil}`);

const anvilProcess = exec(
`anvil --auto-impersonate --port ${port} ${args.anvil}`
`anvil --auto-impersonate --port ${args.port} ${args.anvil}`
);

if (anvilProcess.stdout && anvilProcess.stderr) {
anvilProcess.stdout.pipe(process.stdout);
anvilProcess.stderr.pipe(process.stderr);
}

await waitOn({ resources: [`tcp:127.0.0.1:${port}`] });
await waitOn({ resources: [`tcp:127.0.0.1:${args.port}`] });

const cleanup = () => {
console.log("\nShutting down anvil and cleaning up...");
Expand All @@ -76,7 +73,7 @@ const localnet = async (args: any) => {
};

try {
const addr = await initLocalnet(port);
const addr = await initLocalnet(args.port);

// EVM Contract Addresses
const evmHeader = "\nEVM Contract Addresses";
Expand All @@ -97,7 +94,6 @@ const localnet = async (args: any) => {

console.table(evmAddresses);

// ZetaChain Contract Addresses
const zetaHeader = "\nZetaChain Contract Addresses";
console.log(ansis.green(`${zetaHeader}\n${"=".repeat(zetaHeader.length)}`));

Expand All @@ -115,6 +111,14 @@ const localnet = async (args: any) => {
console.table(zetaAddresses);

fs.writeFileSync(LOCALNET_PID_FILE, process.pid.toString(), "utf-8");

if (args.stopAfterInit) {
console.log(
ansis.green("Localnet successfully initialized. Stopping...")
);
cleanup();
process.exit(0);
}
} catch (error: any) {
console.error(ansis.red`Error initializing localnet: ${error}`);
cleanup();
Expand Down Expand Up @@ -145,4 +149,8 @@ export const localnetTask = task("localnet", "Start localnet", localnet)
"",
types.string
)
.addFlag("forceKill", "Force kill any process on the port without prompting");
.addFlag("forceKill", "Force kill any process on the port without prompting")
.addFlag(
"stopAfterInit",
"Stop the localnet after successful initialization"
);

0 comments on commit c7ec285

Please sign in to comment.