From 75bb023b498a0242afec8dcd4337d51a710e8b8d Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Thu, 2 Nov 2023 12:21:03 +0000 Subject: [PATCH] fix(cli): change import order so .env file is loaded first --- packages/cli/src/mud.ts | 68 ++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/packages/cli/src/mud.ts b/packages/cli/src/mud.ts index 5ab75f6f9a7..4d2e8205f9b 100755 --- a/packages/cli/src/mud.ts +++ b/packages/cli/src/mud.ts @@ -1,39 +1,45 @@ #!/usr/bin/env node -import yargs from "yargs"; -import { hideBin } from "yargs/helpers"; -import { commands } from "./commands"; -import { logError } from "./utils/errors"; - // Load .env file into process.env import * as dotenv from "dotenv"; -import chalk from "chalk"; dotenv.config(); -yargs(hideBin(process.argv)) - // Explicit name to display in help (by default it's the entry file, which may not be "mud" for e.g. ts-node) - .scriptName("mud") - // Use the commands directory to scaffold - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- command array overload isn't typed, see https://github.com/yargs/yargs/blob/main/docs/advanced.md#esm-hierarchy - .command(commands as any) - // Enable strict mode. - .strict() - // Custom error handler - .fail((msg, err) => { - console.error(chalk.red(msg)); - if (msg.includes("Missing required argument")) { - console.log( - chalk.yellow(`Run 'pnpm mud ${process.argv[2]} --help' for a list of available and required arguments.`) - ); - } - console.log(""); - // Even though `.fail` type says we should get an `Error`, this can sometimes be undefined - if (err != null) { - logError(err); +async function run() { + // Import everything else async so they get loaded using env vars in .env + const { default: yargs } = await import("yargs"); + const { default: chalk } = await import("chalk"); + const { hideBin } = await import("yargs/helpers"); + const { logError } = await import("./utils/errors"); + const { commands } = await import("./commands"); + + yargs(hideBin(process.argv)) + // Explicit name to display in help (by default it's the entry file, which may not be "mud" for e.g. ts-node) + .scriptName("mud") + // Use the commands directory to scaffold + // command array overload isn't typed, see https://github.com/yargs/yargs/blob/main/docs/advanced.md#esm-hierarchy + // eslint-disable-next-line @typescript-eslint/no-explicit-any + .command(commands as any) + // Enable strict mode. + .strict() + // Custom error handler + .fail((msg, err) => { + console.error(chalk.red(msg)); + if (msg.includes("Missing required argument")) { + console.log( + chalk.yellow(`Run 'pnpm mud ${process.argv[2]} --help' for a list of available and required arguments.`) + ); + } console.log(""); - } + // Even though `.fail` type says we should get an `Error`, this can sometimes be undefined + if (err != null) { + logError(err); + console.log(""); + } + + process.exit(1); + }) + // Useful aliases. + .alias({ h: "help" }).argv; +} - process.exit(1); - }) - // Useful aliases. - .alias({ h: "help" }).argv; +run();