Skip to content

Commit 12543e9

Browse files
committed
refactor(chrono): update ChronoApp.hasFile method
1 parent a6bb852 commit 12543e9

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

packages/chrono/playground/.data/.gitkeep

Whitespace-only changes.

packages/chrono/playground/PlayApp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ const app = new ChronoApp(drive, hash)
99

1010
// app.init()
1111

12-
app.objectService.hashAndSaveFile('message.md')
12+
app.hashFile('message.md')

packages/chrono/src/ChronoApp.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
import IDrive from './gateways/IDrive'
22
import IHash from './gateways/IHash'
3+
import BlobRepositoryImpl from './repositories/BlobRepositoryImpl'
4+
import IBlobRepository from './repositories/IBlobRepository'
5+
import IObjectRepository from './repositories/IObjectRepository'
36
import ObjectRepositoryImpl from './repositories/ObjectRepositoryImpl'
4-
import ObjectService from './services/ObjectService'
57
import HashFileUseCase from './use-cases/HashFileUseCase'
68
import InitUseCase from './use-cases/InitUseCase'
79

810
export default class ChronoApp {
11+
private readonly objectRepository: IObjectRepository
12+
private readonly blobRepository: IBlobRepository
13+
914
constructor(
1015
private readonly drive: IDrive,
1116
private readonly hash: IHash
1217
) {
13-
this.objectService = new ObjectService(drive, hash)
18+
this.objectRepository = new ObjectRepositoryImpl(drive, hash)
19+
this.blobRepository = new BlobRepositoryImpl(drive, hash)
1420
}
1521

16-
public objectService: ObjectService
17-
1822
public async init() {
1923
const useCase = new InitUseCase(this.drive)
2024

2125
await useCase.execute()
2226
}
2327

2428
public async hashFile(path: string) {
25-
const repository = new ObjectRepositoryImpl(this.drive, this.hash)
26-
27-
const useCase = new HashFileUseCase(this.drive, repository)
29+
const useCase = new HashFileUseCase(this.drive, this.objectRepository, this.blobRepository)
2830

2931
await useCase.execute({ path })
3032
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ export default class HashFileUseCase {
1616
) {}
1717

1818
public async execute({ path }: Params) {
19+
if (!(await this.drive.exists(path))) {
20+
throw new BaseException('File not found')
21+
}
22+
1923
const content = await this.drive.read(path)
2024

2125
if (!content) {
22-
throw new BaseException('File not found or can not be read')
26+
throw new BaseException('File can not be read')
2327
}
2428

2529
const { blobHash } = await this.blobRepository.save(content)

packages/chrono/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"include": ["tsup.config.ts" , "src/**/*"],
44
"compilerOptions": {
55
"target": "ESNext",
6-
"module": "ESNext",
6+
"module": "NodeNext",
77
"moduleResolution": "NodeNext",
88
"esModuleInterop": true,
99
"forceConsistentCasingInFileNames": true,

0 commit comments

Comments
 (0)