-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): change import order so .env file is loaded first (#1860)
- Loading branch information
Showing
2 changed files
with
42 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@latticexyz/cli": patch | ||
--- | ||
|
||
Changed `mud` CLI import order so that environment variables from the `.env` file are loaded before other imports. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |