everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
53 lines (51 loc) • 1.91 kB
JavaScript
import { getDatabaseUrlSecretName, getMigrationSlug, getMigrationStorage } from "./core.mjs";
import { existsSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
//#region src/db/identity.ts
/**
* Walk up from a directory to the nearest ancestor containing a package.json.
* Returns the start directory when none is found.
*/
function findWorkspaceDir(startDir) {
let current = startDir;
for (let i = 0; i < 15; i++) {
if (existsSync(join(current, "package.json"))) return current;
const parent = dirname(current);
if (parent === current) break;
current = parent;
}
return startDir;
}
/**
* Derive workspace identity from a workspace directory.
*/
function workspaceIdentityFromWorkspaceDir(dir) {
const workspaceDir = findWorkspaceDir(dir);
const slug = getMigrationSlug(workspaceDir);
return {
slug,
secretName: getDatabaseUrlSecretName(slug),
journal: getMigrationStorage(slug),
workspaceDir
};
}
/**
* Derive workspace identity from a module's own location.
*
* This is the primitive for drizzle.config.ts files: `import.meta.url` is the
* one module-identity fact that survives every config loader we run under —
* real ESM (Node/Bun) and drizzle-kit's bundled tsx CJS transform (which
* defines `import.meta.url` but NOT `import.meta.dirname`, leaving that
* `undefined` in evaluated configs).
*
* Pass the member expression itself (`workspaceIdentityFromModuleUrl(import.meta.url)`)
* so the transform substitutes the value; do not pass the `import.meta` object.
*/
function workspaceIdentityFromModuleUrl(moduleUrl) {
const modulePath = moduleUrl.startsWith("file:") ? fileURLToPath(moduleUrl) : moduleUrl;
return workspaceIdentityFromWorkspaceDir(dirname(modulePath));
}
//#endregion
export { findWorkspaceDir, workspaceIdentityFromModuleUrl, workspaceIdentityFromWorkspaceDir };
//# sourceMappingURL=identity.mjs.map