diff options
author | 2024-03-27 15:20:11 -0400 | |
---|---|---|
committer | 2024-03-27 15:20:11 -0400 | |
commit | f5df12cfebba1abdef50faa7a5549b545f0b3f8c (patch) | |
tree | 260f648662f4aa643736eec51a93578851e25ae2 /packages/db/src/core/integration/index.ts | |
parent | bd7effcf720c2a79d130c72b323377f33fb7afe8 (diff) | |
download | astro-f5df12cfebba1abdef50faa7a5549b545f0b3f8c.tar.gz astro-f5df12cfebba1abdef50faa7a5549b545f0b3f8c.tar.zst astro-f5df12cfebba1abdef50faa7a5549b545f0b3f8c.zip |
Provide guidance when --remote is omitted (#10579)
* Provide guidance when --remote is omitted
* Only restrict to server mode
* Use an AstroError
* Update code
Diffstat (limited to 'packages/db/src/core/integration/index.ts')
-rw-r--r-- | packages/db/src/core/integration/index.ts | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/packages/db/src/core/integration/index.ts b/packages/db/src/core/integration/index.ts index e48b95c09..d288475a5 100644 --- a/packages/db/src/core/integration/index.ts +++ b/packages/db/src/core/integration/index.ts @@ -1,7 +1,8 @@ import { existsSync } from 'fs'; import { dirname } from 'path'; import { fileURLToPath } from 'url'; -import type { AstroIntegration } from 'astro'; +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'; @@ -13,6 +14,7 @@ import { fileURLIntegration } from './file-url.js'; import { typegenInternal } from './typegen.js'; import { type LateSeedFiles, type LateTables, vitePluginDb } from './vite-plugin-db.js'; import { vitePluginInjectEnvTs } from './vite-plugin-inject-env-ts.js'; +import { loadEnv } from 'vite'; function astroDBIntegration(): AstroIntegration { let connectToStudio = false; @@ -33,12 +35,14 @@ function astroDBIntegration(): AstroIntegration { }, }; let command: 'dev' | 'build' | 'preview'; + let output: AstroConfig['output'] = 'server'; return { name: 'astro:db', hooks: { 'astro:config:setup': async ({ updateConfig, config, command: _command, logger }) => { command = _command; root = config.root; + output = config.output; if (command === 'preview') return; @@ -111,6 +115,12 @@ function astroDBIntegration(): AstroIntegration { }); }, 'astro:build:start': async ({ logger }) => { + if(!connectToStudio && !databaseFileEnvDefined() && (output === 'server' || output === 'hybrid')) { + const message = `Attempting to build without the --remote flag or the ASTRO_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.'))); }, 'astro:build:done': async ({}) => { @@ -120,6 +130,11 @@ function astroDBIntegration(): AstroIntegration { }; } +function databaseFileEnvDefined() { + const env = loadEnv('', process.cwd()); + return env.ASTRO_DATABASE_FILE != null || process.env.ASTRO_DATABASE_FILE != null; +} + export function integration(): AstroIntegration[] { return [astroDBIntegration(), fileURLIntegration()]; } |