From 52d5cc8e8f8f596eded3318510cf3138c1153845 Mon Sep 17 00:00:00 2001 From: maayan Date: Mon, 15 Jul 2024 20:01:27 -0400 Subject: [PATCH] change LocalNode stop() function to return a promise --- CHANGELOG.md | 2 ++ examples/typescript/local_node.ts | 20 +++++++++++++++++++- src/cli/localNode.ts | 16 ++++++++++++---- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3668c063..1e93e4ac7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T # Unreleased +- Change the `stop()` function on `LocalNode` to return a `Promise` so we can wait for the processes to be killed + # 1.24.0 (2024-07-12) - Make `fundAccount` to wait for the `fungible_asset_processor` indexer processor diff --git a/examples/typescript/local_node.ts b/examples/typescript/local_node.ts index ffe7f8ea6..4962f9c90 100644 --- a/examples/typescript/local_node.ts +++ b/examples/typescript/local_node.ts @@ -8,9 +8,11 @@ const cli = require("@aptos-labs/ts-sdk/dist/common/cli/index.js"); +let localNode: any; + // Run local node async function runLocalNode() { - const localNode = new cli.LocalNode(); + localNode = new cli.LocalNode(); await localNode.run(); } @@ -99,8 +101,21 @@ async function runScript() { }); } +// Stop local node +async function stopLocalNode() { + await localNode.stop(); + try { + // Query localnet endpoint + await fetch("http://localhost:8080"); + } catch (err: any) { + console.log("localnet stopped"); + } +} + async function run() { + // start the localnet await runLocalNode(); + await init(); await compile(); await tests(); @@ -108,6 +123,9 @@ async function run() { await createObjectAndPublishPackage(); await upgradeObjectPackage(); await runScript(); + + // stop the localnet + await stopLocalNode(); } run(); diff --git a/src/cli/localNode.ts b/src/cli/localNode.ts index 45bd5710d..c83809ca1 100644 --- a/src/cli/localNode.ts +++ b/src/cli/localNode.ts @@ -15,9 +15,17 @@ export class LocalNode { * kills all the descendent processes * of the node process, including the node process itself */ - stop() { - if (!this.process?.pid) return; - kill(this.process.pid); + async stop() { + await new Promise((resolve, reject) => { + if (!this.process?.pid) return; + kill(this.process.pid, (err) => { + if (err) { + reject(err); + } else { + resolve(true); + } + }); + }); } /** @@ -40,7 +48,7 @@ export class LocalNode { */ start() { const cliCommand = "npx"; - const cliArgs = ["aptos", "node", "run-local-testnet", "--force-restart", "--assume-yes", "--with-indexer-api"]; + const cliArgs = ["aptos", "node", "run-localnet", "--force-restart", "--assume-yes", "--with-indexer-api"]; const currentPlatform = platform(); let childProcess;