diff --git a/packages/cli/src/commands/explorer/explorer.ts b/packages/cli/src/commands/explorer/explorer.ts index 037163de..4dd8c01f 100644 --- a/packages/cli/src/commands/explorer/explorer.ts +++ b/packages/cli/src/commands/explorer/explorer.ts @@ -5,9 +5,11 @@ export const explorerCommand: CommandModule = { describe: "Explorer commands", builder: async (yargs) => { const { explorerStartCommand } = await import("./explorerStart"); + const { explorerStopCommand } = await import("./explorerStop"); return yargs .command(explorerStartCommand) + .command(explorerStopCommand) .demandCommand( 1, "You must specify a subcommand. Use --help to see available options." diff --git a/packages/cli/src/commands/explorer/explorerStop.ts b/packages/cli/src/commands/explorer/explorerStop.ts new file mode 100644 index 00000000..3655e0cc --- /dev/null +++ b/packages/cli/src/commands/explorer/explorerStop.ts @@ -0,0 +1,17 @@ +import { CommandModule } from "yargs"; + +export const explorerStopCommand: CommandModule = { + command: "stop", + describe: "Stop the explorer UI", + handler: async () => { + try { + const { default: explorerStop } = + await import("../../scripts/explorer/stop"); + await explorerStop(); + process.exit(0); + } catch (error) { + console.error("Failed to stop explorer:", error); + process.exit(1); + } + }, +}; diff --git a/packages/cli/src/scripts/explorer/start.ts b/packages/cli/src/scripts/explorer/start.ts index b84105c9..a9e129ef 100644 --- a/packages/cli/src/scripts/explorer/start.ts +++ b/packages/cli/src/scripts/explorer/start.ts @@ -35,7 +35,15 @@ async function runDockerContainer(args: { const { port = 5003, explorerImage } = args; console.log(`\nExplorer is running at http://localhost:${port}\n`); - const dockerArgs = ["run", "--rm", "-p", `${port}:3000`]; + const dockerArgs = [ + "run", + "-d", + "--rm", + "--name", + "protokit-explorer", + "-p", + `${port}:3000`, + ]; if (args.indexerUrl !== undefined) { dockerArgs.push("-e", `NEXT_PUBLIC_INDEXER_URL=${args.indexerUrl}`); diff --git a/packages/cli/src/scripts/explorer/stop.ts b/packages/cli/src/scripts/explorer/stop.ts new file mode 100644 index 00000000..db229407 --- /dev/null +++ b/packages/cli/src/scripts/explorer/stop.ts @@ -0,0 +1,37 @@ +import { spawn } from "child_process"; + +const CONTAINER_NAME = "protokit-explorer"; + +async function stopDockerContainer(): Promise { + return await new Promise((resolve, reject) => { + console.log("Stopping explorer container..."); + const child = spawn("docker", ["stop", CONTAINER_NAME], { + stdio: "inherit", + }); + + child.on("error", (error) => { + console.error("Failed to stop explorer container:", error); + reject(error); + }); + + child.on("exit", (code) => { + if (code !== null && code !== 0) { + reject( + new Error(`Failed to stop explorer container (exit code ${code})`) + ); + } else { + console.log("Explorer container stopped successfully"); + resolve(); + } + }); + }); +} + +export default async function (): Promise { + try { + await stopDockerContainer(); + } catch (error) { + console.error("Failed to stop explorer:", error); + throw error; + } +}