Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change LocalNode stop() function to return a Promise #465

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
});
}

// 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"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice


const currentPlatform = platform();
let childProcess;
Expand Down
Loading