Skip to content

Commit d64ab2b

Browse files
committed
refactor(chrono): allow multiple paths on AddUseCase
1 parent 19aea29 commit d64ab2b

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

packages/chrono/src/use-cases/AddUseCase.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import IObjectRepository from '../repositories/IObjectRepository'
88
import HashEntryService from '../services/HashEntryService'
99

1010
interface Params {
11-
path: string
11+
path: string | string[]
1212
}
1313

1414
export default class AddUseCase {
@@ -19,7 +19,7 @@ export default class AddUseCase {
1919
private readonly entryRepository: IIndexEntryRepository
2020
) {}
2121

22-
public async addEntry(path: string) {
22+
public async addFileEntry(path: string) {
2323
const hashService = new HashEntryService(
2424
this.drive,
2525
this.objectRepository,
@@ -58,28 +58,41 @@ export default class AddUseCase {
5858
return newEntry
5959
}
6060

61-
public async execute({ path }: Params) {
61+
public async addDirectoryEntry(path: string) {
6262
const result = [] as IndexEntry[]
6363

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-
7264
const files = await this.drive.readdir(path, {
7365
recursive: true,
7466
onlyFiles: true,
7567
})
7668

7769
for await (const file of files) {
78-
const entry = await this.addEntry(`${path}/${file}`)
70+
const entry = await this.addFileEntry(`${path}/${file}`)
7971

8072
result.push(entry)
8173
}
8274

8375
return result
8476
}
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+
}
8598
}

0 commit comments

Comments
 (0)