Skip to content

Commit

Permalink
feat: introduce entry boxing to hold id
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Sep 22, 2023
1 parent 965fc8a commit 8b5cfce
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ const tower = createTower({
branch: 'tagtower'
})

const tag: string = 'some@tag'
const entry: Record<string, any> = {
const id: string = 'some@tag'
const data: Record<string, any> = {
hash: '3f9f0a88b411a8932bce289a3dd498d70a4dc96c',
author: 'Anton Golub <[email protected]>',
message: `feat: initial feat`
}

await tower.create(tag, entry) // stores entry to the specified remote
await tower.read(tag) // returns found TEntry | null
await tower.create(id, data) // stores entry to the specified remote
await tower.read(id) // returns found TEntry | null
await tower.read() // if tag is empty, returns TEntry[]
await tower.update(tag, entry) // just a shortcut for delete & create
await tower.delete(tag) // void
await tower.update(id, data) // just a shortcut for delete & create
await tower.delete(id) // void
```

## License
Expand Down
32 changes: 21 additions & 11 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import {readTags, deleteTag, pushTags} from './git'
import {TTower, TTowerFactory, TTowerOpts} from './interface'
import {TTagEntry, TTower, TTowerFactory, TTowerOpts} from './interface'

export const createTower: TTowerFactory = (opts: TTowerOpts): TTower => ({
async create(tag, entry){
await pushTags({...opts, tags: [{tag, body: JSON.stringify(entry)}]})
async create(tag, data){
await pushTags({...opts, tags: [{tag, body: JSON.stringify(data)}]})
},
async read(tag?: string) {
const tags = (await readTags(opts)).map(({tag, body}) => ({tag, entry: JSON.parse(body.trim())}))
if (tag === undefined) {
return tags
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
async read(id?: string) {
const entries: TTagEntry[] = (await readTags(opts)).map(({tag: id, body}) => ({id, data: jsonParse(body)}))
if (id === undefined) {
return entries
}

return tags.find(({tag: _tag}) => _tag === tag)?.entry || null
return entries.find(({id: _id}) => _id === id) || null
},
async delete(tag) {
await deleteTag({...opts, tag})
},
async update(tag, entry) {
await this.delete(tag)
await this.create(tag, entry)
async update(id, data) {
await this.delete(id)
await this.create(id, data)
}
})

const jsonParse = (value: any): TTagEntry['data'] => {
try {
return JSON.parse(value.trim())
} catch {
return {value}
}
}

export * from './interface'
11 changes: 7 additions & 4 deletions src/main/ts/interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

export type TTagEntry = Record<string, any>
export type TTagEntry = {
id: string,
data: Record<string, any>
}

export type TTower = {
create(id: string, entry: TTagEntry): Promise<void>
read(id: string): Promise<TTagEntry|null>
create(id: string, data: TTagEntry['data']): Promise<void>
read(id: string): Promise<TTagEntry | null>
read(): Promise<TTagEntry[]>
update(id: string, entry: TTagEntry): Promise<void>
update(id: string, data: TTagEntry['data']): Promise<void>
delete(id: string): Promise<void>
}

Expand Down
22 changes: 11 additions & 11 deletions src/test/ts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import process from "node:process";
const temp = path.resolve(process.cwd(), 'temp')

describe('tagTower', () => {
it('provides CRUD', async () => {
it('provides CRUD-like API', async () => {
const cwd = path.resolve(temp, 'local-repo')
await fs.mkdir(cwd, { recursive: true })
await exec('git', ['init', '--bare'], {cwd})
Expand All @@ -20,24 +20,24 @@ describe('tagTower', () => {
temp
})

const tag = 'some@tag'
const entry: Record<string, any> = {
const id = 'some@tag'
const data: Record<string, any> = {
hash: '3f9f0a88b411a8932bce289a3dd498d70a4dc96c',
author: 'Anton Golub <[email protected]>',
message: `feat: initial feat`
}

await tower.create(tag, entry)
assert.deepEqual(await tower.read(tag), entry)
await tower.create(id, data)
assert.deepEqual((await tower.read(id))?.data, data)
assert.equal(await tower.read('not-found'), null)

const tags = await tower.read()
assert.ok(tags.some(entry => entry.tag === tag))
const entries = await tower.read()
assert.ok(entries.some(entry => entry.id === id))

await tower.update(tag, {...entry, foo: 'bar'})
assert.equal((await tower.read(tag))?.foo, 'bar')
await tower.update(id, {...data, foo: 'bar'})
assert.equal((await tower.read(id))?.data.foo, 'bar')

await tower.delete(tag)
assert.equal(await tower.read(tag), null)
await tower.delete(id)
assert.equal(await tower.read(id), null)
})
})

0 comments on commit 8b5cfce

Please sign in to comment.