From 8e0cf4c31f39b5ba06b0f6cdb6e690d3adf3d6a3 Mon Sep 17 00:00:00 2001 From: Alber Date: Mon, 17 Jul 2023 16:10:48 +0100 Subject: [PATCH] clients/js: refactor cmds & CLI docs generator (#3182) * 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[] --- clients/js/README.md | 27 +++++++++++++++++++++++ clients/js/package-lock.json | 14 ++++++------ clients/js/package.json | 2 +- clients/js/src/cmds/Yargs.ts | 4 +++- clients/js/src/cmds/index.ts | 32 +++++++++++++++++++++++++++ clients/js/src/cmds/info/index.ts | 20 +++++------------ clients/js/src/cmds/info/info.ts | 19 ++++++++++++++++ clients/js/src/doc.ts | 36 ++++++------------------------- clients/js/src/main.ts | 36 +++++-------------------------- 9 files changed, 106 insertions(+), 84 deletions(-) create mode 100644 clients/js/src/cmds/index.ts create mode 100644 clients/js/src/cmds/info/info.ts diff --git a/clients/js/README.md b/clients/js/README.md index 23c88900c..604c29053 100644 --- a/clients/js/README.md +++ b/clients/js/README.md @@ -298,6 +298,33 @@ Options: ``` +
+ transfer + +```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] +``` +
+
verify-vaa diff --git a/clients/js/package-lock.json b/clients/js/package-lock.json index 7bfc6f369..34765d9c6 100644 --- a/clients/js/package-lock.json +++ b/clients/js/package-lock.json @@ -48,7 +48,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" } @@ -3531,9 +3531,9 @@ } }, "node_modules/@types/yargs": { - "version": "17.0.2", - "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.2.tgz", - "integrity": "sha512-JhZ+pNdKMfB0rXauaDlrIvm+U7V4m03PPOSVoPS66z8gf+G4Z/UW8UlrVIj2MRQOBzuoEvYtjS0bqYwnpZaS9Q==", + "version": "17.0.24", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.24.tgz", + "integrity": "sha512-6i0aC7jV6QzQB8ne1joVZ0eSFIstHsCrobmOtghM11yGlH0j43FKL2UhWdELkyps0zuf7qVTUVCCR+tgSlyLLw==", "dev": true, "dependencies": { "@types/yargs-parser": "*" @@ -10878,9 +10878,9 @@ } }, "@types/yargs": { - "version": "17.0.2", - "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.2.tgz", - "integrity": "sha512-JhZ+pNdKMfB0rXauaDlrIvm+U7V4m03PPOSVoPS66z8gf+G4Z/UW8UlrVIj2MRQOBzuoEvYtjS0bqYwnpZaS9Q==", + "version": "17.0.24", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.24.tgz", + "integrity": "sha512-6i0aC7jV6QzQB8ne1joVZ0eSFIstHsCrobmOtghM11yGlH0j43FKL2UhWdELkyps0zuf7qVTUVCCR+tgSlyLLw==", "dev": true, "requires": { "@types/yargs-parser": "*" diff --git a/clients/js/package.json b/clients/js/package.json index fdae4edac..86d801333 100644 --- a/clients/js/package.json +++ b/clients/js/package.json @@ -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" } diff --git a/clients/js/src/cmds/Yargs.ts b/clients/js/src/cmds/Yargs.ts index 242338ffa..65d4471ab 100644 --- a/clients/js/src/cmds/Yargs.ts +++ b/clients/js/src/cmds/Yargs.ts @@ -1,4 +1,4 @@ -import yargs from "yargs"; +import yargs, { CommandModule } from "yargs"; export class Yargs { yargs: typeof yargs; @@ -16,3 +16,5 @@ export class Yargs { } export type YargsAddCommandsFn = (y: typeof yargs) => typeof yargs; + +export type YargsCommandModule = CommandModule; diff --git a/clients/js/src/cmds/index.ts b/clients/js/src/cmds/index.ts new file mode 100644 index 000000000..ebf9f15c6 --- /dev/null +++ b/clients/js/src/cmds/index.ts @@ -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, +]; diff --git a/clients/js/src/cmds/info/index.ts b/clients/js/src/cmds/info/index.ts index 0517a1c4e..005e316cc 100644 --- a/clients/js/src/cmds/info/index.ts +++ b/clients/js/src/cmds/info/index.ts @@ -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 = () => {}; diff --git a/clients/js/src/cmds/info/info.ts b/clients/js/src/cmds/info/info.ts new file mode 100644 index 000000000..6f5a6438f --- /dev/null +++ b/clients/js/src/cmds/info/info.ts @@ -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, +]; diff --git a/clients/js/src/doc.ts b/clients/js/src/doc.ts index a009dd544..7d78aaf83 100644 --- a/clients/js/src/doc.ts +++ b/clients/js/src/doc.ts @@ -1,22 +1,9 @@ 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 = ""; @@ -24,24 +11,15 @@ async function getHelpText(cmd: any): Promise { // 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) { diff --git a/clients/js/src/main.ts b/clients/js/src/main.ts index 244799533..8f7bac2ba 100644 --- a/clients/js/src/main.ts +++ b/clients/js/src/main.ts @@ -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;