Skip to content

Commit d80025c

Browse files
committed
feat: --else option for commit-message-install, close #9
1 parent 0c84aa6 commit d80025c

File tree

4 files changed

+79
-30
lines changed

4 files changed

+79
-30
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ script:
1515
- node -c bin/commit-message-run.js
1616
- $(npm bin)/if-node-version ">=6" npm test
1717
- DEBUG=commit-message-install node ./bin/commit-message-install.js
18+
- DEBUG=commit-message-install node ./bin/commit-message-install.js --else "echo --else is working"
1819
# synthetic test case
1920
- DEBUG=commit-message-install node ./bin/commit-message-install.js -f test/message.txt
2021
- npm run demo

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ Happy installation
5252
**note** `platform` can be `*` or specific one like `darwin` (from Node `os.platform()`) or a
5353
list of several platforms like `darwin,linux`
5454

55+
### Alternative command
56+
57+
You can specify a command to run *if commit message has no JSON block*. For example you can
58+
install default dependency
59+
60+
```bash
61+
$ $(npm bin)/commit-message-install --else "npm install foo-bar"
62+
```
63+
5564
### Run or skip command based on platform
5665

5766
If the commit message allows a specific platform, you can run any command, while

bin/commit-message-install.js

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
'use strict'
44

5-
const args = require('minimist')(process.argv.slice(2), {
6-
alias: {
7-
file: 'f'
8-
},
9-
string: 'file'
10-
})
5+
const debug = require('debug')('commit-message-install')
6+
const execa = require('execa')
117

128
const api = require('..')
139
const getMessage = api.getMessage
@@ -19,28 +15,56 @@ function onError (e) {
1915
process.exit(1)
2016
}
2117

22-
let start
23-
if (args.file) {
24-
console.log('loading message from file', args.file)
25-
const fs = require('fs')
26-
const message = fs.readFileSync(args.file, 'utf8')
27-
start = Promise.resolve(message)
28-
} else {
29-
start = getMessage()
30-
}
31-
start
32-
.then(getJsonBlock)
33-
.then(json => {
34-
if (!json) {
35-
return
36-
}
37-
// alias for API simplicity
38-
json.packages = json.packages || json.package
39-
if (json.package) {
40-
delete json.package
41-
}
42-
console.log('got json block from the git commit message')
43-
console.log(JSON.stringify(json, null, 2))
44-
return npmInstall(json)
18+
function commitMessageInstall (cliArguments) {
19+
if (!cliArguments) {
20+
cliArguments = process.argv.slice(2)
21+
}
22+
23+
const args = require('minimist')(cliArguments, {
24+
alias: {
25+
file: 'f'
26+
},
27+
string: ['file', 'else']
4528
})
46-
.catch(onError)
29+
30+
let start
31+
if (args.file) {
32+
console.log('loading message from file', args.file)
33+
const fs = require('fs')
34+
const message = fs.readFileSync(args.file, 'utf8')
35+
start = Promise.resolve(message)
36+
} else {
37+
start = getMessage()
38+
}
39+
return start
40+
.then(getJsonBlock)
41+
.then(json => {
42+
if (!json) {
43+
debug('nothing to do from the commit message')
44+
if (args.else) {
45+
debug('have --else command')
46+
debug(args.else)
47+
const options = {
48+
stdio: 'inherit'
49+
}
50+
return execa.shell(args.else, options)
51+
}
52+
return
53+
}
54+
// alias for API simplicity
55+
json.packages = json.packages || json.package
56+
if (json.package) {
57+
delete json.package
58+
}
59+
console.log('got json block from the git commit message')
60+
console.log(JSON.stringify(json, null, 2))
61+
return npmInstall(json)
62+
})
63+
.catch(onError)
64+
}
65+
66+
if (!module.parent) {
67+
commitMessageInstall()
68+
}
69+
70+
module.exports = commitMessageInstall

src/commit-message-install-spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ describe('commit-message-install', () => {
2323
})
2424
})
2525

26+
context('--else branch', () => {
27+
const commitMessageInstall = require('../bin/commit-message-install')
28+
29+
beforeEach(() => {
30+
stubSpawnShellOnce('git show -s --pretty=%b', 0, 'nothing to do', '')
31+
stubSpawnShellOnce('echo cool', 0, 'cool is working', '')
32+
})
33+
34+
it('executes --else command', () => {
35+
return commitMessageInstall(['--else', 'echo cool']).then(x => {
36+
la(x.stdout === 'cool is working')
37+
})
38+
})
39+
})
40+
2641
context('getInstallJson', () => {
2742
const getInstallJson = require('.').getInstallJson
2843

0 commit comments

Comments
 (0)