66import { readdir , readFile } from 'fs/promises' ;
77import { join , dirname } from 'path' ;
88import { fileURLToPath } from 'url' ;
9+ import { existsSync } from 'fs' ;
910import type { QsvSkill , SkillCategory } from './types.js' ;
1011
1112const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
@@ -15,8 +16,24 @@ export class SkillLoader {
1516 private skillsDir : string ;
1617
1718 constructor ( skillsDir ?: string ) {
18- // When compiled, __dirname is dist/src/, so go up 2 levels to project root
19- this . skillsDir = skillsDir || join ( __dirname , '../../qsv' ) ;
19+ if ( skillsDir ) {
20+ this . skillsDir = skillsDir ;
21+ } else {
22+ // Try multiple possible locations for the qsv directory
23+ // 1. When built for production: dist/loader.js -> ../qsv
24+ // 2. When built for tests: dist/src/loader.js -> ../../qsv
25+ const productionPath = join ( __dirname , '../qsv' ) ;
26+ const testPath = join ( __dirname , '../../qsv' ) ;
27+
28+ if ( existsSync ( productionPath ) ) {
29+ this . skillsDir = productionPath ;
30+ } else if ( existsSync ( testPath ) ) {
31+ this . skillsDir = testPath ;
32+ } else {
33+ // Fallback to production path (will fail with clearer error)
34+ this . skillsDir = productionPath ;
35+ }
36+ }
2037 }
2138
2239 /**
@@ -111,9 +128,9 @@ export class SkillLoader {
111128 acc [ cat ] = this . getByCategory ( cat ) . length ;
112129 return acc ;
113130 } , { } as Record < string , number > ) ,
114- totalExamples : skills . reduce ( ( sum , s ) => sum + s . examples . length , 0 ) ,
115- totalOptions : skills . reduce ( ( sum , s ) => sum + s . command . options . length , 0 ) ,
116- totalArgs : skills . reduce ( ( sum , s ) => sum + s . command . args . length , 0 )
131+ totalExamples : skills . reduce ( ( sum , s ) => sum + ( s . examples ? .length || 0 ) , 0 ) ,
132+ totalOptions : skills . reduce ( ( sum , s ) => sum + ( s . command . options ? .length || 0 ) , 0 ) ,
133+ totalArgs : skills . reduce ( ( sum , s ) => sum + ( s . command . args ? .length || 0 ) , 0 )
117134 } ;
118135 }
119136
0 commit comments