Skip to content

Commit

Permalink
Use an AstroError
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Mar 27, 2024
1 parent 32ba90a commit 895c4d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
9 changes: 6 additions & 3 deletions packages/db/src/core/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync } from 'fs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import type { AstroConfig, AstroIntegration } from 'astro';
import { AstroError } from 'astro/errors';
import { mkdir, writeFile } from 'fs/promises';
import { blue, yellow } from 'kleur/colors';
import parseArgs from 'yargs-parser';
Expand Down Expand Up @@ -114,8 +115,10 @@ function astroDBIntegration(): AstroIntegration {
});
},
'astro:build:start': async ({ logger }) => {
if(!connectToStudio && !databaseFileEnvDefined() && output === 'server') {
throw new Error(`Attempting to build without the --remote flag or the DATABASE_FILE environment variable defined. You probably want to pass --remote to astro build.`)
if(!connectToStudio && !databaseFileEnvDefined() && (output === 'server' || output === 'hybrid')) {
const message = `Attempting to build without the --remote flag or the DATABASE_FILE environment variable defined. You probably want to pass --remote to astro build.`;
const hint = 'Learn more connecting to Studio: https://docs.astro.build/en/guides/astro-db/#connect-to-astro-studio';
throw new AstroError(message, hint);
}

logger.info('database: ' + (connectToStudio ? yellow('remote') : blue('local database.')));
Expand All @@ -129,7 +132,7 @@ function astroDBIntegration(): AstroIntegration {

function databaseFileEnvDefined() {
const env = loadEnv('', process.cwd());
return env.DATABASE_FILE != null || process.env.DATABASE_FILE != null;
return env.ASTRO_DATABASE_FILE != null || process.env.ASTRO_DATABASE_FILE != null;
}

export function integration(): AstroIntegration[] {
Expand Down
26 changes: 22 additions & 4 deletions packages/db/test/local-prod.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ describe('astro:db local database', () => {
describe('build (not remote) with DATABASE_FILE env', () => {
const prodDbPath = new URL('./fixtures/basics/dist/astro.db', import.meta.url).toString();
before(async () => {
process.env.DATABASE_FILE = prodDbPath;
process.env.ASTRO_DATABASE_FILE = prodDbPath;
await fixture.build();
});

after(async () => {
delete process.env.DATABASE_FILE;
delete process.env.ASTRO_DATABASE_FILE;
});

it('Can render page', async () => {
Expand All @@ -32,8 +32,8 @@ describe('astro:db local database', () => {
});

describe('build (not remote)', () => {
it('should throw during the build', async () => {
delete process.env.DATABASE_FILE;
it('should throw during the build for server output', async () => {
delete process.env.ASTRO_DATABASE_FILE;
let buildError = null;
try {
await fixture.build();
Expand All @@ -43,5 +43,23 @@ describe('astro:db local database', () => {

expect(buildError).to.be.an('Error');
});

it('should throw during the build for hybrid output', async () => {
let fixture2 = await loadFixture({
root: new URL('./fixtures/local-prod/', import.meta.url),
output: 'hybrid',
adapter: testAdapter(),
});

delete process.env.ASTRO_DATABASE_FILE;
let buildError = null;
try {
await fixture2.build();
} catch(err) {
buildError = err;
}

expect(buildError).to.be.an('Error');
});
});
});

0 comments on commit 895c4d4

Please sign in to comment.