You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

33 lines
1.1 KiB

import { existsSync, readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
function isDrizzlePkgRoot(dir: string): boolean {
const pkg = path.join(dir, "package.json");
if (!existsSync(pkg)) {
return false;
}
try {
const { name } = JSON.parse(readFileSync(pkg, "utf8")) as { name?: string };
return name === "drizzle-pkg";
} catch {
return false;
}
}
/**
* `drizzle-pkg` 包根目录(与 `package.json`、`db.sqlite` 同级)。
* - 开发:用 `import.meta` 锚定,避免 cwd 变化把 `file:db.sqlite` 指到错误文件(只读 / DBMOVED)。
* - 生产:打包后 chunk 路径不可靠,回退到 `cwd/packages/drizzle-pkg`;部署时建议使用绝对 `DATABASE_URL`。
*/
export function getDrizzlePkgRoot(): string {
const fromMeta = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
if (isDrizzlePkgRoot(fromMeta)) {
return fromMeta;
}
const fromCwd = path.resolve(process.cwd(), "packages/drizzle-pkg");
if (isDrizzlePkgRoot(fromCwd)) {
return fromCwd;
}
return fromMeta;
}