Skip to content

Commit

Permalink
feat: provide parse/format customization
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Sep 22, 2023
1 parent 7cdd321 commit 1c2a8e1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const tower = createTower({
branch: 'tagtower', // Branch for storing tags. Defaults to 'tagtower'
temp: './temp', // Dir to hold temporary git channels. Defaults to fs.mkdtemp(path.join(os.tmpdir(), 'tempy-tagtower-'))
committerName: 'Foo Bar', // Username and email to sign annotaged git tags
committerEmail: '[email protected]' // Defaults to Semrel Extra Bot <[email protected]>
committerEmail: '[email protected]', // Defaults to Semrel Extra Bot <[email protected]>
format: v => v + '', // Opt value formatter. Defaults to JSON.stringify
parse: v => v // Opt parser. Defaults to JSON.parse
})

const id: string = 'some@tag'
Expand Down
9 changes: 6 additions & 3 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {TTagEntry, TTower, TTowerFactory, TTowerOpts} from './interface'

export const createTower: TTowerFactory = (opts: TTowerOpts): TTower => ({
async create(tag, data){
await pushTags({...opts, tags: [{tag, body: JSON.stringify(data)}]})
const formatter = opts.format || format
await pushTags({...opts, tags: [{tag, body: formatter(data)}]})
},
// 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)}))
const parser = opts.parse || parse
const entries: TTagEntry[] = (await readTags(opts)).map(({tag: id, body}) => ({id, data: parser(body)}))
if (id === undefined) {
return entries
}
Expand All @@ -24,7 +26,8 @@ export const createTower: TTowerFactory = (opts: TTowerOpts): TTower => ({
}
})

const jsonParse = (value: any): TTagEntry['data'] => {
const format = JSON.stringify
const parse = (value: any): TTagEntry['data'] => {
try {
return JSON.parse(value.trim())
} catch {
Expand Down
4 changes: 3 additions & 1 deletion src/main/ts/interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

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

export type TTower = {
Expand All @@ -20,6 +20,8 @@ export type TTowerOpts = {
temp?: string
committerName?: string
committerEmail?: string
parse?: (v: string) => any
format?: (v: any) => string
}

export type TAnnotatedTag = {
Expand Down
17 changes: 17 additions & 0 deletions src/test/ts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,21 @@ describe('tagTower', () => {
await tower.delete(id)
assert.equal(await tower.read(id), null)
})

it('supports custom parse/format', async () => {
const cwd = path.resolve(temp, 'custom-format')
await fs.mkdir(cwd, { recursive: true })
await exec('git', ['init', '--bare'], {cwd})

const tower = createTower({
url: cwd,
branch: 'test-tag-tower',
temp,
format(v) { return `--${v}--`},
parse(v) { return `++${v.trim().slice(2, -2)}++` }
})

await tower.create('foo', 'bar')
assert.equal((await tower.read('foo'))?.data, '++bar++')
})
})

0 comments on commit 1c2a8e1

Please sign in to comment.