Skip to content

Commit

Permalink
Merge pull request #9 from tuvistavie/promise-api
Browse files Browse the repository at this point in the history
Add promise API. Close #8.
  • Loading branch information
Daniel Perez committed May 3, 2016
2 parents 10d20a9 + 5b05cbd commit 35761cd
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 219 deletions.
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
language: node_js
node_js:
- '0.11'
- '0.10'
- '5'
- '5.1'
- '4'
- '4.2'
- '4.1'
- '4.0'
- '0.12'
before_script:
- git config --global user.name "Daniel Perez"
- git config --global user.email "tuvistavie@gmail.com"
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ Repository.clone 'https://github.com/tuvistavie/node-git-cli', 'git-cli', (err,
console.log 'pushed to remote'
```

From version 0.10, all functions still take a callback, but also return promises,
so you can rewrite the above as follow:

```javascript
const Repository = require('git-cli').Repository
const fs = require('fs')

Repository.clone('https://github.com/tuvistavie/node-git-cli', 'git-cli')
.then(repo => {
return repo.log()
.then(logs => {
console.log(logs[0].subject)
return repo.showRemote('origin')
}).then(remote => {
console.log(remote.fetchUrl)
fs.writeFileSync("#{repo.workingDir()}/newFile", 'foobar')
return repo.status()
}).then(status => {
console.log(status[0].path)
console.log(status[0].tracked)
return repo.add()
}).then(() => repo.status())
.then(status => {
console.log status[0].path
console.log status[0].tracked
return repo.commit('added newFile')
}).then(() => repo.log())
.then(logs => {
console.log(logs[0].subject)
return repo.push()
}).then(() => console.log('pushed' to remote))
}).catch(e => console.log(e))
```

Checkout out [the tests](test/repository-test.coffee) for more examples.

[travis-build]: https://travis-ci.org/tuvistavie/node-git-cli
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "git-cli",
"version": "0.9.1",
"version": "0.10.0",
"description": "Simple CLI like git interface for NodeJS",
"main": "lib/git-cli.js",
"scripts": {
Expand All @@ -27,9 +27,9 @@
"grunt-contrib-coffee": "^0.10.1",
"grunt-contrib-watch": "^0.6.1",
"grunt-exec": "^0.4.5",
"grunt-mocha-cov": "^0.2.1",
"mocha": "^1.20.0",
"tmp": "0.0.23"
"grunt-mocha-cov": "^0.4.0",
"mocha": "^2.4.5",
"tmp": "0.0.28"
},
"dependencies": {
"string": "^1.8.1",
Expand Down
8 changes: 4 additions & 4 deletions src/branch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ execute = require('./runner').execute

exports.currentBranch = (options, callback) ->
[options, callback] = util.setOptions options, callback
cb = util.wrapCallback callback, (err, stdout) -> gitUtil.parseCurrentBranch stdout
command = new CliCommand(['git', 'branch'], options)
execute command, @_getOptions(), cb
execOptions = processResult: (err, stdout) -> gitUtil.parseCurrentBranch stdout
execute command, @_getOptions(execOptions), callback

exports.branch = (branch, options, callback) ->
branch = [branch] if _.isString(branch)
Expand All @@ -17,6 +17,6 @@ exports.branch = (branch, options, callback) ->
else
[[options, callback], hasName] = [util.setOptions(branch, options), false]
branch = [] unless hasName
cb = util.wrapCallback callback, (err, stdout) -> gitUtil.parseBranches stdout unless hasName
command = new CliCommand(['git', 'branch'], branch, options)
execute command, @_getOptions(), cb
execOptions = processResult: (err, stdout) -> gitUtil.parseBranches stdout unless hasName
execute command, @_getOptions(execOptions), callback
2 changes: 2 additions & 0 deletions src/config.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports =
Promise: Promise
2 changes: 2 additions & 0 deletions src/git-cli.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
errors = require './errors'
config = require './config'

module.exports =
Repository: require './repository'
GitError: errors.GitError
BadRepositoryError: errors.BadRepositoryError
config: config
8 changes: 4 additions & 4 deletions src/remote.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ execute = require('./runner').execute

exports.listRemotes = (options, callback) ->
[options, callback] = util.setOptions options, callback
cb = util.wrapCallback callback, (err, stdout) -> stdout.trim().split "\n"
command = new CliCommand(['git', 'remote', 'show'], options)
execute command, @_getOptions(), cb
execOptions = processResult: (err, stdout) -> stdout.trim().split "\n"
execute command, @_getOptions(execOptions), callback

exports.showRemote = (name, options, callback) ->
[options, callback] = util.setOptions options, callback
cb = util.wrapCallback callback, (err, stdout) -> gitUtil.parseRemote stdout
command = new CliCommand(['git', 'remote', 'show'], name, options)
execute command, @_getOptions(), cb
execOptions = processResult: (err, stdout) -> gitUtil.parseRemote stdout
execute command, @_getOptions(execOptions), callback

exports.addRemote = (name, url, options, callback) ->
[options, callback] = util.setOptions options, callback
Expand Down
8 changes: 4 additions & 4 deletions src/repo-class-methods.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ exports.init = (path, options, callback) ->
[options, callback] = util.setOptions options, callback
fs.ensureDirSync path
command = new CliCommand(['git', 'init'], path, options)
cb = util.wrapCallback callback, (=> new this("#{path}/.git"))
execute command, {}, cb
execOptions = processResult: (=> new this("#{path}/.git"))
execute command, execOptions, callback

exports.clone = (url, path, options, callback) ->
[options, callback] = util.setOptions options, callback
command = new CliCommand(['git', 'clone'], [url, path], options)
cb = util.wrapCallback callback, (=> new this("#{path}/.git"))
execute command, {}, cb
execOptions = processResult: (=> new this("#{path}/.git"))
execute command, execOptions, callback
4 changes: 2 additions & 2 deletions src/repository.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Repository

workingDir: -> path.dirname @path

_getOptions: ->
cwd: @workingDir()
_getOptions: (extra={}) ->
_.assign({cwd: @workingDir()}, extra)

_.each require('./repo-class-methods'), (method, key) ->
Repository[key] = method
Expand Down
13 changes: 10 additions & 3 deletions src/runner.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
exec = require('child_process').exec
Promise = require('./config').Promise

Runner =
execute: (command, options, callback) ->
exec command.toString(), options, (err, stdout, stderr) ->
if callback?
callback err, stdout, stderr
new Promise (resolve, reject) ->
exec command.toString(), options, (err, stdout, stderr) ->
result = if options.processResult then options.processResult(err, stdout, stderr) else stdout
if callback?
callback(err, result, stderr)
if err?
reject(err, result, stderr)
else
resolve(result, stderr)

module.exports = Runner
21 changes: 11 additions & 10 deletions src/stats.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ execute = require('./runner').execute
util = require './util'
CliCommand = require './cli-command'

exports.diff = (options, callback) ->
exports.diff = (options={}, callback) ->
[options, callback] = util.setOptions options, callback
args = @_getDiffArgs(options)
command = new CliCommand(['git', 'diff'], args, options)
execute command, @_getOptions(), callback

exports.diffStats = (options, callback) ->
exports.diffStats = (options={}, callback) ->
[options, callback] = util.setOptions options, callback
args = @_getDiffArgs(options)
cliOpts = _.extend({ shortstat: '' }, options)
command = new CliCommand(['git', 'diff'], args, cliOpts)
cb = util.wrapCallback callback, (err, stdout) -> gitUtil.parseShortDiff(stdout)
execute command, @_getOptions(), cb
execOptions = processResult: (err, stdout) -> gitUtil.parseShortDiff(stdout)
execute command, @_getOptions(execOptions), callback

exports._getDiffArgs = (options) ->
args = []
Expand All @@ -30,15 +30,16 @@ exports._getDiffArgs = (options) ->
exports.status = (options, callback) ->
[options, callback] = util.setOptions options, callback
command = new CliCommand(['git', 'status'], _.extend({ s: '' }, options))
cb = util.wrapCallback callback, (err, stdout) =>
statusInfo = gitUtil.parseStatus(stdout)
_.each(statusInfo, (f) => f.fullPath = "#{@workingDir()}/#{f.path}")
execute command, @_getOptions(), cb
execOptions =
processResult: (err, stdout) =>
statusInfo = gitUtil.parseStatus(stdout)
_.each(statusInfo, (f) => f.fullPath = "#{@workingDir()}/#{f.path}")
execute command, @_getOptions(execOptions), callback

exports.log = (options={}, callback) ->
[options, callback] = util.setOptions options, callback
format = '{"author": "%an", "email": "%ae", "date": "%cd", "subject": "%s", "body": "%b", "hash": "%H"},'
cliOpts = _.extend({ pretty: "format:#{format}" }, options)
command = new CliCommand(['git', 'log'], cliOpts)
cb = util.wrapCallback callback, (err, stdout) -> gitUtil.parseLog(stdout)
execute command, @_getOptions(), cb
execOptions = processResult: (err, stdout) -> gitUtil.parseLog(stdout)
execute command, @_getOptions(execOptions), callback
7 changes: 0 additions & 7 deletions src/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,4 @@ util =
else
[options, callback ? null]

wrapCallback: (callback, handler) ->
return unless callback?
done = callback
(err, stdout, stderr) ->
args = handler err, stdout, stderr
done err, args

module.exports = util

0 comments on commit 35761cd

Please sign in to comment.