|
1 | | -import { mkdir, copyFile, writeFile, readFile } from 'node:fs/promises' |
| 1 | +import { mkdir, copyFile, writeFile, readFile, stat } from 'node:fs/promises' |
2 | 2 | import chokidar from 'chokidar' |
3 | 3 | import { glob } from 'tinyglobby' |
4 | 4 | import { join, resolve as resolveFs, relative } from 'pathe' |
@@ -274,28 +274,39 @@ async function generateDatabaseSchema(nuxt: Nuxt, hub: ResolvedHubConfig) { |
274 | 274 | write: true |
275 | 275 | }) |
276 | 276 |
|
277 | | - // Build schema types during prepare/dev/build |
| 277 | + // Build schema types during prepare/dev/build, then copy to node_modules |
278 | 278 | nuxt.hooks.hookOnce('app:templatesGenerated', async () => { |
| 279 | + // Build first |
279 | 280 | await buildDatabaseSchema(nuxt.options.buildDir, { relativeDir: nuxt.options.rootDir }) |
280 | | - }) |
281 | 281 |
|
282 | | - // Copy schema to node_modules/@nuxthub/db/ for workflow compatibility |
283 | | - nuxt.hooks.hookOnce('app:templatesGenerated', async () => { |
| 282 | + // Then copy to node_modules/@nuxthub/db/ for workflow compatibility |
284 | 283 | const physicalDbDir = join(nuxt.options.rootDir, 'node_modules', '@nuxthub', 'db') |
285 | 284 | await mkdir(physicalDbDir, { recursive: true }) |
286 | 285 |
|
287 | 286 | try { |
288 | 287 | await copyFile(join(nuxt.options.buildDir, 'hub/db/schema.mjs'), join(physicalDbDir, 'schema.mjs')) |
289 | 288 |
|
290 | | - // Copy the generated .d.mts file for TypeScript support (overwrites stub from setupDatabaseClient) |
291 | | - const schemaDtsSource = join(nuxt.options.buildDir, 'hub/db/schema.d.mts') |
| 289 | + // Copy the generated .d.mts file for TypeScript support |
| 290 | + // Try buildDir first, then fall back to .nuxt (for when buildDir is in .cache during build) |
| 291 | + const buildDirSource = join(nuxt.options.buildDir, 'hub/db/schema.d.mts') |
| 292 | + const nuxtDirSource = join(nuxt.options.rootDir, '.nuxt/hub/db/schema.d.mts') |
| 293 | + |
| 294 | + let schemaTypes: string | undefined |
292 | 295 | try { |
293 | | - const schemaTypes = await readFile(schemaDtsSource, 'utf-8') |
294 | | - await writeFile(join(physicalDbDir, 'schema.d.mts'), schemaTypes) |
| 296 | + schemaTypes = await readFile(buildDirSource, 'utf-8') |
295 | 297 | } catch { |
296 | | - // During tests, don't overwrite existing types with stub (preserves dev-generated types) |
297 | | - if (nuxt.options.test) return |
298 | | - // Fallback: create a simple re-export if .d.mts doesn't exist yet |
| 298 | + // Fallback to .nuxt directory (types generated during prepare) |
| 299 | + try { |
| 300 | + schemaTypes = await readFile(nuxtDirSource, 'utf-8') |
| 301 | + } catch { |
| 302 | + // Types not found in either location |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + if (schemaTypes && schemaTypes.length > 50) { |
| 307 | + await writeFile(join(physicalDbDir, 'schema.d.mts'), schemaTypes) |
| 308 | + } else if (!nuxt.options.test) { |
| 309 | + // Fallback: create a simple re-export if types not available |
299 | 310 | await writeFile(join(physicalDbDir, 'schema.d.mts'), `export * from './schema.mjs'`) |
300 | 311 | } |
301 | 312 | } catch (error) { |
@@ -531,9 +542,13 @@ export const db: ReturnType<typeof drizzleCore<typeof schema>> |
531 | 542 | } |
532 | 543 | try { |
533 | 544 | await writeFile(join(physicalDbDir, 'package.json'), JSON.stringify(packageJson, null, 2)) |
534 | | - // Stub schema files (overwritten with actual schema during build) |
535 | | - await writeFile(join(physicalDbDir, 'schema.mjs'), 'export {}') |
536 | | - await writeFile(join(physicalDbDir, 'schema.d.mts'), 'export {}') |
| 545 | + // Stub schema files only if they don't exist (real types written by app:templatesGenerated hook) |
| 546 | + const schemaPath = join(physicalDbDir, 'schema.mjs') |
| 547 | + const schemaDtsPath = join(physicalDbDir, 'schema.d.mts') |
| 548 | + const schemaExists = await stat(schemaPath).then(s => s.size > 20).catch(() => false) |
| 549 | + const schemaDtsExists = await stat(schemaDtsPath).then(s => s.size > 20).catch(() => false) |
| 550 | + if (!schemaExists) await writeFile(schemaPath, 'export {}') |
| 551 | + if (!schemaDtsExists) await writeFile(schemaDtsPath, 'export {}') |
537 | 552 | } catch (error) { |
538 | 553 | throw new Error(`Failed to create @nuxthub/db package files: ${(error as Error).message}`) |
539 | 554 | } |
|
0 commit comments