Commit 6daedd7 1 parent 620c5d3 commit 6daedd7 Copy full SHA for 6daedd7
File tree 4 files changed +64
-0
lines changed
4 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -88,3 +88,20 @@ Output in `some/path/stamp.json`:
88
88
"date" : " 2020-09-04T19:53:03.790Z"
89
89
}
90
90
```
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
+ ```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -2,3 +2,4 @@ import { execute } from './executor'
2
2
3
3
export * from './interfaces'
4
4
export { execute }
5
+ export { readBuildstamp } from './getter'
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments