Skip to content

Commit 6daedd7

Browse files
authored
feat: added readBuildstamp (#20)
1 parent 620c5d3 commit 6daedd7

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,20 @@ Output in `some/path/stamp.json`:
8888
"date": "2020-09-04T19:53:03.790Z"
8989
}
9090
```
91+
### readBuildstamp(path)
92+
Safely parses and returns buildstamp by given path. Returns `undefined` on error.
93+
```javascript
94+
import { readBuildstamp } from 'buildstamp'
95+
96+
const stamp = getBuildstamp('some/path')
97+
/*
98+
{
99+
git: {
100+
commitId: 'fc6e78b11ef4c7db1c8b89fa6b0d9b3ad4ad481d',
101+
repoName: 'qiwi/buildstamp.git'
102+
},
103+
docker: { imageTag: 'foo', bar: 'bar' },
104+
date: '2020-08-27T20:47:41.958Z'
105+
}
106+
*/
107+
```

src/main/ts/getter.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { readFileToString } from './utils'
2+
import { TStamp } from './interfaces'
3+
4+
export const readBuildstamp = (stampPath: string): TStamp | undefined => {
5+
try {
6+
return JSON.parse(readFileToString(stampPath))
7+
} catch (e) {
8+
console.error('Buildstamp reading error:', e.message)
9+
}
10+
return undefined
11+
}

src/main/ts/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ import { execute } from './executor'
22

33
export * from './interfaces'
44
export { execute }
5+
export { readBuildstamp } from './getter'

src/test/ts/getter.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { readBuildstamp, TStamp } from '../../main/ts'
2+
import * as utils from '../../main/ts/utils'
3+
4+
const stamp: TStamp = {
5+
git: {
6+
commitId: 'foo',
7+
repoName: 'bar',
8+
repoUrl: 'baz',
9+
},
10+
date: 0,
11+
docker: {
12+
imageTag: 'bat',
13+
},
14+
}
15+
16+
describe('readBuildstamp', () => {
17+
it('reads and parses file', () => {
18+
jest.spyOn(utils, 'readFileToString')
19+
.mockImplementation(() => JSON.stringify(stamp))
20+
expect(readBuildstamp('some/path')).toEqual(stamp)
21+
})
22+
23+
it('logs an error', () => {
24+
jest.spyOn(utils, 'readFileToString')
25+
.mockImplementation(() => {
26+
throw new Error('foo')
27+
})
28+
const errorSpy = jest.spyOn(console, 'error')
29+
.mockImplementation(() => { /* noop */ })
30+
expect(readBuildstamp('some/path')).toBeUndefined()
31+
expect(errorSpy).toHaveBeenCalled()
32+
})
33+
34+
afterAll(jest.resetAllMocks)
35+
})

0 commit comments

Comments
 (0)