0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00
This commit is contained in:
Fred K. Schott 2024-03-02 01:44:25 -08:00
parent 2db9031a9f
commit 96bfb75886
5 changed files with 30 additions and 3 deletions

View file

@ -163,6 +163,7 @@ export interface CLIFlags {
host?: string | boolean;
port?: number;
config?: string;
stage?: string;
open?: string | boolean;
}
@ -502,6 +503,17 @@ export interface AstroUserConfig {
*/
trailingSlash?: 'always' | 'never' | 'ignore';
/**
* @docs
* @name stage
* @type {('development' | 'production')}
* @description
*
* TODO
*/
stage?: 'development' | 'production';
/**
* @docs
* @name redirects

View file

@ -68,6 +68,7 @@ export function resolveFlags(flags: Partial<Flags>): CLIFlags {
typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined,
open:
typeof flags.open === 'string' || typeof flags.open === 'boolean' ? flags.open : undefined,
stage: typeof flags.stage === 'string' ? flags.stage : undefined,
};
}

View file

@ -116,6 +116,9 @@ export const AstroConfigSchema = z.object({
.array(z.object({ name: z.string(), hooks: z.object({}).passthrough().default({}) }))
.default(ASTRO_CONFIG_DEFAULTS.integrations)
),
stage: z
.union([z.literal('development'), z.literal('production')])
.optional(),
build: z
.object({
format: z

View file

@ -30,17 +30,22 @@ function astroDBIntegration(): AstroIntegration {
},
};
let command: 'dev' | 'build' | 'preview';
return {
name: 'astro:db',
hooks: {
'astro:config:setup': async ({ updateConfig, config, command: _command, logger }) => {
if (command === 'preview') return;
command = _command;
root = config.root;
if (command === 'preview') return;
let dbPlugin: VitePlugin | undefined = undefined;
connectToStudio = command === 'build';
if (!config.stage && command === 'build') {
throw new Error('stage is required in db/config.js');
} else {
connectToStudio = dbConfig.stage === 'production';
}
if (connectToStudio) {
appToken = await getManagedAppTokenOrExit();
@ -68,6 +73,7 @@ function astroDBIntegration(): AstroIntegration {
});
},
'astro:config:done': async ({ config }) => {
if (command === 'preview') return;
// TODO: refine where we load tables
// @matthewp: may want to load tables by path at runtime
const { mod, dependencies } = await loadDbConfigFile(config.root);
@ -90,6 +96,7 @@ function astroDBIntegration(): AstroIntegration {
await typegen({ tables: tables.get() ?? {}, root: config.root });
},
'astro:server:start': async ({ logger }) => {
if (command === 'preview') return;
// Wait for the server startup to log, so that this can come afterwards.
setTimeout(() => {
logger.info(
@ -98,6 +105,7 @@ function astroDBIntegration(): AstroIntegration {
}, 100);
},
'astro:server:setup': async ({ server }) => {
if (command === 'preview') return;
const filesToWatch = [
...CONFIG_FILE_NAMES.map((c) => new URL(c, getDbDirectoryUrl(root))),
...configFileDependencies.map((c) => new URL(c, root)),
@ -110,9 +118,11 @@ function astroDBIntegration(): AstroIntegration {
});
},
'astro:build:start': async ({ logger }) => {
if (command === 'preview') return;
logger.info('database: ' + (connectToStudio ? yellow('remote') : blue('local database.')));
},
'astro:build:done': async ({}) => {
if (command === 'preview') return;
await appToken?.destroy();
},
},

View file

@ -240,6 +240,7 @@ export type DBSnapshot = {
export const dbConfigSchema = z.object({
tables: tablesSchema.optional(),
stage: z.union([z.literal('development'), z.literal('production')]).optional(),
});
export type DBConfigInput = z.input<typeof dbConfigSchema>;