From 0989cd3284281e3e471a92ac116e14e65f59f8a5 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Fri, 15 Mar 2024 13:58:36 -0400 Subject: [PATCH] feat(db): `execute` command logs (#10439) * feat: db execute success and error logs * chore: changeset --- .changeset/hip-mugs-know.md | 5 +++++ .../db/src/core/cli/commands/execute/index.ts | 17 ++++++++++++++--- packages/db/src/core/errors.ts | 13 +++++++++---- 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 .changeset/hip-mugs-know.md diff --git a/.changeset/hip-mugs-know.md b/.changeset/hip-mugs-know.md new file mode 100644 index 0000000000..321c3e494f --- /dev/null +++ b/.changeset/hip-mugs-know.md @@ -0,0 +1,5 @@ +--- +"@astrojs/db": patch +--- + +Add success and error logs to `astro db execute` command diff --git a/packages/db/src/core/cli/commands/execute/index.ts b/packages/db/src/core/cli/commands/execute/index.ts index 7fd1261821..606245bbe9 100644 --- a/packages/db/src/core/cli/commands/execute/index.ts +++ b/packages/db/src/core/cli/commands/execute/index.ts @@ -4,7 +4,8 @@ import type { Arguments } from 'yargs-parser'; import { FILE_NOT_FOUND_ERROR, MISSING_EXECUTE_PATH_ERROR, - SEED_DEFAULT_EXPORT_ERROR, + EXEC_DEFAULT_EXPORT_ERROR, + EXEC_ERROR, } from '../../../errors.js'; import { getLocalVirtualModContents, @@ -13,6 +14,8 @@ import { import { bundleFile, importBundledFile } from '../../../load-file.js'; import { getManagedAppTokenOrExit } from '../../../tokens.js'; import { type DBConfig } from '../../../types.js'; +import { LibsqlError } from '@libsql/client'; +import { green } from 'kleur/colors'; export async function cmd({ astroConfig, @@ -54,8 +57,16 @@ export async function cmd({ const mod = await importBundledFile({ code, root: astroConfig.root }); if (typeof mod.default !== 'function') { - console.error(SEED_DEFAULT_EXPORT_ERROR); + console.error(EXEC_DEFAULT_EXPORT_ERROR); process.exit(1); } - await mod.default(); + try { + await mod.default(); + console.info(`${green('✔')} File run successfully.`); + } catch (e) { + if (e instanceof LibsqlError) { + throw new Error(EXEC_ERROR(e.message)); + } + throw e; + } } diff --git a/packages/db/src/core/errors.ts b/packages/db/src/core/errors.ts index 0724a74e99..198c86b787 100644 --- a/packages/db/src/core/errors.ts +++ b/packages/db/src/core/errors.ts @@ -41,11 +41,16 @@ export const SEED_ERROR = (error: string) => { return `${red(`Error while seeding database:`)}\n\n${error}`; }; +export const EXEC_ERROR = (error: string) => { + return `${red(`Error while executing file:`)}\n\n${error}`; +}; + export const SEED_DEFAULT_EXPORT_ERROR = (fileName: string) => { - return ( - red('Error while seeding database:') + - `\n\nMissing default function export in ${bold(fileName)}` - ); + return SEED_ERROR(`Missing default function export in ${bold(fileName)}`); +}; + +export const EXEC_DEFAULT_EXPORT_ERROR = (fileName: string) => { + return EXEC_ERROR(`Missing default function export in ${bold(fileName)}`); }; export const REFERENCE_DNE_ERROR = (columnName: string) => {