66import { specsService } from '../specs/service' ;
77import type { Spec } from './schema' ;
88import { detectSubSpecs } from '../sub-specs' ;
9- import { join } from 'path' ;
9+ import { join , resolve } from 'path' ;
1010import { readFileSync } from 'node:fs' ;
1111import matter from 'gray-matter' ;
1212
@@ -17,6 +17,23 @@ export type ParsedSpec = Omit<Spec, 'tags'> & {
1717 tags : string [ ] | null ;
1818} ;
1919
20+ const DEFAULT_SPECS_DIR = resolve ( process . cwd ( ) , '../../specs' ) ;
21+
22+ function getSpecsRootDir ( ) : string {
23+ const envDir = process . env . SPECS_DIR ;
24+ if ( ! envDir ) {
25+ return DEFAULT_SPECS_DIR ;
26+ }
27+ return envDir . startsWith ( '/' ) ? envDir : resolve ( process . cwd ( ) , envDir ) ;
28+ }
29+
30+ function buildSpecDirPath ( filePath : string ) : string {
31+ const normalized = filePath
32+ . replace ( / ^ s p e c s \/ / , '' )
33+ . replace ( / \/ R E A D M E \. m d $ / , '' ) ;
34+ return join ( getSpecsRootDir ( ) , normalized ) ;
35+ }
36+
2037interface SpecRelationships {
2138 dependsOn : string [ ] ;
2239 related : string [ ] ;
@@ -113,7 +130,7 @@ export async function getSpecsWithSubSpecCount(projectId?: string): Promise<(Par
113130 }
114131
115132 return specs . map ( spec => {
116- const specDirPath = join ( process . cwd ( ) , '../../specs' , spec . filePath . replace ( '/README.md' , '' ) . replace ( 'specs/' , '' ) ) ;
133+ const specDirPath = buildSpecDirPath ( spec . filePath ) ;
117134 const subSpecsCount = countSubSpecs ( specDirPath ) ;
118135 return { ...parseSpecTags ( spec ) , subSpecsCount } ;
119136 } ) ;
@@ -131,7 +148,7 @@ export async function getSpecById(id: string, projectId?: string): Promise<(Pars
131148
132149 // Detect sub-specs from filesystem (only for filesystem mode)
133150 if ( ! projectId ) {
134- const specDirPath = join ( process . cwd ( ) , '../../specs' , spec . filePath . replace ( '/README.md' , '' ) . replace ( 'specs/' , '' ) ) ;
151+ const specDirPath = buildSpecDirPath ( spec . filePath ) ;
135152 const subSpecs = detectSubSpecs ( specDirPath ) ;
136153 const relationships = getFilesystemRelationships ( specDirPath ) ;
137154 return { ...parsedSpec , subSpecs, relationships } ;
0 commit comments