Skip to content

Commit

Permalink
clients/js: refactor cmds & CLI docs generator (#3182)
Browse files Browse the repository at this point in the history
* feat: update npm commands for Githun Actions CI

* feat: add worm cli github actions

* feat: save HTML test report as artifact

* chore: update github action, show report correctly

* fix: add missing CommandModule type

* chore: rm unused import

* fix: override auto-detected locale by OS system

* feat: infere command modules on doc.ts & main.ts

* feat: command args accepts an array of modules

* fix: cmds must be outside main, breaks otherwise

* fix: import CLI_COMMAND_MODULES outside of main

* chore: add missing transfer command from README

* chore: rm test branch dependencies

* feat: extract info cmds into array const

* chore: document command imports as list

* chore: package.json spacing

* chore: bump @types/yargs version

* feat: cast correct array type YargsCommandModule[]
  • Loading branch information
AlberErre authored and bruce-riley committed Jul 17, 2023
1 parent caec50b commit d9175bf
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 84 deletions.
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[]);
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,
];
36 changes: 7 additions & 29 deletions clients/js/src/doc.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,25 @@
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
// 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
.getHelp();
}

(async function () {
const cmds = [
aptos,
editVaa,
evm,
generate,
info,
near,
parse,
recover,
submit,
sui,
verifyVaa,
status,
];
const cmds = CLI_COMMAND_MODULES;

const helpOutputs: Buffer[] = [];
for (const cmd of cmds) {
Expand Down
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;

0 comments on commit d9175bf

Please sign in to comment.