Skip to content

Commit 1bae236

Browse files
committed
feat: add getJsonFromGit method to the API
1 parent 07a83f2 commit 1bae236

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ steps:
9494

9595
## API
9696

97+
### getJsonFromGit
98+
99+
Extracts JSON block from the current Git message text
100+
101+
```js
102+
const {getJsonFromGit} = require('commit-message-install')
103+
getJsonFromGit()
104+
.then(json => {
105+
// {platform: 'win32', packages: 'foo', branch: 'test-branch'}
106+
})
107+
```
108+
109+
### getInstallJson
110+
97111
You can form good Json object to be included in markdown `json` block in the body of
98112
the commit message using provided function
99113

__snapshots__/commit-message-install-spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,8 @@ exports['commit-message-install getJsonBlock returns json with branch 1'] = {
126126
"foo": "bar",
127127
"branch": "test-branch"
128128
}
129+
130+
exports['commit-message-install getJsonFromGit extracts the json from git message 1'] = {
131+
"platform": "win32",
132+
"branch": "some-branch"
133+
}

src/commit-message-install-spec.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
/* eslint-env mocha */
44
const getMessage = require('.').getMessage
55
const getJsonBlock = require('.').getJsonBlock
6+
const getJsonFromGit = require('.').getJsonFromGit
7+
68
const la = require('lazy-ass')
79
const is = require('check-more-types')
810
const stripIndent = require('common-tags').stripIndent
911
const snapshot = require('snap-shot-it')
1012
const stubSpawnShellOnce = require('stub-spawn-once').stubSpawnShellOnce
1113

1214
describe('commit-message-install', () => {
15+
const getMessageGitCommand = 'git show -s --pretty=%b'
16+
1317
context('gets last commit message', () => {
1418
beforeEach(() => {
15-
stubSpawnShellOnce('git show -s --pretty=%b', 0, 'message body', '')
19+
stubSpawnShellOnce(getMessageGitCommand, 0, 'message body', '')
1620
})
1721

1822
it('returns just the body of the commit message', () => {
@@ -23,6 +27,30 @@ describe('commit-message-install', () => {
2327
})
2428
})
2529

30+
context('getJsonFromGit', () => {
31+
const message = `
32+
below is test json block
33+
34+
\`\`\`json
35+
{
36+
"platform": "win32",
37+
"branch": "some-branch"
38+
}
39+
\`\`\`
40+
`
41+
beforeEach(() => {
42+
stubSpawnShellOnce(getMessageGitCommand, 0, message, '')
43+
})
44+
45+
it('is a function', () => {
46+
la(is.fn(getJsonFromGit))
47+
})
48+
49+
it('extracts the json from git message', () => {
50+
getJsonFromGit().then(snapshot)
51+
})
52+
})
53+
2654
context('--else branch', () => {
2755
const commitMessageInstall = require('../bin/commit-message-install')
2856

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,19 @@ function getInstallJson (packages, env, platform, branch) {
206206
return json
207207
}
208208

209+
// looks at the current Git message,
210+
// extracts JSON block
211+
function getJsonFromGit () {
212+
return getMessage().then(getJsonBlock)
213+
}
214+
209215
module.exports = {
210216
getMessage,
211217
getCommand,
212218
runIf,
213219
isPlatformAllowed,
214220
getJsonBlock,
215221
npmInstall,
216-
getInstallJson
222+
getInstallJson,
223+
getJsonFromGit
217224
}

0 commit comments

Comments
 (0)