From fe77f8e8b5aa00bb90f7fff5a277362e908f2d45 Mon Sep 17 00:00:00 2001 From: Alan Soares Date: Wed, 8 Nov 2023 16:13:22 +1300 Subject: [PATCH] refactor: migrate sh script to mjs --- packages/evm/bin/cli.mjs | 49 +++++++++++++++++++++++++++++++++++++++ packages/evm/bin/cli.sh | 45 ----------------------------------- packages/evm/package.json | 2 +- 3 files changed, 50 insertions(+), 46 deletions(-) create mode 100755 packages/evm/bin/cli.mjs delete mode 100755 packages/evm/bin/cli.sh diff --git a/packages/evm/bin/cli.mjs b/packages/evm/bin/cli.mjs new file mode 100755 index 000000000..5b19f3dcb --- /dev/null +++ b/packages/evm/bin/cli.mjs @@ -0,0 +1,49 @@ +#!/usr/bin/env zx +import { $, argv } from "zx"; + +// List of valid commands +const VALID_COMMANDS = ["codegen"]; + +async function getNpxCompatibleCommand() { + const commands = ["npx", "pnpx", "bunx"]; + + for (const cmd of commands) { + try { + await $`command -v ${cmd}`; + return cmd; // Return early if the command is found + } catch { + continue; // Continue to the next iteration if the command is not found + } + } +} + +async function run() { + const COMMAND = argv._[1]; + + const NPX_COMMAND = await getNpxCompatibleCommand(); + + switch (COMMAND) { + case "codegen": + { + const { src, out, flatten, exclude } = argv; + + await $`${NPX_COMMAND} tsx ./scripts/codegen.ts --src ${src} --out ${out} --exclude ${exclude} ${ + flatten ? "--flatten" : "" + }`; + } + break; + default: + await $`echo unknown command received: '${COMMAND}'`; + await $`echo valid commands:`; + + // eslint-disable-next-line no-undef + VALID_COMMANDS.forEach((cmd) => console.log(` * ${cmd}`)); + } +} + +run().catch((e) => { + // eslint-disable-next-line no-undef + console.error(e); + // eslint-disable-next-line no-undef + process.exit(1); +}); diff --git a/packages/evm/bin/cli.sh b/packages/evm/bin/cli.sh deleted file mode 100755 index e077e13fe..000000000 --- a/packages/evm/bin/cli.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash - -# commands: -# codegen: generate code from abi - -COMMAND=$1 - -# comma separated list of valid commands -VALID_COMMANDS="codegen" - -get_npx_compatible_command(){ - # check if npx is installed - if command -v npx &> /dev/null - then - echo "npx" - # check if pnpx is installed - elif command -v pnpx &> /dev/null - then - echo "pnpx" - # check if bunx is installed - elif command -v bunx &> /dev/null - then - echo "bunx" - else - echo "npx, pnpx or bunx is required to run this script" - exit 1 - fi -} - - -# get the npx compatible command -NPX_COMMAND=$(get_npx_compatible_command) - -case $COMMAND in - codegen) - echo "using '$NPX_COMMAND' to run codegen" - $NPX_COMMAND tsx ./scripts/codegen.ts "$@" - ;; - *) - echo "unknown command received: '$COMMAND'" - echo "valid commands:" - # split string into array and print each element on a new line with a - prefix - echo "$VALID_COMMANDS" | tr ',' '\n' | sed 's/^/ * /' - ;; -esac \ No newline at end of file diff --git a/packages/evm/package.json b/packages/evm/package.json index ee70d61ca..9576f3d6a 100644 --- a/packages/evm/package.json +++ b/packages/evm/package.json @@ -18,7 +18,7 @@ "./clients.js", "./clients.d.ts" ], - "bin": "./bin/cli.sh", + "bin": "./bin/cli.mjs", "exports": { ".": { "import": "./build/module/index.js",