fix: resolver avisos de build e tipagem

This commit is contained in:
Esdras Renan 2025-11-04 21:02:53 -03:00
parent 741f1d7f9c
commit fa9efdb5af
7 changed files with 154 additions and 59 deletions

View file

@ -1,4 +1,5 @@
import path from "node:path"
import { fileURLToPath, pathToFileURL } from "node:url"
import { PrismaClient } from "@prisma/client"
@ -7,6 +8,12 @@ declare global {
}
// Resolve a robust DATABASE_URL for all runtimes (prod/dev)
const PROJECT_ROOT = process.cwd()
const PROJECT_ROOT_URL = (() => {
const baseHref = pathToFileURL(PROJECT_ROOT).href
return new URL(baseHref.endsWith("/") ? baseHref : `${baseHref}/`)
})()
function resolveFileUrl(url: string) {
if (!url.startsWith("file:")) {
return url
@ -14,13 +21,16 @@ function resolveFileUrl(url: string) {
const filePath = url.slice("file:".length)
if (filePath.startsWith("./") || filePath.startsWith("../")) {
const schemaDir = path.resolve(process.cwd(), "prisma")
const absolutePath = path.resolve(schemaDir, filePath)
const normalized = path.normalize(filePath)
const targetUrl = new URL(normalized, PROJECT_ROOT_URL)
const absolutePath = fileURLToPath(targetUrl)
if (!absolutePath.startsWith(PROJECT_ROOT)) {
throw new Error(`DATABASE_URL path escapes project directory: ${filePath}`)
}
return `file:${absolutePath}`
}
if (!filePath.startsWith("/")) {
const absolutePath = path.resolve(process.cwd(), filePath)
return `file:${absolutePath}`
return resolveFileUrl(`file:./${filePath}`)
}
return url
}