Skip to content

Commit 4c2d097

Browse files
committed
fix: sdk-paths
1 parent 6f1ab77 commit 4c2d097

File tree

4 files changed

+78
-18
lines changed

4 files changed

+78
-18
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ jobs:
3636
- name: Run unit tests
3737
run: npm test
3838

39-
- name: Run CLI comprehensive tests
40-
run: |
41-
echo "⚠️ Skipping comprehensive CLI tests in CI due to stdin interaction issues"
42-
echo "✅ These tests should be run manually before releases"
43-
echo " Command: npm run build && node test-cli-comprehensive.js"
44-
4539
template-validation:
4640
runs-on: ubuntu-latest
4741
needs: test

jest.config.cjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ module.exports = {
1414
'^.+\\.(ts|js)$': ['ts-jest', {
1515
useESM: true,
1616
tsconfig: {
17+
module: 'ES2022',
1718
target: 'ES2022',
18-
module: 'ESNext'
19+
moduleResolution: 'node',
20+
esModuleInterop: true,
21+
resolveJsonModule: true
1922
}
2023
}]
2124
},

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: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,82 @@ import fs from "fs";
33
import path from "path";
44
import { fileURLToPath } from "url";
55

6-
const __filename = fileURLToPath(import.meta.url);
7-
const __dirname = path.dirname(__filename);
8-
9-
function findTemplatesDir(maxUp = 5): string {
10-
let dir = __dirname;
11-
for (let i = 0; i <= maxUp; i++) {
12-
const candidate = path.join(dir, "templates");
13-
if (fs.existsSync(candidate)) return candidate;
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
15+
function findSDKRootSync(): string {
16+
let dir = getFileDir();
17+
const maxUp = 10; // Search up to 10 levels
18+
19+
for (let i = 0; i < maxUp; i++) {
20+
const packageJsonPath = path.join(dir, "package.json");
21+
if (fs.existsSync(packageJsonPath)) {
22+
try {
23+
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
24+
// Check if this is the kitdot package
25+
if (pkg.name === 'kitdot') {
26+
return dir;
27+
}
28+
} catch (_e) {
29+
// Invalid package.json, continue searching
30+
}
31+
}
32+
const parent = path.dirname(dir);
33+
if (parent === dir) break; // Reached root
34+
dir = parent;
35+
}
36+
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+
}
1451
const parent = path.dirname(dir);
1552
if (parent === dir) break;
1653
dir = parent;
1754
}
18-
throw new Error("templates folder not found (checked up from __dirname)");
55+
56+
throw new Error('Could not find kitdot package root');
57+
}
58+
59+
// Cache the SDK root
60+
let _sdkRoot: string | null = null;
61+
62+
function getSDKRoot(): string {
63+
if (!_sdkRoot) {
64+
_sdkRoot = findSDKRootSync();
65+
}
66+
return _sdkRoot;
67+
}
68+
69+
export async function findSDKRoot(): Promise<string> {
70+
return getSDKRoot();
71+
}
72+
73+
function findTemplatesDir(): string {
74+
const sdkRoot = getSDKRoot();
75+
const templatesPath = path.join(sdkRoot, "templates");
76+
77+
if (fs.existsSync(templatesPath)) {
78+
return templatesPath;
79+
}
80+
81+
throw new Error(`templates folder not found in SDK root: ${sdkRoot}`);
1982
}
2083

2184
export function resolveTemplatePath(templatePath: string): string {

0 commit comments

Comments
 (0)