1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
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';
import { CONFIG_FILE_NAMES, DB_PATH } from '../consts.js';
import { resolveDbConfig } from '../load-file.js';
import { type ManagedAppToken, getManagedAppTokenOrExit } from '../tokens.js';
import { type VitePlugin, getDbDirectoryUrl } from '../utils.js';
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;
let configFileDependencies: string[] = [];
let root: URL;
let appToken: ManagedAppToken | undefined;
// Make table loading "late" to pass to plugins from `config:setup`,
// but load during `config:done` to wait for integrations to settle.
let tables: LateTables = {
get() {
throw new Error('[astro:db] INTERNAL Tables not loaded yet');
},
};
let seedFiles: LateSeedFiles = {
get() {
throw new Error('[astro:db] INTERNAL Seed files not loaded yet');
},
};
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;
let dbPlugin: VitePlugin | undefined = undefined;
const args = parseArgs(process.argv.slice(3));
connectToStudio = process.env.ASTRO_INTERNAL_TEST_REMOTE || args['remote'];
if (connectToStudio) {
appToken = await getManagedAppTokenOrExit();
dbPlugin = vitePluginDb({
connectToStudio,
appToken: appToken.token,
tables,
root: config.root,
srcDir: config.srcDir,
});
} else {
dbPlugin = vitePluginDb({
connectToStudio: false,
tables,
seedFiles,
root: config.root,
srcDir: config.srcDir,
});
}
updateConfig({
vite: {
assetsInclude: [DB_PATH],
plugins: [dbPlugin, vitePluginInjectEnvTs(config, logger)],
},
});
},
'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 { dbConfig, dependencies, integrationSeedPaths } = await resolveDbConfig(config);
tables.get = () => dbConfig.tables;
seedFiles.get = () => integrationSeedPaths;
configFileDependencies = dependencies;
const localDbUrl = new URL(DB_PATH, config.root);
if (!connectToStudio && !existsSync(localDbUrl)) {
await mkdir(dirname(fileURLToPath(localDbUrl)), { recursive: true });
await writeFile(localDbUrl, '');
}
await typegenInternal({ tables: tables.get() ?? {}, root: config.root });
},
'astro:server:start': async ({ logger }) => {
// Wait for the server startup to log, so that this can come afterwards.
setTimeout(() => {
logger.info(
connectToStudio ? 'Connected to remote database.' : 'New local database created.'
);
}, 100);
},
'astro:server:setup': async ({ server }) => {
const filesToWatch = [
...CONFIG_FILE_NAMES.map((c) => new URL(c, getDbDirectoryUrl(root))),
...configFileDependencies.map((c) => new URL(c, root)),
];
server.watcher.on('all', (event, relativeEntry) => {
const entry = new URL(relativeEntry, root);
if (filesToWatch.some((f) => entry.href === f.href)) {
server.restart();
}
});
},
'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 ({}) => {
await appToken?.destroy();
},
},
};
}
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()];
}
|