Skip to content

Commit 5293ee6

Browse files
committed
fix: properly handle import.meta.url with ESM support
Use import.meta.url to resolve SDK paths from file location instead of process.cwd(). This ensures templates are found correctly whether kitdot is run from source or installed as a global package. Changes: - Use import.meta.url with fileURLToPath to get current file directory - Search upward from file location to find package.json with name='kitdot' - Fallback to cwd search if file-based search fails (for test compat) - Enable Node's experimental VM modules for Jest ESM support - Inline TypeScript config in jest.config.cjs (remove tsconfig.test.json) - Configure ts-jest with ES2022 module support All 73 tests passing locally.
1 parent 92a4544 commit 5293ee6

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

jest.config.cjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ module.exports = {
1313
transform: {
1414
'^.+\\.(ts|js)$': ['ts-jest', {
1515
useESM: true,
16-
tsconfig: 'tsconfig.test.json'
16+
tsconfig: {
17+
module: 'ES2022',
18+
target: 'ES2022',
19+
moduleResolution: 'node',
20+
esModuleInterop: true,
21+
resolveJsonModule: true
22+
}
1723
}]
1824
},
1925
moduleNameMapper: {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"start": "node dist/cli.js",
1414
"lint": "eslint src/**/*.ts",
1515
"lint:fix": "eslint src/**/*.ts --fix",
16-
"test": "jest",
17-
"test:templates": "jest --testPathPattern=template-validation",
16+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
17+
"test:templates": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPattern=template-validation",
1818
"prepublishOnly": "npm run build"
1919
},
2020
"keywords": [

src/utils/sdk-paths.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
// src/sdk-paths.ts
22
import fs from "fs";
33
import path from "path";
4+
import { fileURLToPath } from "url";
45

5-
// Find the SDK root by searching upward from current working directory
6-
// This approach works in all environments without needing import.meta or __dirname
6+
// Get the directory of this source file
7+
// In production: dist/utils/sdk-paths.js
8+
// In development: src/utils/sdk-paths.ts
9+
function getFileDir(): string {
10+
return path.dirname(fileURLToPath(import.meta.url));
11+
}
12+
13+
// Find the SDK root by searching upward from this file's location
14+
// This works whether the package is installed globally or run from source
715
function findSDKRootSync(): string {
8-
let dir = process.cwd();
16+
let dir = getFileDir();
917
const maxUp = 10; // Search up to 10 levels
1018

1119
for (let i = 0; i < maxUp; i++) {
@@ -14,7 +22,7 @@ function findSDKRootSync(): string {
1422
try {
1523
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
1624
// Check if this is the kitdot package
17-
if (pkg.name === 'kitdot' || fs.existsSync(path.join(dir, 'templates'))) {
25+
if (pkg.name === 'kitdot') {
1826
return dir;
1927
}
2028
} catch (_e) {
@@ -26,8 +34,26 @@ function findSDKRootSync(): string {
2634
dir = parent;
2735
}
2836

29-
// Fallback to cwd if we can't find the SDK root
30-
return process.cwd();
37+
// Fallback: search from cwd (useful for tests)
38+
dir = process.cwd();
39+
for (let i = 0; i < maxUp; i++) {
40+
const packageJsonPath = path.join(dir, "package.json");
41+
if (fs.existsSync(packageJsonPath)) {
42+
try {
43+
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
44+
if (pkg.name === 'kitdot') {
45+
return dir;
46+
}
47+
} catch (_e) {
48+
// Invalid package.json, continue searching
49+
}
50+
}
51+
const parent = path.dirname(dir);
52+
if (parent === dir) break;
53+
dir = parent;
54+
}
55+
56+
throw new Error('Could not find kitdot package root');
3157
}
3258

3359
// Cache the SDK root

tsconfig.test.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)