|
1 | 1 | import { readFileSync, existsSync } from "fs"; |
2 | 2 | import { join, dirname } from "path"; |
| 3 | +import { fileURLToPath } from "url"; |
3 | 4 | import { uiMap } from "./uiMap.js"; |
4 | 5 |
|
| 6 | +/** |
| 7 | + * Get the directory of the current module, works in both ESM and CJS. |
| 8 | + */ |
| 9 | +function getCurrentDir(): string { |
| 10 | + if (typeof __dirname !== "undefined") { |
| 11 | + return __dirname; |
| 12 | + } |
| 13 | + return dirname(fileURLToPath(import.meta.url)); |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Find the package root by looking for package.json walking up from the current directory. |
| 18 | + */ |
| 19 | +function findPackageRoot(startDir: string): string { |
| 20 | + let dir = startDir; |
| 21 | + while (dir !== dirname(dir)) { |
| 22 | + if (existsSync(join(dir, "package.json"))) { |
| 23 | + return dir; |
| 24 | + } |
| 25 | + dir = dirname(dir); |
| 26 | + } |
| 27 | + return process.cwd(); |
| 28 | +} |
| 29 | + |
5 | 30 | /** |
6 | 31 | * Get the default UI dist path by finding the package root. |
7 | 32 | */ |
8 | 33 | function getDefaultUIDistPath(): string { |
9 | | - try { |
10 | | - // eslint-disable-next-line @typescript-eslint/no-require-imports |
11 | | - const packageJsonPath = require.resolve("mongodb-mcp-server/package.json"); |
12 | | - const packageRoot = dirname(packageJsonPath); |
13 | | - return join(packageRoot, "dist", "ui"); |
14 | | - } catch { |
15 | | - return join(process.cwd(), "dist", "ui"); |
16 | | - } |
| 34 | + const currentDir = getCurrentDir(); |
| 35 | + const packageRoot = findPackageRoot(currentDir); |
| 36 | + return join(packageRoot, "dist", "ui"); |
17 | 37 | } |
18 | 38 |
|
19 | 39 | /** |
|
0 commit comments