Skip to content

Commit eb46de5

Browse files
onmaxatinux
andauthored
fix(db): create package.json during prepare (#797)
Co-authored-by: Sébastien Chopin <seb@nuxt.com>
1 parent 631c4b9 commit eb46de5

1 file changed

Lines changed: 56 additions & 46 deletions

File tree

src/db/setup.ts

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mkdir, copyFile, writeFile, readFile } from 'node:fs/promises'
1+
import { mkdir, copyFile, writeFile, readFile, stat } from 'node:fs/promises'
22
import chokidar from 'chokidar'
33
import { glob } from 'tinyglobby'
44
import { join, resolve as resolveFs, relative } from 'pathe'
@@ -74,8 +74,8 @@ export async function resolveDatabaseConfig(nuxt: Nuxt, hub: HubConfig): Promise
7474
}
7575
break
7676
}
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) {
7979
config.driver = 'd1'
8080
break
8181
}
@@ -274,59 +274,45 @@ async function generateDatabaseSchema(nuxt: Nuxt, hub: ResolvedHubConfig) {
274274
write: true
275275
})
276276

277-
// Build schema types during prepare/dev/build
277+
// Build schema types during prepare/dev/build, then copy to node_modules
278278
nuxt.hooks.hookOnce('app:templatesGenerated', async () => {
279+
// Build first
279280
await buildDatabaseSchema(nuxt.options.buildDir, { relativeDir: nuxt.options.rootDir })
280-
})
281281

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 })
287285

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'))
290288

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)
294299
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')
297301
} 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
303303
}
304+
}
304305

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'`)
327311
}
328-
})
329-
}
312+
} catch (error) {
313+
log.warn(`Failed to copy schema to node_modules/.hub/: ${error}`)
314+
}
315+
})
330316

331317
nuxt.options.alias ||= {}
332318
// Create hub:db:schema alias to @nuxthub/db/schema for backwards compatibility
@@ -597,6 +583,30 @@ export const db: ReturnType<typeof drizzleCore<typeof schema>>
597583
physicalDbTypes
598584
)
599585

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+
600610
// Create hub:db alias to @nuxthub/db for backwards compatibility
601611
nuxt.options.alias!['hub:db'] = '@nuxthub/db'
602612

0 commit comments

Comments
 (0)