-
Notifications
You must be signed in to change notification settings - Fork 64
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
Add Aptos CLI #246
Merged
Merged
Add Aptos CLI #246
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
79d751c
add cli module
0xmaayan f21836c
add aptoscli npm package and use it in integration tests
0xmaayan d0cc997
disable localnet process for sdk tests on ci
0xmaayan 397eee7
clean up code and update readme
0xmaayan 5cdc9bd
fix lint
0xmaayan 831f496
address comments
0xmaayan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./localNode"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { ChildProcessWithoutNullStreams, spawn } from "child_process"; | ||
import kill from "tree-kill"; | ||
import { sleep } from "../utils/helpers"; | ||
|
||
export class LocalNode { | ||
readonly MAXIMUM_WAIT_TIME_SEC = 30; | ||
|
||
readonly READINESS_ENDPOINT = "http://127.0.0.1:8070/"; | ||
|
||
process: ChildProcessWithoutNullStreams | null = null; | ||
|
||
/** | ||
* kills all the descendent processes | ||
* of the node process, including the node process itself | ||
*/ | ||
stop() { | ||
if (!this.process?.pid) return; | ||
kill(this.process.pid); | ||
} | ||
|
||
/** | ||
* Runs a local testnet and waits for process to be up. | ||
* | ||
* If local node process is already up it returns and does | ||
* not start the process | ||
*/ | ||
async run() { | ||
const nodeIsUp = await this.checkIfProcessIsUp(); | ||
if (nodeIsUp) { | ||
return; | ||
} | ||
this.start(); | ||
await this.waitUntilProcessIsUp(); | ||
} | ||
|
||
/** | ||
* Starts the local testnet by running the aptos node run-local-testnet command | ||
*/ | ||
start() { | ||
const cliCommand = "npx"; | ||
const cliArgs = ["aptos", "node", "run-local-testnet", "--force-restart", "--assume-yes", "--with-indexer-api"]; | ||
|
||
const childProcess = spawn(cliCommand, cliArgs); | ||
this.process = childProcess; | ||
|
||
childProcess.stderr?.on("data", (data: any) => { | ||
const str = data.toString(); | ||
// Print local node output log | ||
// eslint-disable-next-line no-console | ||
console.log(str); | ||
}); | ||
|
||
childProcess.stdout?.on("data", (data: any) => { | ||
const str = data.toString(); | ||
// Print local node output log | ||
// eslint-disable-next-line no-console | ||
console.log(str); | ||
}); | ||
} | ||
|
||
/** | ||
* Waits for the local testnet process to be up | ||
* | ||
* @returns Promise<boolean> | ||
*/ | ||
async waitUntilProcessIsUp(): Promise<boolean> { | ||
let operational = await this.checkIfProcessIsUp(); | ||
const start = Date.now() / 1000; | ||
let last = start; | ||
|
||
while (!operational && start + this.MAXIMUM_WAIT_TIME_SEC > last) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await sleep(1000); | ||
// eslint-disable-next-line no-await-in-loop | ||
operational = await this.checkIfProcessIsUp(); | ||
last = Date.now() / 1000; | ||
} | ||
|
||
// If we are here it means something blocks the process to start. | ||
// Might worth checking if another process is running on port 8080 | ||
if (!operational) { | ||
throw new Error("Process failed to start"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Checks if the local testnet is up | ||
* | ||
* @returns Promise<boolean> | ||
*/ | ||
async checkIfProcessIsUp(): Promise<boolean> { | ||
try { | ||
// Query readiness endpoint | ||
const data = await fetch(this.READINESS_ENDPOINT); | ||
if (data.status === 200) { | ||
return true; | ||
} | ||
return false; | ||
} catch (err: any) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = async function () { | ||
// Check if the current local node process is | ||
// from within the sdk node environment | ||
if (globalThis.__LOCAL_NODE__.process) { | ||
const aptosNode = globalThis.__LOCAL_NODE__; | ||
// Local node runs multiple procceses, to avoid asynchronous operations | ||
// that weren't stopped in our tests, we kill all the descendent processes | ||
// of the node process, including the node process itself | ||
aptosNode.stop(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const { LocalNode } = require("../src/cli"); | ||
|
||
module.exports = async function setup() { | ||
const localNode = new LocalNode(); | ||
globalThis.__LOCAL_NODE__ = localNode; | ||
await localNode.run(); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can probably return void promise then