|
1 | 1 | import ansis from "ansis"; |
2 | 2 | import { Command } from "commander"; |
3 | 3 | import fs from "fs"; |
| 4 | +import path from "path"; |
4 | 5 |
|
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"; |
6 | 9 |
|
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[] = []; |
12 | 13 |
|
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 | + } |
15 | 27 |
|
| 28 | + // Derive whether TON was enabled from the registry (only needed for TON Docker shutdown) |
16 | 29 | 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 | + } |
23 | 35 | } |
24 | 36 | } 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 | + } |
31 | 50 | } |
| 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); |
32 | 56 | } |
33 | 57 | }; |
34 | 58 |
|
35 | 59 | export const stopCommand = new Command("stop") |
36 | 60 | .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(); |
44 | 63 | }); |
0 commit comments