Skip to content

Commit

Permalink
Change LocalNode stop() function to return a Promise (#465)
Browse files Browse the repository at this point in the history
change LocalNode stop() function to return a promise
  • Loading branch information
0xmaayan authored Jul 16, 2024
1 parent a5588e5 commit e136049
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion examples/typescript/local_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -99,15 +101,31 @@ 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");

Check warning on line 111 in examples/typescript/local_node.ts

View workflow job for this annotation

GitHub Actions / run-tests

Unexpected console statement
}
}

async function run() {
// start the localnet
await runLocalNode();

await init();
await compile();
await tests();
await publish();
await createObjectAndPublishPackage();
await upgradeObjectPackage();
await runScript();

// stop the localnet
await stopLocalNode();
}

run();
16 changes: 12 additions & 4 deletions src/cli/localNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit e136049

Please sign in to comment.