@@ -9,34 +9,48 @@ import { GitignestFormatter, JsonFormatter } from './formatters';
99import { GitignoreService } from './gitignore' ;
1010
1111export class StructureService {
12- static async getStructure ( dirPath : string ) : Promise < FolderStructure > {
13- const ignoreRules = await GitignoreService . loadRules ( dirPath ) ;
12+ private static relativeFromRoot ( rootUri : vscode . Uri , targetUri : vscode . Uri ) : string {
13+ const root = rootUri . path . replace ( / \/ + $ / , '' ) ;
14+ const target = targetUri . path ;
15+ if ( ! target . startsWith ( root ) ) {
16+ return target . replace ( / ^ \/ + / , '' ) ;
17+ }
18+ return target . slice ( root . length ) . replace ( / ^ \/ + / , '' ) ;
19+ }
20+
21+ private static uriBaseName ( uri : vscode . Uri ) : string {
22+ const segments = uri . path . split ( '/' ) . filter ( Boolean ) ;
23+ return segments [ segments . length - 1 ] ?? uri . path ;
24+ }
25+
26+ static async getStructure ( dirUri : vscode . Uri ) : Promise < FolderStructure > {
27+ const ignoreRules = await GitignoreService . loadRules ( dirUri ) ;
1428 const ig = ignore ( ) . add ( ignoreRules ) ;
15- const folderName = path . basename ( dirPath ) ;
29+ const folderName = this . uriBaseName ( dirUri ) ;
1630 return {
17- [ folderName ] : await this . buildStructure ( dirPath , ig , dirPath ) ,
31+ [ folderName ] : await this . buildStructure ( dirUri , ig , dirUri ) ,
1832 } ;
1933 }
2034
2135 static async buildStructure (
22- dirPath : string ,
36+ dirUri : vscode . Uri ,
2337 ig : ReturnType < typeof ignore > ,
24- rootPath : string ,
38+ rootUri : vscode . Uri ,
2539 ) : Promise < FolderStructure > {
2640 const structure : FolderStructure = { } ;
27- const entries = await FileSystemService . readdir ( dirPath ) ;
41+ const entries = await FileSystemService . readdir ( dirUri ) ;
2842
2943 for ( const entry of entries ) {
30- const fullPath = path . join ( dirPath , entry . name ) ;
31- const relFromRoot = path . relative ( rootPath , fullPath ) ;
44+ const fullUri = vscode . Uri . joinPath ( dirUri , entry . name ) ;
45+ const relFromRoot = this . relativeFromRoot ( rootUri , fullUri ) ;
3246
3347 if ( entry . name . startsWith ( '.' ) || ig . ignores ( relFromRoot ) ) {
3448 continue ;
3549 }
3650
3751 const isDir = ( entry . type & vscode . FileType . Directory ) === vscode . FileType . Directory ;
3852 if ( isDir ) {
39- structure [ entry . name ] = await this . buildStructure ( fullPath , ig , rootPath ) ;
53+ structure [ entry . name ] = await this . buildStructure ( fullUri , ig , rootUri ) ;
4054 } else {
4155 const ext = this . fileTypeFor ( entry . name ) ;
4256 const base = this . baseNameFor ( entry . name ) ;
@@ -60,7 +74,7 @@ export class StructureService {
6074 }
6175
6276 static async createStructure (
63- basePath : string ,
77+ baseUri : vscode . Uri ,
6478 content : string ,
6579 format : OutputFormat ,
6680 ) : Promise < void > {
@@ -69,7 +83,7 @@ export class StructureService {
6983 }
7084
7185 if ( format === 'Plain Text Format' ) {
72- await this . createFromPlainText ( basePath , content ) ;
86+ await this . createFromPlainText ( baseUri , content ) ;
7387 } else {
7488 try {
7589 const structure = JSON . parse ( content ) ;
@@ -78,14 +92,14 @@ export class StructureService {
7892 'Invalid JSON structure: use nested objects for folders and string file types for files' ,
7993 ) ;
8094 }
81- await this . createFromJSON ( basePath , structure ) ;
95+ await this . createFromJSON ( baseUri , structure ) ;
8296 } catch ( error ) {
8397 throw new Error ( 'Invalid JSON format' ) ;
8498 }
8599 }
86100 }
87101
88- private static async createFromPlainText ( basePath : string , content : string ) : Promise < void > {
102+ private static async createFromPlainText ( baseUri : vscode . Uri , content : string ) : Promise < void > {
89103 const rawLines = content . split ( '\n' ) ;
90104 // Ignore the first line (header/title) regardless of its text
91105 const lines = rawLines
@@ -107,7 +121,7 @@ export class StructureService {
107121 pathStack . pop ( ) ;
108122 }
109123
110- const fullPath = path . join ( basePath , ...pathStack , node . name ) ;
124+ const fullPath = vscode . Uri . joinPath ( baseUri , ...pathStack , node . name ) ;
111125
112126 if ( node . isDirectory ) {
113127 await FileSystemService . mkdirIfAbsent ( fullPath ) ;
@@ -126,16 +140,16 @@ export class StructureService {
126140 }
127141
128142 private static async createFromJSON (
129- basePath : string ,
143+ baseUri : vscode . Uri ,
130144 structure : FolderStructure ,
131145 ) : Promise < void > {
132146 for ( const [ key , value ] of Object . entries ( structure ) ) {
133147 if ( typeof value === 'string' ) {
134148 const fileName = value === 'file' || value . trim ( ) === '' ? key : `${ key } .${ value } ` ;
135- const fullPath = path . join ( basePath , fileName ) ;
149+ const fullPath = vscode . Uri . joinPath ( baseUri , fileName ) ;
136150 await FileSystemService . writeFileIfAbsent ( fullPath , '' ) ;
137151 } else {
138- const dirPath = path . join ( basePath , key ) ;
152+ const dirPath = vscode . Uri . joinPath ( baseUri , key ) ;
139153 await FileSystemService . mkdirIfAbsent ( dirPath ) ;
140154 await this . createFromJSON ( dirPath , value ) ;
141155 }
0 commit comments