Skip to content

Commit ade24f4

Browse files
committed
refactor(chrono): rename Entry to IndexEntry
1 parent d00285f commit ade24f4

15 files changed

+283
-113
lines changed

packages/chrono/src/ChronoApp.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,33 @@ import IHash from './gateways/IHash'
33

44
import IBlobRepository from './repositories/IBlobRepository'
55
import IObjectRepository from './repositories/IObjectRepository'
6-
import IStageItemRepository from './repositories/IStageItemRepository'
76

87
import LocalObjectRepository from './repositories/implementations/LocalObjectRepository'
98
import LocalBlobRepository from './repositories/implementations/LocalBlobRepository'
10-
import LocalStageItemRepository from './repositories/implementations/LocalStageItemRepository'
119

1210
import InitUseCase from './use-cases/InitUseCase'
1311
import CatFileUseCase from './use-cases/CatFileUseCase'
1412
import HashFileUseCase from './use-cases/HashEntryUseCase'
1513
import RemoveUseCase from './use-cases/RemoveUseCase'
1614
import CommitUseCase from './use-cases/CommitUseCase'
1715
import StatusUseCase from './use-cases/StatusUseCase'
18-
import IEntryRepository from './repositories/IEntryRepository'
16+
import IIndexEntryRepository from './repositories/IIndexEntryRepository'
1917
import LocalEntryRepository from './repositories/implementations/LocalEntryRepository'
2018
import AddUseCase from './use-cases/AddUseCase'
2119
import ListFilesUseCase from './use-cases/ListFilesUseCase'
22-
import HashEntryService from './services/HashEntryService'
2320

2421
export default class ChronoApp {
2522
private readonly objectRepository: IObjectRepository
2623
private readonly blobRepository: IBlobRepository
27-
28-
private readonly stageItemRepository: IStageItemRepository
29-
private readonly entryRepository: IEntryRepository
30-
31-
private readonly hashEntryService: HashEntryService
24+
private readonly entryRepository: IIndexEntryRepository
3225

3326
constructor(
3427
private readonly drive: IDrive,
3528
private readonly hash: IHash
3629
) {
3730
this.objectRepository = new LocalObjectRepository(drive, hash)
3831
this.blobRepository = new LocalBlobRepository(drive, hash)
39-
40-
this.stageItemRepository = new LocalStageItemRepository(drive)
41-
this.entryRepository = new LocalEntryRepository(drive, hash)
42-
43-
this.hashEntryService = new HashEntryService(
44-
drive,
45-
this.objectRepository,
46-
this.blobRepository
47-
)
32+
this.entryRepository = new LocalEntryRepository(drive)
4833
}
4934

5035
public async init() {
@@ -100,7 +85,12 @@ export default class ChronoApp {
10085
}
10186

10287
public async status() {
103-
const useCase = new StatusUseCase(this.drive, this.entryRepository)
88+
const useCase = new StatusUseCase(
89+
this.drive,
90+
this.objectRepository,
91+
this.blobRepository,
92+
this.entryRepository
93+
)
10494

10595
return useCase.execute()
10696
}

packages/chrono/src/entities/ChronoEntry.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

packages/chrono/src/entities/ChronoObject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default class ChronoObject {
4545
}
4646
}
4747

48-
public static from(object: Record<string, any>, body?: string) {
48+
public static fromObject(object: Record<string, any>, body?: string) {
4949
let content = ''
5050

5151
Object.entries(object).forEach(([key, value]) => {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import ChronoObject from './ChronoObject'
2+
3+
interface Payload {
4+
message: string
5+
tree: string
6+
parent?: string
7+
body?: string
8+
}
9+
10+
export default class ChronoObjectCommit extends ChronoObject {
11+
public get message() {
12+
return this.head.message
13+
}
14+
15+
public get tree() {
16+
return this.head.tree
17+
}
18+
19+
public get parent() {
20+
return this.head.parent
21+
}
22+
23+
public static from(payload: Payload) {
24+
const object = ChronoObject.fromObject(
25+
{
26+
type: 'commit',
27+
message: payload.message,
28+
tree: payload.tree,
29+
parent: payload.parent,
30+
},
31+
payload.body
32+
)
33+
34+
return new ChronoObjectCommit(object.content)
35+
}
36+
37+
public serialize() {
38+
return {
39+
...super.serialize(),
40+
message: this.message,
41+
}
42+
}
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export enum IndexEntryStatus {
2+
Unmodified = 0,
3+
Modified = 1,
4+
Added = 2,
5+
Deleted = 3,
6+
Renamed = 4,
7+
Copied = 5,
8+
}
9+
10+
export default class IndexEntry {
11+
public path: string
12+
public hash: string
13+
public status: IndexEntryStatus
14+
15+
public static STATUS = IndexEntryStatus
16+
17+
constructor(
18+
path: string,
19+
hash: string,
20+
status: IndexEntryStatus = IndexEntryStatus.Unmodified
21+
) {
22+
this.path = path
23+
this.hash = hash
24+
this.status = status
25+
}
26+
27+
public static from({ path, hash, status }: Pick<IndexEntry, 'path' | 'hash' | 'status'>) {
28+
return new IndexEntry(path, hash, status)
29+
}
30+
}

packages/chrono/src/repositories/IEntryRepository.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import IndexEntry from '../entities/IndexEntry'
2+
3+
export default interface IIndexEntryRepository {
4+
findAll(): Promise<IndexEntry[]>
5+
saveAll(entries: IndexEntry[]): Promise<void>
6+
}

packages/chrono/src/repositories/implementations/LocalEntryRepository.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
import ChronoEntry from '../../entities/ChronoEntry'
1+
import IndexEntry from '../../entities/IndexEntry'
22
import IDrive from '../../gateways/IDrive'
3-
import IHash from '../../gateways/IHash'
43
import HelperService from '../../services/HelperService'
5-
import IEntryRepository from '../IEntryRepository'
4+
import IIndexEntryRepository from '../IIndexEntryRepository'
65

7-
export default class LocalEntryRepository implements IEntryRepository {
8-
constructor(
9-
private readonly drive: IDrive,
10-
private readonly hash: IHash
11-
) {}
6+
export default class LocalEntryRepository implements IIndexEntryRepository {
7+
constructor(private readonly drive: IDrive) {}
128

13-
public findAll: IEntryRepository['findAll'] = async () => {
9+
public findAll: IIndexEntryRepository['findAll'] = async () => {
1410
const contents = await this.drive.read('.chrono/index')
1511

1612
const json = JSON.parse(contents ? HelperService.decode(contents) : '[]')
1713

18-
return json.map((i: any) => new ChronoEntry(i.path, i.hash, i.status))
14+
const entries = json.map((i: any) => new IndexEntry(i.path, i.hash, i.status))
15+
16+
return entries
1917
}
2018

21-
public saveAll: IEntryRepository['saveAll'] = async (entries) => {
19+
public saveAll: IIndexEntryRepository['saveAll'] = async (entries) => {
2220
await this.drive.write(
2321
'.chrono/index',
2422
HelperService.encode(JSON.stringify(entries, null, 4))

packages/chrono/src/services/HashEntryService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default class HashEntryService {
2121

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

24-
const blobObject = ChronoObject.from({
24+
const blobObject = ChronoObject.fromObject({
2525
type: 'blob',
2626
blobHash,
2727
})

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import ChronoEntry from '../entities/ChronoEntry'
1+
import IndexEntry from '../entities/IndexEntry'
22
import IDrive from '../gateways/IDrive'
33

44
import IBlobRepository from '../repositories/IBlobRepository'
5-
import IEntryRepository from '../repositories/IEntryRepository'
5+
import IIndexEntryRepository from '../repositories/IIndexEntryRepository'
66
import IObjectRepository from '../repositories/IObjectRepository'
77

88
import HashEntryService from '../services/HashEntryService'
@@ -16,7 +16,7 @@ export default class AddUseCase {
1616
private readonly drive: IDrive,
1717
private readonly objectRepository: IObjectRepository,
1818
private readonly blobRepository: IBlobRepository,
19-
private readonly entryRepository: IEntryRepository
19+
private readonly entryRepository: IIndexEntryRepository
2020
) {}
2121

2222
public async addEntry(path: string) {
@@ -31,11 +31,25 @@ export default class AddUseCase {
3131

3232
const entry = entries.find((entry) => entry.path === path)
3333

34-
if (entry) return entry
35-
3634
const { objectHash } = await hashService.hashEntry(path)
3735

38-
const newEntry = new ChronoEntry(path, objectHash, ChronoEntry.STATUS.Added)
36+
if (entry && entry.hash === objectHash) {
37+
entry.status = IndexEntry.STATUS.Unmodified
38+
39+
await this.entryRepository.saveAll(entries)
40+
41+
return entry
42+
}
43+
44+
if (entry) {
45+
entry.status = IndexEntry.STATUS.Added
46+
47+
await this.entryRepository.saveAll(entries)
48+
49+
return entry
50+
}
51+
52+
const newEntry = new IndexEntry(path, objectHash, IndexEntry.STATUS.Added)
3953

4054
entries.push(newEntry)
4155

@@ -45,7 +59,7 @@ export default class AddUseCase {
4559
}
4660

4761
public async execute({ path }: Params) {
48-
const result = [] as ChronoEntry[]
62+
const result = [] as IndexEntry[]
4963

5064
if (await this.drive.isFile(path)) {
5165
const entry = await this.addEntry(path)

packages/chrono/src/use-cases/CatFileUseCase.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('CatFileUseCase', () => {
1717
await drive.write('message.md', HelperService.encode('Hello World!'))
1818

1919
const { objectHash } = await objectRepository.save(
20-
ChronoObject.from({
20+
ChronoObject.fromObject({
2121
type: 'blob',
2222
blobHash: '123',
2323
})

0 commit comments

Comments
 (0)