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

fix(cli): change import order so .env file is loaded first #1860

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Changes from 1 commit
Commits
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
68 changes: 37 additions & 31 deletions packages/cli/src/mud.ts
Original file line number Diff line number Diff line change
@@ -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 can pick up 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();