-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathindex.ts
192 lines (177 loc) · 6.6 KB
/
index.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { existsSync } from 'fs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import type { AstroConfig, AstroIntegration } from 'astro';
import { mkdir, writeFile } from 'fs/promises';
import { blue, yellow } from 'kleur/colors';
import { loadEnv } from 'vite';
import parseArgs from 'yargs-parser';
import { SEED_DEV_FILE_NAME } from '../../runtime/queries.js';
import { AstroDbError } from '../../runtime/utils.js';
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, resolved, vitePluginDb } from './vite-plugin-db.js';
import { vitePluginInjectEnvTs } from './vite-plugin-inject-env-ts.js';
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,
output: config.output,
});
} else {
dbPlugin = vitePluginDb({
connectToStudio: false,
tables,
seedFiles,
root: config.root,
srcDir: config.srcDir,
output: config.output,
});
}
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:setup': async ({ server, logger }) => {
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();
}
});
// Wait for dev server log before showing "connected".
setTimeout(() => {
logger.info(
connectToStudio ? 'Connected to remote database.' : 'New local database created.'
);
if (connectToStudio) return;
const localSeedPaths = SEED_DEV_FILE_NAME.map(
(name) => new URL(name, getDbDirectoryUrl(root))
);
let seedInFlight = false;
// Load seed file on dev server startup.
if (seedFiles.get().length || localSeedPaths.find((path) => existsSync(path))) {
loadSeedModule();
}
const eagerReloadIntegrationSeedPaths = seedFiles
.get()
// Map integration seed paths to URLs, if possible.
// Module paths like `@example/seed` will be ignored
// from eager reloading.
.map((s) => (typeof s === 'string' && s.startsWith('.') ? new URL(s, root) : s))
.filter((s): s is URL => s instanceof URL);
const eagerReloadSeedPaths = [...eagerReloadIntegrationSeedPaths, ...localSeedPaths];
server.watcher.on('all', (event, relativeEntry) => {
if (event === 'unlink' || event === 'unlinkDir') return;
// When a seed file changes, load manually
// to track when seeding finishes and log a message.
const entry = new URL(relativeEntry, root);
if (eagerReloadSeedPaths.find((path) => entry.href === path.href)) {
loadSeedModule();
}
});
function loadSeedModule() {
if (seedInFlight) return;
seedInFlight = true;
const mod = server.moduleGraph.getModuleById(resolved.seedVirtual);
if (mod) server.moduleGraph.invalidateModule(mod);
server
.ssrLoadModule(resolved.seedVirtual)
.then(() => {
logger.info('Seeded database.');
})
.catch((e) => {
logger.error(e instanceof Error ? e.message : String(e));
})
.finally(() => {
seedInFlight = false;
});
}
}, 100);
},
'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 AstroDbError(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()];
}