Skip to content

Commit

Permalink
feat(db): execute command logs (#10439)
Browse files Browse the repository at this point in the history
* feat: db execute success and error logs

* chore: changeset
  • Loading branch information
bholmesdev authored Mar 15, 2024
1 parent f8e0ad3 commit 0989cd3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-mugs-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Add success and error logs to `astro db execute` command
17 changes: 14 additions & 3 deletions packages/db/src/core/cli/commands/execute/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
}
13 changes: 9 additions & 4 deletions packages/db/src/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 0989cd3

Please sign in to comment.