From 913591d13761d7c99f4b5bf547942922736a9f3a Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Thu, 30 Jun 2022 07:16:39 -0700 Subject: [PATCH] refactor to provide better cli error handling (#3768) --- .changeset/sour-clocks-unite.md | 5 +++ packages/astro/src/cli/index.ts | 75 ++++++++++++++------------------- 2 files changed, 37 insertions(+), 43 deletions(-) create mode 100644 .changeset/sour-clocks-unite.md diff --git a/.changeset/sour-clocks-unite.md b/.changeset/sour-clocks-unite.md new file mode 100644 index 0000000000..85ddbedbf1 --- /dev/null +++ b/.changeset/sour-clocks-unite.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix an issue with unfriendly config validation error output diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index 2bbd3cdc24..a54ae1b720 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -81,10 +81,12 @@ function resolveCommand(flags: Arguments): CLICommand { return 'help'; } -/** The primary CLI action */ -export async function cli(args: string[]) { - const flags = yargs(args); - const cmd = resolveCommand(flags); +/** + * Run the given command with the given flags. + * NOTE: This function provides no error handling, so be sure + * to present user-friendly error output where the fn is called. + **/ +async function runCommand(cmd: string, flags: yargs.Arguments) { const root = flags.root; switch (cmd) { @@ -114,31 +116,19 @@ export async function cli(args: string[]) { // switch (cmd) { case 'add': { - try { - telemetry.record(event.eventCliSession(cmd)); - const packages = flags._.slice(3) as string[]; - return await add(packages, { cwd: root, flags, logging, telemetry }); - } catch (err) { - return throwAndExit(cmd, err); - } + telemetry.record(event.eventCliSession(cmd)); + const packages = flags._.slice(3) as string[]; + return await add(packages, { cwd: root, flags, logging, telemetry }); } case 'docs': { - try { - telemetry.record(event.eventCliSession(cmd)); - return await openInBrowser('https://docs.astro.build/'); - } catch (err) { - return throwAndExit(cmd, err); - } + telemetry.record(event.eventCliSession(cmd)); + return await openInBrowser('https://docs.astro.build/'); } case 'telemetry': { - try { - // Do not track session start, since the user may be trying to enable, - // disable, or modify telemetry settings. - const subcommand = flags._[3]?.toString(); - return await telemetryHandler.update(subcommand, { flags, telemetry }); - } catch (err) { - return throwAndExit(cmd, err); - } + // Do not track session start, since the user may be trying to enable, + // disable, or modify telemetry settings. + const subcommand = flags._[3]?.toString(); + return await telemetryHandler.update(subcommand, { flags, telemetry }); } } @@ -150,20 +140,12 @@ export async function cli(args: string[]) { // by the end of this switch statement. switch (cmd) { case 'dev': { - try { - await devServer(astroConfig, { logging, telemetry }); - return await new Promise(() => {}); // lives forever - } catch (err) { - return throwAndExit(cmd, err); - } + await devServer(astroConfig, { logging, telemetry }); + return await new Promise(() => {}); // lives forever } case 'build': { - try { - return await build(astroConfig, { logging, telemetry }); - } catch (err) { - return throwAndExit(cmd, err); - } + return await build(astroConfig, { logging, telemetry }); } case 'check': { @@ -172,17 +154,24 @@ export async function cli(args: string[]) { } case 'preview': { - try { - const server = await preview(astroConfig, { logging, telemetry }); - return await server.closed(); // keep alive until the server is closed - } catch (err) { - return throwAndExit(cmd, err); - } + const server = await preview(astroConfig, { logging, telemetry }); + return await server.closed(); // keep alive until the server is closed } } // No command handler matched! This is unexpected. - throwAndExit(cmd, new Error(`Error running ${cmd} -- no command found.`)); + throw new Error(`Error running ${cmd} -- no command found.`); +} + +/** The primary CLI action */ +export async function cli(args: string[]) { + const flags = yargs(args); + const cmd = resolveCommand(flags); + try { + await runCommand(cmd, flags); + } catch (err) { + return throwAndExit(cmd, err); + } } /** Display error and exit */