@@ -8,7 +8,7 @@ import IObjectRepository from '../repositories/IObjectRepository'
8
8
import HashEntryService from '../services/HashEntryService'
9
9
10
10
interface Params {
11
- path : string
11
+ path : string | string [ ]
12
12
}
13
13
14
14
export default class AddUseCase {
@@ -19,7 +19,7 @@ export default class AddUseCase {
19
19
private readonly entryRepository : IIndexEntryRepository
20
20
) { }
21
21
22
- public async addEntry ( path : string ) {
22
+ public async addFileEntry ( path : string ) {
23
23
const hashService = new HashEntryService (
24
24
this . drive ,
25
25
this . objectRepository ,
@@ -58,28 +58,41 @@ export default class AddUseCase {
58
58
return newEntry
59
59
}
60
60
61
- public async execute ( { path } : Params ) {
61
+ public async addDirectoryEntry ( path : string ) {
62
62
const result = [ ] as IndexEntry [ ]
63
63
64
- if ( await this . drive . isFile ( path ) ) {
65
- const entry = await this . addEntry ( path )
66
-
67
- result . push ( entry )
68
-
69
- return result
70
- }
71
-
72
64
const files = await this . drive . readdir ( path , {
73
65
recursive : true ,
74
66
onlyFiles : true ,
75
67
} )
76
68
77
69
for await ( const file of files ) {
78
- const entry = await this . addEntry ( `${ path } /${ file } ` )
70
+ const entry = await this . addFileEntry ( `${ path } /${ file } ` )
79
71
80
72
result . push ( entry )
81
73
}
82
74
83
75
return result
84
76
}
77
+
78
+ public async execute ( { path } : Params ) {
79
+ const result = [ ] as IndexEntry [ ]
80
+ const paths = Array . isArray ( path ) ? path : [ path ]
81
+
82
+ for await ( const p of paths ) {
83
+ if ( await this . drive . isFile ( p ) ) {
84
+ const entry = await this . addFileEntry ( p )
85
+
86
+ result . push ( entry )
87
+
88
+ continue
89
+ }
90
+
91
+ const entries = await this . addDirectoryEntry ( p )
92
+
93
+ result . push ( ...entries )
94
+ }
95
+
96
+ return result
97
+ }
85
98
}
0 commit comments