Skip to content

Commit

Permalink
Improve usability of CLI options. Bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
danhper committed Aug 15, 2014
1 parent 13b6d7f commit 39acfc6
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 111 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "git-cli",
"version": "0.5.0",
"version": "0.6.0",
"description": "Simple CLI like git interface for NodeJS",
"main": "lib/git-cli.js",
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion src/git-util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ GitUtil =
deletions: stats[2]
}


parseLog: (logStr) ->
logStr = '[' + logStr[0...-1] + ']'
logs = JSON.parse logStr.replace /\n/g, ""
Expand Down
174 changes: 86 additions & 88 deletions src/repository.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,85 +18,85 @@ class Repository
throw new errors.BadRepositoryError(BAD_PATH_MSG)

@init: (path, options, callback) ->
options = Util.setOptions options, callback
[options, callback] = Util.setOptions options, callback
fs.ensureDirSync path
command = new CliCommand('git', ['init', path], options.cli)
if options.callback
callback = options.callback
options.callback = (err, stdout, stderr) ->
command = new CliCommand('git', ['init', path], options)
if callback
done = callback
callback = (err, stdout, stderr) ->
repository = new Repository("#{path}/.git")
callback err, repository
Runner.execute command, options
done err, repository
Runner.execute command, {}, callback

@clone: (url, path, options, callback) ->
options = Util.setOptions options, callback
command = new CliCommand(['git', 'clone'], [url, path], options.cli)
if options.callback
callback = options.callback
options.callback = (err, stdout, stderr) ->
[options, callback] = Util.setOptions options, callback
command = new CliCommand(['git', 'clone'], [url, path], options)
if callback
done = callback
callback = (err, stdout, stderr) ->
repository = new Repository("#{path}/.git")
callback err, repository
Runner.execute command, options
done err, repository
Runner.execute command, {}, callback

add: (files, options, callback) ->
if _.isArray files
options = Util.setOptions options, callback
[options, callback] = Util.setOptions options, callback
else
options = Util.setOptions files, options
[options, callback] = Util.setOptions files, options
files = ['.']
args = []
Array.prototype.push.apply(args, files)
command = new CliCommand(['git', 'add'], args, options.cli)
Runner.execute command, @_createOptions(options)

status: (options={}) ->
options = Util.setOptions options, callback
command = new CliCommand(['git', 'status'], _.extend({ s: '' }, options.cli))
if options.callback
callback = options.callback
options.callback = (err, stdout, stderr) =>
command = new CliCommand(['git', 'add'], args, options)
Runner.execute command, @_getOptions(), callback

status: (options, callback) ->
[options, callback] = Util.setOptions options, callback
command = new CliCommand(['git', 'status'], _.extend({ s: '' }, options))
if callback?
done = callback
callback = (err, stdout, stderr) =>
statusInfo = GitUtil.parseStatus(stdout)
_.each(statusInfo, (f) => f.fullPath = "#{@workingDir()}/#{f.path}")
callback err, statusInfo
Runner.execute command, @_createOptions(options)
done err, statusInfo
Runner.execute command, @_getOptions(), callback

diff: (options, callback) ->
options = Util.setOptions options, callback
[options, callback] = Util.setOptions options, callback
args = @_getDiffArgs(options)
command = new CliCommand(['git', 'diff'], args, options.cli)
Runner.execute command, @_createOptions(options)
command = new CliCommand(['git', 'diff'], args, options)
Runner.execute command, @_getOptions(), callback

diffStats: (options={}) ->
options = Util.setOptions options, callback
diffStats: (options, callback) ->
[options, callback] = Util.setOptions options, callback
args = @_getDiffArgs(options)
cliOpts = _.extend({ shortstat: '' }, options.cli)
cliOpts = _.extend({ shortstat: '' }, options)
command = new CliCommand(['git', 'diff'], args, cliOpts)
if options.callback
callback = options.callback
options.callback = (err, stdout, stderr) ->
if callback
done = callback
callback = (err, stdout, stderr) ->
stats = GitUtil.parseShortDiff(stdout)
callback err, stats
Runner.execute command, @_createOptions(options)
done err, stats
Runner.execute command, @_getOptions(), callback

log: (options={}) ->
options = Util.setOptions 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.cli)
cliOpts = _.extend({ pretty: "format:#{format}" }, options)
command = new CliCommand(['git', 'log'], cliOpts)
if options.callback
callback = options.callback
options.callback = (err, stdout, stderr) ->
if callback
done = callback
callback = (err, stdout, stderr) ->
logs = GitUtil.parseLog stdout
callback err, logs
Runner.execute command, @_createOptions(options)
done err, logs
Runner.execute command, @_getOptions(), callback

commit: (message, options, callback) ->
options = Util.setOptions options, callback
cliOpts = _.extend({m: message}, options.cli)
[options, callback] = Util.setOptions options, callback
cliOpts = _.extend({m: message}, options)
if options.autoAdd
cliOpts.a = ''
command = new CliCommand(['git', 'commit'], cliOpts)
Runner.execute command, @_createOptions(options)
Runner.execute command, @_getOptions(), callback

_getDiffArgs: (options) ->
args = []
Expand All @@ -108,71 +108,69 @@ class Repository
args

listRemotes: (options, callback) ->
options = Util.setOptions options, callback
if options.callback
callback = options.callback
options.callback = (err, stdout, stderr) ->
[options, callback] = Util.setOptions options, callback
if callback
done = callback
callback = (err, stdout, stderr) ->
remotes = stdout.trim().split "\n"
callback err, remotes
command = new CliCommand(['git', 'remote', 'show'], options.cli)
Runner.execute command, @_createOptions(options)
done err, remotes
command = new CliCommand(['git', 'remote', 'show'], options)
Runner.execute command, @_getOptions(), callback

showRemote: (name, options, callback) ->
options = Util.setOptions options, callback
if options.callback
callback = options.callback
options.callback = (err, stdout, stderr) ->
[options, callback] = Util.setOptions options, callback
if callback?
done = callback
callback = (err, stdout, stderr) ->
remoteInfo = GitUtil.parseRemote stdout
callback err, remoteInfo
command = new CliCommand(['git', 'remote', 'show'], name, options.cli)
Runner.execute command, @_createOptions(options)
done err, remoteInfo
command = new CliCommand(['git', 'remote', 'show'], name, options)
Runner.execute command, @_getOptions(), callback

currentBranch: (options, callback) ->
options = Util.setOptions options, callback
if options.callback
callback = options.callback
options.callback = (err, stdout, stderr) ->
[options, callback] = Util.setOptions options, callback
if callback
done = callback
callback = (err, stdout, stderr) ->
branch = GitUtil.parseCurrentBranch stdout
callback err, branch
command = new CliCommand(['git', 'branch'], options.cli)
Runner.execute command, @_createOptions(options)
done err, branch
command = new CliCommand(['git', 'branch'], options)
Runner.execute command, @_getOptions(), callback

branch: (branch, options, callback) ->
branch = [branch] if _.isString(branch)
if _.isArray(branch)
[options, hasName] = [Util.setOptions(options, callback), true]
[[options, callback], hasName] = [Util.setOptions(options, callback), true]
else
[options, hasName] = [Util.setOptions(branch, options), false]
[[options, callback], hasName] = [Util.setOptions(branch, options), false]
branch = [] unless hasName
if options.callback && !hasName
callback = options.callback
options.callback = (err, stdout, stderr) ->
if callback? && !hasName
done = callback
callback = (err, stdout, stderr) ->
branches = GitUtil.parseBranches stdout
callback err, branches
command = new CliCommand(['git', 'branch'], branch, options.cli)
Runner.execute command, @_createOptions(options)
done err, branches
command = new CliCommand(['git', 'branch'], branch, options)
Runner.execute command, @_getOptions(), callback

checkout: (branch, options, callback) ->
options = Util.setOptions options, callback
command = new CliCommand(['git', 'checkout'], branch, options.cli)
Runner.execute command, @_createOptions(options)
[options, callback] = Util.setOptions options, callback
command = new CliCommand(['git', 'checkout'], branch, options)
Runner.execute command, @_getOptions(), callback

push: (args, options, callback) ->
args = [args] if _.isString(args)
if _.isArray(args)
options = Util.setOptions(options, callback)
[options, callback] = Util.setOptions(options, callback)
else
[options, args] = [Util.setOptions(args, options), []]
command = new CliCommand(['git', 'push'], args, options.cli)
Runner.execute command, @_createOptions(options)
[[options, callback], args] = [Util.setOptions(args, options), []]
command = new CliCommand(['git', 'push'], args, options)
Runner.execute command, @_getOptions(), callback


workingDir: -> path.dirname @path

_createOptions: (base={}) ->
_.extend
cwd: @workingDir()
, base
_getOptions: ->
cwd: @workingDir()


module.exports = Repository
6 changes: 3 additions & 3 deletions src/runner.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
exec = require('child_process').exec

Runner =
execute: (command, options={}) ->
execute: (command, options, callback) ->
exec command.toString(), options, (err, stdout, stderr) ->
if options.callback?
options.callback err, stdout, stderr
if callback?
callback err, stdout, stderr

module.exports = Runner
6 changes: 2 additions & 4 deletions src/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ Util =

setOptions: (options, callback) ->
if _.isFunction(options) && !callback?
options = { callback: options }
[{}, options]
else
options ?= {}
options.callback = callback ? null
options
[options, callback ? null]

module.exports = Util
15 changes: 7 additions & 8 deletions test/repository-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ before (done) ->
fs.removeSync(process.env['TMPDIR'])
fs.mkdirSync(process.env['TMPDIR'])
tmp.dir (err, path) ->
Repository.clone BASE_REPO_PATH, "#{path}/node-git-cli", { cli: { bare: true } }, (err, repo) ->
Repository.clone BASE_REPO_PATH, "#{path}/node-git-cli", { bare: true }, (err, repo) ->
baseRepository = repo
done()

Expand Down Expand Up @@ -51,11 +51,10 @@ describe 'Repository', ->
repository = new Repository('/path/to/.git')
expect(repository.workingDir()).to.be '/path/to'

describe '#createOptions', ->
it 'should extend options with "cwd"', ->
describe '#_getOptions', ->
it 'should return "cwd"', ->
repository = new Repository('/path/to/.git')
options = { foo: 'bar' }
expect(repository._createOptions(options)).to.eql { cwd: '/path/to', foo: 'bar' }
expect(repository._getOptions()).to.eql { cwd: '/path/to' }

describe 'clone', ->
it 'should clone repository to given directory', (done) ->
Expand Down Expand Up @@ -206,7 +205,7 @@ describe 'Repository', ->
testRepository.branch 'foo', (err, branches) ->
testRepository.branch (err, branches) ->
expect(branches).to.eql ['foo', 'master']
testRepository.branch 'foo', { cli: { D: true } }, (err) ->
testRepository.branch 'foo', { D: true }, (err) ->
expect(err).to.be null
testRepository.branch (err, branches) ->
expect(branches).to.eql ['master']
Expand All @@ -224,7 +223,7 @@ describe 'Repository', ->
done()

it 'should work with -b flag', (done) ->
testRepository.checkout 'foo', { cli: { b: true } }, (err) ->
testRepository.checkout 'foo', { b: true }, (err) ->
expect(err).to.be null
testRepository.currentBranch (err, branch) ->
expect(branch).to.be 'foo'
Expand All @@ -235,7 +234,7 @@ describe 'Repository', ->
baseRepository.log (err, logs) ->
logsCount = logs.length
fs.appendFileSync("#{testRepository.workingDir()}/README.md", 'foobar')
testRepository.commit "foo'bar", { cli: {a: true } }, (err) ->
testRepository.commit "foo'bar", { a: true }, (err) ->
testRepository.push (err) ->
expect(err).to.be null
baseRepository.log (err, logs) ->
Expand Down
12 changes: 6 additions & 6 deletions test/util-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ describe 'Util', ->

describe 'setOptions', ->
it 'should work without options', ->
options = Util.setOptions (-> 1)
[options, callback] = Util.setOptions (-> 1)
expect(options).to.be.a('object')
expect(options.callback).to.be.a('function')
expect(callback).to.be.a('function')

it 'should work with options and callback', ->
options = Util.setOptions { force: true }, (-> 1)
[options, callback] = Util.setOptions { force: true }, (-> 1)
expect(options).to.be.a('object')
expect(options.callback).to.be.a('function')
expect(callback).to.be.a('function')
expect(options.force).to.be true

it 'should work with options and no callback', ->
options = Util.setOptions { force: true }
[options, callback] = Util.setOptions { force: true }
expect(options).to.be.a('object')
expect(options.callback).to.be null
expect(callback).to.be null
expect(options.force).to.be true

0 comments on commit 39acfc6

Please sign in to comment.