0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-24 22:46:02 -05:00

refactor: ensure precedence of file name

This commit is contained in:
bholmesdev 2024-02-27 12:13:48 -05:00
parent 2299a9c227
commit 203dbe369f
4 changed files with 41 additions and 34 deletions

View file

@ -12,18 +12,3 @@ export const DB_TYPES_FILE = 'db-types.d.ts';
export const VIRTUAL_MODULE_ID = 'astro:db';
export const DB_PATH = '.astro/content.db';
export const SEED_DEV_FILE_NAMES = [
'seed.ts',
'seed.js',
'seed.mjs',
'seed.mts',
'seed.dev.ts',
'seed.dev.js',
'seed.dev.mjs',
'seed.dev.mts',
'seed.development.ts',
'seed.development.js',
'seed.development.mjs',
'seed.development.mts',
];

View file

@ -1,14 +1,7 @@
import { existsSync } from 'node:fs';
import {
DB_PATH,
RUNTIME_DRIZZLE_IMPORT,
RUNTIME_IMPORT,
SEED_DEV_FILE_NAMES,
VIRTUAL_MODULE_ID,
} from '../consts.js';
import { SEED_DEV_FILE_NAMES_SORTED } from '../../runtime/queries.js';
import { DB_PATH, RUNTIME_DRIZZLE_IMPORT, RUNTIME_IMPORT, VIRTUAL_MODULE_ID } from '../consts.js';
import type { DBTables } from '../types.js';
import { getDbDirUrl, getRemoteDatabaseUrl, type VitePlugin } from '../utils.js';
import { fileURLToPath } from 'node:url';
const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID;
@ -67,9 +60,11 @@ export function getLocalVirtualModContents({
isDev: boolean;
}) {
const dbUrl = new URL(DB_PATH, root);
const devSeedFile = SEED_DEV_FILE_NAMES.map((f) =>
fileURLToPath(new URL(f, getDbDirUrl(root)))
).find((f) => existsSync(f));
const seedFilePaths = SEED_DEV_FILE_NAMES_SORTED.map(
// Format as /db/[name].ts
// for Vite import.meta.glob
(name) => new URL(name, getDbDirUrl('file:///')).pathname
);
return `
import { collectionToTable, createLocalDatabaseClient, seedDev } from ${RUNTIME_IMPORT};
@ -83,11 +78,11 @@ ${getStringifiedCollectionExports(tables)}
// TODO: test error logging to see if try / catch is needed
${
isDev && devSeedFile
isDev
? `await seedDev({
db,
tables: ${JSON.stringify(tables)},
runSeed: () => import(${JSON.stringify(devSeedFile)}),
fileGlob: import.meta.glob(${JSON.stringify(seedFilePaths)}),
});`
: ''
}

View file

@ -18,6 +18,6 @@ export function getAstroStudioUrl(): string {
return env.ASTRO_STUDIO_URL || 'https://stardate.astro.build';
}
export function getDbDirUrl(root: URL) {
return new URL('db/', root);
export function getDbDirUrl(rootPath: string) {
return new URL('db/', rootPath);
}

View file

@ -22,17 +22,44 @@ import { SEED_EMPTY_ARRAY_ERROR } from '../core/errors.js';
const sqlite = new SQLiteAsyncDialect();
/**
* Sorted by precedence.
* Ex. If both "seed.dev.ts" and "seed.ts" are present,
* "seed.dev.ts" will be used.
*/
export const SEED_DEV_FILE_NAMES_SORTED = [
'seed.development.ts',
'seed.development.js',
'seed.development.mjs',
'seed.development.mts',
'seed.dev.ts',
'seed.dev.js',
'seed.dev.mjs',
'seed.dev.mts',
'seed.ts',
'seed.js',
'seed.mjs',
'seed.mts',
];
export async function seedDev({
db,
tables,
runSeed,
// Glob all potential seed files to catch renames and deletions.
fileGlob,
}: {
db: SqliteRemoteDatabase;
tables: DBTables;
runSeed: () => Promise<void>;
fileGlob: Record<string, () => Promise<void>>;
}) {
await recreateTables({ db, tables });
await runSeed();
for (const fileName of SEED_DEV_FILE_NAMES_SORTED) {
const key = Object.keys(fileGlob).find((f) => new RegExp(`${fileName}$`).test(f));
if (key) {
await fileGlob[key]();
return;
}
}
}
export async function recreateTables({