Skip to content

Commit 865de85

Browse files
veksenclaude
andcommitted
feat(mikroorm): add project root detection via tsconfig.json
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 274a1a7 commit 865de85

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { existsSync } from "node:fs";
2+
import { dirname, join } from "node:path";
3+
4+
let cachedProjectRoot: string | undefined;
5+
6+
/**
7+
* Finds the project root by walking up from `process.cwd()` looking for `tsconfig.json`.
8+
*
9+
* In deployed environments, `process.cwd()` may not be the project root
10+
* (e.g., `cd .amplify-hosting/compute/default/ && node app.js`).
11+
* Walking up to find `tsconfig.json` — which is never copied to deployment directories —
12+
* gives us the real project root.
13+
*
14+
* The result is cached since the project root doesn't change during a process's lifetime.
15+
*/
16+
export function findProjectRoot(): string {
17+
if (cachedProjectRoot !== undefined) {
18+
return cachedProjectRoot;
19+
}
20+
let projectRoot = process.cwd();
21+
for (let d = projectRoot; d !== dirname(d); d = dirname(d)) {
22+
if (existsSync(join(d, "tsconfig.json"))) {
23+
projectRoot = d;
24+
break;
25+
}
26+
}
27+
cachedProjectRoot = projectRoot;
28+
return projectRoot;
29+
}
30+
31+
/** @internal Exposed for testing only */
32+
export function _resetProjectRootCache() {
33+
cachedProjectRoot = undefined;
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { test } from "node:test";
2+
import assert from "node:assert";
3+
import { existsSync } from "node:fs";
4+
import { join } from "node:path";
5+
import {
6+
findProjectRoot,
7+
_resetProjectRootCache,
8+
} from "../src/path.js";
9+
10+
test("findProjectRoot", async (t) => {
11+
t.afterEach(() => {
12+
_resetProjectRootCache();
13+
});
14+
15+
await t.test("returns a directory containing tsconfig.json", () => {
16+
const root = findProjectRoot();
17+
assert.ok(
18+
existsSync(join(root, "tsconfig.json")),
19+
`Expected ${root} to contain tsconfig.json`,
20+
);
21+
});
22+
23+
await t.test("caches the result across calls", () => {
24+
const first = findProjectRoot();
25+
const second = findProjectRoot();
26+
assert.strictEqual(first, second);
27+
});
28+
});

0 commit comments

Comments
 (0)