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

Clients/js/refactor cmds & CLI docs generator #3182

Merged
merged 28 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8ee9b94
feat: update npm commands for Githun Actions CI
AlberErre Jun 21, 2023
916ff2b
feat: add worm cli github actions
AlberErre Jun 21, 2023
22053fc
feat: save HTML test report as artifact
AlberErre Jun 21, 2023
f8077af
chore: update github action, show report correctly
AlberErre Jun 21, 2023
a3389ff
Merge branch 'main' of https://github.com/AlberErre/wormhole
AlberErre Jun 22, 2023
f300ae3
Merge branch 'wormhole-foundation:main' into main
AlberErre Jul 3, 2023
6608f78
Merge branch 'wormhole-foundation:main' into main
AlberErre Jul 6, 2023
1413a05
fix: add missing CommandModule type
AlberErre Jun 23, 2023
c65cd13
chore: rm unused import
AlberErre Jun 23, 2023
c8f4759
fix: override auto-detected locale by OS system
AlberErre Jul 3, 2023
9d27611
feat: infere command modules on doc.ts & main.ts
AlberErre Jul 4, 2023
b98eec9
feat: command args accepts an array of modules
AlberErre Jul 4, 2023
98b0319
fix: cmds must be outside main, breaks otherwise
AlberErre Jul 4, 2023
d80a53c
fix: import CLI_COMMAND_MODULES outside of main
AlberErre Jul 4, 2023
b39c548
chore: add missing transfer command from README
AlberErre Jul 4, 2023
7eb8a74
chore: rm test branch dependencies
AlberErre Jul 9, 2023
6402d1b
feat: extract info cmds into array const
AlberErre Jul 9, 2023
1209c61
chore: document command imports as list
AlberErre Jul 9, 2023
fd009f4
chore: package.json spacing
AlberErre Jul 9, 2023
552a68c
Merge branch 'main' into clients/js/refactor-cmds
AlberErre Jul 9, 2023
ae13aa7
chore: bump @types/yargs version
AlberErre Jul 9, 2023
6bdb56b
feat: cast correct array type YargsCommandModule[]
AlberErre Jul 9, 2023
dab913a
Merge branch 'main' into clients/js/refactor-cmds
AlberErre Jul 10, 2023
95c0de5
Merge branch 'main' into clients/js/refactor-cmds
AlberErre Jul 12, 2023
4127067
Merge branch 'wormhole-foundation:main' into main
AlberErre Jul 12, 2023
edf7182
Merge branch 'main' into clients/js/refactor-cmds
AlberErre Jul 12, 2023
c8f4484
Merge branch 'main' into clients/js/refactor-cmds
AlberErre Jul 12, 2023
1c18666
Merge branch 'main' into clients/js/refactor-cmds
AlberErre Jul 13, 2023
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
27 changes: 27 additions & 0 deletions clients/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,33 @@ Options:
```
</details>

<details>
<summary> transfer </summary>

```sh
Options:
--help Show help [boolean]
--version Show version number [boolean]
--src-chain source chain
[required] [choices: "solana", "ethereum", "terra", "bsc", "polygon",
"avalanche", "oasis", "algorand", "aurora", "fantom", "karura", "acala",
"klaytn", "celo", "near", "moonbeam", "neon", "terra2", "injective",
"osmosis", "sui", "aptos", "arbitrum", "optimism", "gnosis", "pythnet",
"xpla", "btc", "base", "sei", "wormchain", "sepolia"]
--dst-chain destination chain
[required] [choices: "solana", "ethereum", "terra", "bsc", "polygon",
"avalanche", "oasis", "algorand", "aurora", "fantom", "karura", "acala",
"klaytn", "celo", "near", "moonbeam", "neon", "terra2", "injective",
"osmosis", "sui", "aptos", "arbitrum", "optimism", "gnosis", "pythnet",
"xpla", "btc", "base", "sei", "wormchain", "sepolia"]
--dst-addr destination address [string] [required]
--token-addr token address [string] [default: native token]
--amount token amount [string] [required]
-n, --network Network [required] [choices: "mainnet", "testnet", "devnet"]
--rpc RPC endpoint [string]
```
</details>

<details>
<summary> verify-vaa </summary>

Expand Down
14 changes: 7 additions & 7 deletions clients/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@types/bn.js": "^5.1.0",
"@types/bs58": "^4.0.1",
"@types/node-fetch": "^2.6.3",
"@types/yargs": "^17.0.2",
"@types/yargs": "^17.0.24",
"copy-dir": "^1.3.0",
"typescript": "^4.6"
}
Expand Down
4 changes: 3 additions & 1 deletion clients/js/src/cmds/Yargs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import yargs from "yargs";
import yargs, { CommandModule } from "yargs";

export class Yargs {
yargs: typeof yargs;
Expand All @@ -16,3 +16,5 @@ export class Yargs {
}

export type YargsAddCommandsFn = (y: typeof yargs) => typeof yargs;

export type YargsCommandModule = CommandModule<any, any>;
32 changes: 32 additions & 0 deletions clients/js/src/cmds/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https://github.com/yargs/yargs/blob/main/docs/advanced.md#example-command-hierarchy-using-indexmjs
import * as aptos from "./aptos";
import * as editVaa from "./editVaa";
import * as evm from "./evm";
import * as generate from "./generate";
import * as info from "./info";
import * as near from "./near";
import * as parse from "./parse";
import * as recover from "./recover";
import * as submit from "./submit";
import * as sui from "./sui";
import * as transfer from "./transfer";
import * as verifyVaa from "./verifyVaa";
import * as status from "./status";

// Commands can be imported as an array of commands.
// Documentation about command hierarchy can be found here: https://github.com/yargs/yargs/blob/main/docs/advanced.md#example-command-hierarchy-using-indexmjs
export const CLI_COMMAND_MODULES = [
aptos,
editVaa,
evm,
generate,
info,
near,
parse,
recover,
submit,
sui,
transfer,
verifyVaa,
status,
];
20 changes: 5 additions & 15 deletions clients/js/src/cmds/info/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import yargs from "yargs";
import * as chainId from "./chainId";
import * as contract from "./contract";
import * as emitter from "./emitter";
import * as origin from "./origin";
import * as registrations from "./registrations";
import * as rpc from "./rpc";
import * as wrapped from "./wrapped";
import { YargsCommandModule } from "../Yargs";
import { INFO_COMMANDS } from "./info";

export const command = "info";
export const desc = "Contract, chain, rpc and address information utilities";
// Imports modules logic from root commands, more info here -> https://github.com/yargs/yargs/blob/main/docs/advanced.md#providing-a-command-module
export const builder = (y: typeof yargs) =>
y
.command(chainId)
.command(contract)
.command(emitter)
.command(origin)
.command(registrations)
.command(rpc)
.command(wrapped);
// Commands can be imported as an array of commands.
// Documentation about command hierarchy can be found here: https://github.com/yargs/yargs/blob/main/docs/advanced.md#example-command-hierarchy-using-indexmjs
y.command(INFO_COMMANDS as unknown as YargsCommandModule[]);
barnjamin marked this conversation as resolved.
Show resolved Hide resolved
export const handler = () => {};
19 changes: 19 additions & 0 deletions clients/js/src/cmds/info/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as chainId from "./chainId";
import * as contract from "./contract";
import * as emitter from "./emitter";
import * as origin from "./origin";
import * as registrations from "./registrations";
import * as rpc from "./rpc";
import * as wrapped from "./wrapped";

// Commands can be imported as an array of commands.
// Documentation about command hierarchy can be found here: https://github.com/yargs/yargs/blob/main/docs/advanced.md#example-command-hierarchy-using-indexmjs
export const INFO_COMMANDS = [
chainId,
contract,
emitter,
origin,
registrations,
rpc,
wrapped,
];
51 changes: 14 additions & 37 deletions clients/js/src/doc.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,51 @@
import * as fs from "fs";

import yargs from "yargs";
// Side effects are here to trigger before the afflicted libraries' on-import warnings can be emitted.
// It is also imported so that it can side-effect without being tree-shaken.
import "./side-effects";
// https://github.com/yargs/yargs/blob/main/docs/advanced.md#example-command-hierarchy-using-indexmjs
import * as aptos from "./cmds/aptos";
import * as editVaa from "./cmds/editVaa";
import * as evm from "./cmds/evm";
import * as generate from "./cmds/generate";
import * as info from "./cmds/info";
import * as near from "./cmds/near";
import * as parse from "./cmds/parse";
import * as recover from "./cmds/recover";
import * as submit from "./cmds/submit";
import * as sui from "./cmds/sui";
import * as verifyVaa from "./cmds/verifyVaa";
import * as status from "./cmds/status";

import { CLI_COMMAND_MODULES } from "./cmds";

const MD_TAG = "<!--CLI_USAGE-->";

async function getHelpText(cmd: any): Promise<string> {
// Note that `yargs` is called as a function to produce a fresh copy.
// Otherwise the imported module is effectively a singleton where state from
// Otherwise the imported module is effectively a singleton where state from
// other commands is accumulated from repeat calls.
return await cmd.builder(yargs()).scriptName(`worm ${cmd.command}`).getHelp();
return await cmd
.builder(yargs())
.scriptName(`worm ${cmd.command}`)
.locale("en") //NOTE: 'locale' needed to override auto-detected locale from the user’s operating system
barnjamin marked this conversation as resolved.
Show resolved Hide resolved
.getHelp();
}

(async function () {
const cmds = [
aptos,
editVaa,
evm,
generate,
info,
near,
parse,
recover,
submit,
sui,
verifyVaa,
status
];
const cmds = CLI_COMMAND_MODULES;
barnjamin marked this conversation as resolved.
Show resolved Hide resolved

const helpOutputs: Buffer[] = [];
for (const cmd of cmds) {
const helpText = await getHelpText(cmd);

helpOutputs.push(Buffer.from(`
helpOutputs.push(
Buffer.from(`
<details>
<summary> ${cmd.command} </summary>

\`\`\`sh
${helpText}
\`\`\`
</details>
`))
`)
);
}



const f = fs.readFileSync("README.md");
const startIdx = f.indexOf(MD_TAG, 0);
const stopIdx = f.indexOf(MD_TAG, startIdx + 1);

const head = f.subarray(0, startIdx + MD_TAG.length);
const tail = f.subarray(stopIdx, f.length);

const content = Buffer.concat([head, ...helpOutputs, tail])
const content = Buffer.concat([head, ...helpOutputs, tail]);

fs.writeFileSync("README.md", content.toString())
fs.writeFileSync("README.md", content.toString());
})();
36 changes: 5 additions & 31 deletions clients/js/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,12 @@ import { hideBin } from "yargs/helpers";
// Side effects are here to trigger before the afflicted libraries' on-import warnings can be emitted.
// It is also imported so that it can side-effect without being tree-shaken.
import "./side-effects";
// https://github.com/yargs/yargs/blob/main/docs/advanced.md#example-command-hierarchy-using-indexmjs
import * as aptos from "./cmds/aptos";
import * as editVaa from "./cmds/editVaa";
import * as evm from "./cmds/evm";
import * as generate from "./cmds/generate";
import * as info from "./cmds/info";
import * as near from "./cmds/near";
import * as parse from "./cmds/parse";
import * as recover from "./cmds/recover";
import * as submit from "./cmds/submit";
import * as sui from "./cmds/sui";
import * as transfer from "./cmds/transfer";
import * as verifyVaa from "./cmds/verifyVaa";
import * as status from "./cmds/status";
import { YargsCommandModule } from "./cmds/Yargs";
import { CLI_COMMAND_MODULES } from "./cmds";

// Note: When adding another subcommand here, please be sure to also include it
// in the `cmds` array in `docs.ts` so it is properly documented.
yargs(hideBin(process.argv))
// https://github.com/yargs/yargs/blob/main/docs/advanced.md#commanddirdirectory-opts
// can't use `.commandDir` because bundling + tree-shaking
.command(aptos)
.command(editVaa)
.command(evm)
.command(generate)
.command(info)
.command(near)
.command(parse)
.command(recover)
.command(submit)
.command(sui)
.command(transfer)
.command(verifyVaa)
.command(status)
// Build CLI commands dinamically from CLI_COMMAND_MODULES list
// Documentation about command hierarchy can be found here: https://github.com/yargs/yargs/blob/main/docs/advanced.md#example-command-hierarchy-using-indexmjs
.command(CLI_COMMAND_MODULES as YargsCommandModule[])
.strict()
.demandCommand().argv;