Skip to content

Commit

Permalink
feat: --else option for commit-message-install, close #9
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Dec 4, 2017
1 parent 0c84aa6 commit d80025c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 30 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ script:
- node -c bin/commit-message-run.js
- $(npm bin)/if-node-version ">=6" npm test
- DEBUG=commit-message-install node ./bin/commit-message-install.js
- DEBUG=commit-message-install node ./bin/commit-message-install.js --else "echo --else is working"
# synthetic test case
- DEBUG=commit-message-install node ./bin/commit-message-install.js -f test/message.txt
- npm run demo
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ Happy installation
**note** `platform` can be `*` or specific one like `darwin` (from Node `os.platform()`) or a
list of several platforms like `darwin,linux`
### Alternative command
You can specify a command to run *if commit message has no JSON block*. For example you can
install default dependency
```bash
$ $(npm bin)/commit-message-install --else "npm install foo-bar"
```

### Run or skip command based on platform

If the commit message allows a specific platform, you can run any command, while
Expand Down
84 changes: 54 additions & 30 deletions bin/commit-message-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

'use strict'

const args = require('minimist')(process.argv.slice(2), {
alias: {
file: 'f'
},
string: 'file'
})
const debug = require('debug')('commit-message-install')
const execa = require('execa')

const api = require('..')
const getMessage = api.getMessage
Expand All @@ -19,28 +15,56 @@ function onError (e) {
process.exit(1)
}

let start
if (args.file) {
console.log('loading message from file', args.file)
const fs = require('fs')
const message = fs.readFileSync(args.file, 'utf8')
start = Promise.resolve(message)
} else {
start = getMessage()
}
start
.then(getJsonBlock)
.then(json => {
if (!json) {
return
}
// alias for API simplicity
json.packages = json.packages || json.package
if (json.package) {
delete json.package
}
console.log('got json block from the git commit message')
console.log(JSON.stringify(json, null, 2))
return npmInstall(json)
function commitMessageInstall (cliArguments) {
if (!cliArguments) {
cliArguments = process.argv.slice(2)
}

const args = require('minimist')(cliArguments, {
alias: {
file: 'f'
},
string: ['file', 'else']
})
.catch(onError)

let start
if (args.file) {
console.log('loading message from file', args.file)
const fs = require('fs')
const message = fs.readFileSync(args.file, 'utf8')
start = Promise.resolve(message)
} else {
start = getMessage()
}
return start
.then(getJsonBlock)
.then(json => {
if (!json) {
debug('nothing to do from the commit message')
if (args.else) {
debug('have --else command')
debug(args.else)
const options = {
stdio: 'inherit'
}
return execa.shell(args.else, options)
}
return
}
// alias for API simplicity
json.packages = json.packages || json.package
if (json.package) {
delete json.package
}
console.log('got json block from the git commit message')
console.log(JSON.stringify(json, null, 2))
return npmInstall(json)
})
.catch(onError)
}

if (!module.parent) {
commitMessageInstall()
}

module.exports = commitMessageInstall
15 changes: 15 additions & 0 deletions src/commit-message-install-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ describe('commit-message-install', () => {
})
})

context('--else branch', () => {
const commitMessageInstall = require('../bin/commit-message-install')

beforeEach(() => {
stubSpawnShellOnce('git show -s --pretty=%b', 0, 'nothing to do', '')
stubSpawnShellOnce('echo cool', 0, 'cool is working', '')
})

it('executes --else command', () => {
return commitMessageInstall(['--else', 'echo cool']).then(x => {
la(x.stdout === 'cool is working')
})
})
})

context('getInstallJson', () => {
const getInstallJson = require('.').getInstallJson

Expand Down

0 comments on commit d80025c

Please sign in to comment.