|
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' |
@@ -74,8 +74,8 @@ export async function resolveDatabaseConfig(nuxt: Nuxt, hub: HubConfig): Promise |
74 | 74 | } |
75 | 75 | break |
76 | 76 | } |
77 | | - // Cloudflare D1 (production only - dev uses local libsql) |
78 | | - if (hub.hosting.includes('cloudflare') && !nuxt.options.dev) { |
| 77 | + // Cloudflare D1 (production only - dev/prepare uses local libsql) |
| 78 | + if (hub.hosting.includes('cloudflare') && !nuxt.options.dev && !nuxt.options._prepare) { |
79 | 79 | config.driver = 'd1' |
80 | 80 | break |
81 | 81 | } |
@@ -274,59 +274,45 @@ 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 | | - if (!nuxt.options._prepare) { |
284 | | - nuxt.hooks.hookOnce('app:templatesGenerated', async () => { |
285 | | - const physicalDbDir = join(nuxt.options.rootDir, 'node_modules', '@nuxthub', 'db') |
286 | | - await mkdir(physicalDbDir, { recursive: true }) |
| 282 | + // Then copy to node_modules/@nuxthub/db/ for workflow compatibility |
| 283 | + const physicalDbDir = join(nuxt.options.rootDir, 'node_modules', '@nuxthub', 'db') |
| 284 | + await mkdir(physicalDbDir, { recursive: true }) |
287 | 285 |
|
288 | | - try { |
289 | | - await copyFile(join(nuxt.options.buildDir, 'hub/db/schema.mjs'), join(physicalDbDir, 'schema.mjs')) |
| 286 | + try { |
| 287 | + await copyFile(join(nuxt.options.buildDir, 'hub/db/schema.mjs'), join(physicalDbDir, 'schema.mjs')) |
290 | 288 |
|
291 | | - // Try to copy the generated .d.mts file for TypeScript support |
292 | | - // The .d.mts is generated in the same directory as schema.mjs |
293 | | - 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 |
| 295 | + try { |
| 296 | + schemaTypes = await readFile(buildDirSource, 'utf-8') |
| 297 | + } catch { |
| 298 | + // Fallback to .nuxt directory (types generated during prepare) |
294 | 299 | try { |
295 | | - const schemaTypes = await readFile(schemaDtsSource, 'utf-8') |
296 | | - await writeFile(join(physicalDbDir, 'schema.d.mts'), schemaTypes) |
| 300 | + schemaTypes = await readFile(nuxtDirSource, 'utf-8') |
297 | 301 | } catch { |
298 | | - // Fallback: create a simple re-export if .d.mts doesn't exist yet |
299 | | - await writeFile( |
300 | | - join(physicalDbDir, 'schema.d.mts'), |
301 | | - `export * from './schema.mjs'` |
302 | | - ) |
| 302 | + // Types not found in either location |
303 | 303 | } |
| 304 | + } |
304 | 305 |
|
305 | | - // Create a minimal package.json for Node.js module resolution |
306 | | - const packageJson = { |
307 | | - name: '@nuxthub/db', |
308 | | - version: '0.0.0', |
309 | | - type: 'module', |
310 | | - exports: { |
311 | | - '.': { |
312 | | - types: './db.d.ts', |
313 | | - default: './db.mjs' |
314 | | - }, |
315 | | - './schema': { |
316 | | - types: './schema.d.mts', |
317 | | - default: './schema.mjs' |
318 | | - } |
319 | | - } |
320 | | - } |
321 | | - await writeFile( |
322 | | - join(physicalDbDir, 'package.json'), |
323 | | - JSON.stringify(packageJson, null, 2) |
324 | | - ) |
325 | | - } catch (error) { |
326 | | - log.warn(`Failed to copy schema to node_modules/.hub/: ${error}`) |
| 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 |
| 310 | + await writeFile(join(physicalDbDir, 'schema.d.mts'), `export * from './schema.mjs'`) |
327 | 311 | } |
328 | | - }) |
329 | | - } |
| 312 | + } catch (error) { |
| 313 | + log.warn(`Failed to copy schema to node_modules/.hub/: ${error}`) |
| 314 | + } |
| 315 | + }) |
330 | 316 |
|
331 | 317 | nuxt.options.alias ||= {} |
332 | 318 | // Create hub:db:schema alias to @nuxthub/db/schema for backwards compatibility |
@@ -597,6 +583,30 @@ export const db: ReturnType<typeof drizzleCore<typeof schema>> |
597 | 583 | physicalDbTypes |
598 | 584 | ) |
599 | 585 |
|
| 586 | + // Create package.json and stub schema files for Node.js module resolution |
| 587 | + // These are needed during `nuxt prepare` so tsconfig paths resolve correctly |
| 588 | + const packageJson = { |
| 589 | + name: '@nuxthub/db', |
| 590 | + version: '0.0.0', |
| 591 | + type: 'module', |
| 592 | + exports: { |
| 593 | + '.': { types: './db.d.ts', default: './db.mjs' }, |
| 594 | + './schema': { types: './schema.d.mts', default: './schema.mjs' } |
| 595 | + } |
| 596 | + } |
| 597 | + try { |
| 598 | + await writeFile(join(physicalDbDir, 'package.json'), JSON.stringify(packageJson, null, 2)) |
| 599 | + // Stub schema files only if they don't exist (real types written by app:templatesGenerated hook) |
| 600 | + const schemaPath = join(physicalDbDir, 'schema.mjs') |
| 601 | + const schemaDtsPath = join(physicalDbDir, 'schema.d.mts') |
| 602 | + const schemaExists = await stat(schemaPath).then(s => s.size > 20).catch(() => false) |
| 603 | + const schemaDtsExists = await stat(schemaDtsPath).then(s => s.size > 20).catch(() => false) |
| 604 | + if (!schemaExists) await writeFile(schemaPath, 'export {}') |
| 605 | + if (!schemaDtsExists) await writeFile(schemaDtsPath, 'export {}') |
| 606 | + } catch (error) { |
| 607 | + throw new Error(`Failed to create @nuxthub/db package files: ${(error as Error).message}`) |
| 608 | + } |
| 609 | + |
600 | 610 | // Create hub:db alias to @nuxthub/db for backwards compatibility |
601 | 611 | nuxt.options.alias!['hub:db'] = '@nuxthub/db' |
602 | 612 |
|
|
0 commit comments