UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

1 lines • 3.13 kB
{"version":3,"file":"identity.mjs","names":[],"sources":["../../src/db/identity.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport {\n getDatabaseUrlSecretName,\n getMigrationSlug,\n getMigrationStorage,\n type MigrationStorage,\n} from \"./core\";\n\n/**\n * The database identity of a workspace: everything downstream consumers need\n * (drizzle-kit configs, migration runners, tooling CLIs) is a pure function of\n * these values. Identity has one source of truth — the workspace's\n * package.json — and must never be guessed from ambient process state\n * (cwd, npm_package_name, shell env).\n */\nexport interface WorkspaceIdentity {\n readonly slug: string;\n readonly secretName: string;\n readonly journal: MigrationStorage;\n readonly workspaceDir: string | undefined;\n}\n\n/**\n * Walk up from a directory to the nearest ancestor containing a package.json.\n * Returns the start directory when none is found.\n */\nexport function findWorkspaceDir(startDir: string): string {\n let current = startDir;\n for (let i = 0; i < 15; i++) {\n if (existsSync(join(current, \"package.json\"))) return current;\n const parent = dirname(current);\n if (parent === current) break;\n current = parent;\n }\n return startDir;\n}\n\n/**\n * Derive workspace identity from a workspace directory.\n */\nexport function workspaceIdentityFromWorkspaceDir(dir: string): WorkspaceIdentity {\n const workspaceDir = findWorkspaceDir(dir);\n const slug = getMigrationSlug(workspaceDir);\n return {\n slug,\n secretName: getDatabaseUrlSecretName(slug),\n journal: getMigrationStorage(slug),\n workspaceDir,\n };\n}\n\n/**\n * Derive workspace identity from a module's own location.\n *\n * This is the primitive for drizzle.config.ts files: `import.meta.url` is the\n * one module-identity fact that survives every config loader we run under —\n * real ESM (Node/Bun) and drizzle-kit's bundled tsx CJS transform (which\n * defines `import.meta.url` but NOT `import.meta.dirname`, leaving that\n * `undefined` in evaluated configs).\n *\n * Pass the member expression itself (`workspaceIdentityFromModuleUrl(import.meta.url)`)\n * so the transform substitutes the value; do not pass the `import.meta` object.\n */\nexport function workspaceIdentityFromModuleUrl(moduleUrl: string): WorkspaceIdentity {\n const modulePath = moduleUrl.startsWith(\"file:\") ? fileURLToPath(moduleUrl) : moduleUrl;\n return workspaceIdentityFromWorkspaceDir(dirname(modulePath));\n}\n"],"mappings":";;;;;;;;;;AA4BA,SAAgB,iBAAiB,UAA0B;CACzD,IAAI,UAAU;CACd,KAAK,IAAI,IAAI,GAAG,IAAI,IAAI,KAAK;EAC3B,IAAI,WAAW,KAAK,SAAS,cAAc,CAAC,GAAG,OAAO;EACtD,MAAM,SAAS,QAAQ,OAAO;EAC9B,IAAI,WAAW,SAAS;EACxB,UAAU;CACZ;CACA,OAAO;AACT;;;;AAKA,SAAgB,kCAAkC,KAAgC;CAChF,MAAM,eAAe,iBAAiB,GAAG;CACzC,MAAM,OAAO,iBAAiB,YAAY;CAC1C,OAAO;EACL;EACA,YAAY,yBAAyB,IAAI;EACzC,SAAS,oBAAoB,IAAI;EACjC;CACF;AACF;;;;;;;;;;;;;AAcA,SAAgB,+BAA+B,WAAsC;CACnF,MAAM,aAAa,UAAU,WAAW,OAAO,IAAI,cAAc,SAAS,IAAI;CAC9E,OAAO,kCAAkC,QAAQ,UAAU,CAAC;AAC9D"}