11import * as fs from 'fs' ;
2+ import { IS_WINDOWS } from './platform.js' ;
23
34/**
45 * Derive cwd from a slug directory name using recursive backtracking.
@@ -11,13 +12,30 @@ import * as fs from 'fs';
1112export function cwdFromSlug ( slug : string ) : string {
1213 const parts = slug . split ( '-' ) ;
1314 let best : string | null = null ;
15+ const isDriveSlug = parts . length > 0 && / ^ [ A - Z a - z ] $ / . test ( parts [ 0 ] || '' ) ;
16+
17+ function candidatePaths ( segments : string [ ] ) : string [ ] {
18+ const unixPath = '/' + segments . join ( '/' ) ;
19+ if ( segments . length > 0 && / ^ [ A - Z a - z ] $ / . test ( segments [ 0 ] || '' ) ) {
20+ const drive = segments [ 0 ] . toUpperCase ( ) ;
21+ const rest = segments . slice ( 1 ) . join ( '/' ) ;
22+ const winPath = rest ? `${ drive } :/${ rest } ` : `${ drive } :/` ;
23+ // On Windows prefer drive-letter paths; on Unix keep legacy order.
24+ return IS_WINDOWS ? [ winPath , unixPath ] : [ unixPath , winPath ] ;
25+ }
26+ return [ unixPath ] ;
27+ }
1428
1529 function resolve ( idx : number , segments : string [ ] ) : void {
1630 if ( best ) return ; // already found a match
1731
1832 if ( idx >= parts . length ) {
19- const p = '/' + segments . join ( '/' ) ;
20- if ( fs . existsSync ( p ) ) best = p ;
33+ for ( const p of candidatePaths ( segments ) ) {
34+ if ( fs . existsSync ( p ) ) {
35+ best = p ;
36+ break ;
37+ }
38+ }
2139 return ;
2240 }
2341
@@ -41,7 +59,15 @@ export function cwdFromSlug(slug: string): string {
4159 }
4260
4361 resolve ( 0 , [ ] ) ;
44- return best || '/' + slug . replace ( / - / g, '/' ) ;
62+ if ( best ) return best ;
63+
64+ if ( isDriveSlug && IS_WINDOWS ) {
65+ const drive = parts [ 0 ] . toUpperCase ( ) ;
66+ const rest = parts . slice ( 1 ) . join ( '/' ) ;
67+ return rest ? `${ drive } :/${ rest } ` : `${ drive } :/` ;
68+ }
69+
70+ return '/' + slug . replace ( / - / g, '/' ) ;
4571}
4672
4773/**
0 commit comments