generated from antongolub/blank-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce entry boxing to hold id
- Loading branch information
1 parent
965fc8a
commit 8b5cfce
Showing
4 changed files
with
45 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}) | ||
|
@@ -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) | ||
}) | ||
}) |