@@ -3,19 +3,82 @@ import fs from "fs";
33import path from "path" ;
44import { 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
2184export function resolveTemplatePath ( templatePath : string ) : string {
0 commit comments