Skip to content

Commit cb880de

Browse files
committed
stop
1 parent 1e656dc commit cb880de

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

src/commands/start.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,12 @@ const startLocalnet = async (options: {
181181

182182
const enabledChains = options.chains || [];
183183

184-
// Initialize the processes array
185-
const processes: ProcessInfo[] = [];
184+
const processes: ProcessInfo[] = [
185+
{
186+
command: "localnet",
187+
pid: process.pid,
188+
},
189+
];
186190

187191
try {
188192
execSync("which anvil");
@@ -332,7 +336,7 @@ const waitForTonContainerToStop = async () => {
332336
}
333337
};
334338

335-
const cleanup = async (options: { chains: string[] }) => {
339+
export const cleanup = async (options: { chains: string[] }) => {
336340
logger.info("Shutting down processes and cleaning up...");
337341

338342
// Close readline interface if it exists
@@ -349,6 +353,9 @@ const cleanup = async (options: { chains: string[] }) => {
349353
const processData = JSON.parse(fs.readFileSync(PROCESS_FILE, "utf-8"));
350354
if (processData && processData.processes) {
351355
for (const proc of processData.processes) {
356+
if (proc.command === "localnet") {
357+
continue;
358+
}
352359
try {
353360
process.kill(proc.pid, "SIGKILL");
354361
logger.info(

src/commands/stop.ts

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,63 @@
11
import ansis from "ansis";
22
import { Command } from "commander";
33
import fs from "fs";
4+
import path from "path";
45

5-
const LOCALNET_JSON_FILE = "./localnet.json";
6+
import { LOCALNET_DIR, NetworkID, REGISTRY_FILE } from "../constants";
7+
import { initLogger } from "../logger";
8+
import { cleanup } from "./start";
69

7-
const stopLocalnet = () => {
8-
if (!fs.existsSync(LOCALNET_JSON_FILE)) {
9-
console.log(ansis.red("Localnet is not running or JSON file is missing."));
10-
return;
11-
}
10+
const stopLocalnet = async () => {
11+
initLogger("info");
12+
const chains: string[] = [];
1213

13-
const jsonData = JSON.parse(fs.readFileSync(LOCALNET_JSON_FILE, "utf-8"));
14-
const pid = jsonData.pid;
14+
// Read localnet PID before doing anything
15+
const processFile = path.join(LOCALNET_DIR, "process.json");
16+
let localnetPid: number | undefined;
17+
try {
18+
if (fs.existsSync(processFile)) {
19+
const data = JSON.parse(fs.readFileSync(processFile, "utf-8"));
20+
const processes =
21+
(data?.processes as { command: string; pid: number }[]) || [];
22+
localnetPid = processes.find((p) => p?.command === "localnet")?.pid;
23+
}
24+
} catch (err) {
25+
// Ignore errors when reading PID; proceed with best-effort cleanup
26+
}
1527

28+
// Derive whether TON was enabled from the registry (only needed for TON Docker shutdown)
1629
try {
17-
process.kill(Number(pid), 0); // check that the process is running
18-
try {
19-
process.kill(Number(pid));
20-
console.log(ansis.green(`Successfully stopped localnet (PID: ${pid})`));
21-
} catch (err) {
22-
console.error(ansis.red(`Failed to stop localnet: ${err}`));
30+
if (fs.existsSync(REGISTRY_FILE)) {
31+
const registry = JSON.parse(fs.readFileSync(REGISTRY_FILE, "utf-8"));
32+
if (registry && registry[NetworkID.TON]) {
33+
chains.push("ton");
34+
}
2335
}
2436
} catch (err) {
25-
console.log(ansis.yellow(`Localnet process (PID: ${pid}) is not running.`));
26-
try {
27-
fs.unlinkSync(LOCALNET_JSON_FILE);
28-
console.log(ansis.green("Localnet JSON file deleted."));
29-
} catch (err) {
30-
console.error(ansis.red(`Failed to delete JSON file: ${err}`));
37+
// If registry is unreadable, proceed with best-effort cleanup
38+
}
39+
40+
try {
41+
await cleanup({ chains });
42+
43+
// After cleanup is done, kill localnet by PID if we captured it
44+
if (typeof localnetPid === "number" && Number.isFinite(localnetPid)) {
45+
try {
46+
process.kill(localnetPid, "SIGKILL");
47+
} catch {
48+
// Ignore if already stopped or cannot kill
49+
}
3150
}
51+
52+
console.log(ansis.green("Localnet stopped successfully."));
53+
} catch (error) {
54+
console.error(ansis.red(`Failed to stop localnet: ${error}`));
55+
process.exit(1);
3256
}
3357
};
3458

3559
export const stopCommand = new Command("stop")
3660
.description("Stop localnet")
37-
.action(() => {
38-
try {
39-
stopLocalnet();
40-
} catch (error) {
41-
console.error(ansis.red(`Error: ${error}`));
42-
process.exit(1);
43-
}
61+
.action(async () => {
62+
await stopLocalnet();
4463
});

0 commit comments

Comments
 (0)