Skip to content

Commit

Permalink
feat(cli): add support for --json option
Browse files Browse the repository at this point in the history
The ```--json``` option returns the result as JSON.stringified array
which is pretty useful in combination with the ```--markdown``` option.

```
  '[
     "+ John Doe <[email protected]>  (3 - 50%)",
     "+ Jane Doe <[email protected]>  (3 - 50%)"
   ]'
```
  • Loading branch information
davidlinse authored and David Linse committed Apr 23, 2022
1 parent 0d27512 commit 478f3d7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/git-contributors.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,20 @@ var format = function(data) {
'use strict';

var deferred = Q.defer();
var stringify = program.markdown === true && program.json === true;

if (program.markdown === true) {
if (program.markdown) {
data = require('../lib/markdown-reporter').format(data, program);
}

if (stringify) {
data = _.trim(data, '\n').split('\n');
}

if (program.json === true) {
data = JSON.stringify(data);
}

deferred.resolve(data);

return deferred.promise;
Expand Down
6 changes: 1 addition & 5 deletions lib/markdown-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
/* jshint indent:2 */

var _ = require('lodash');
var Q = require('q');

module.exports.format = function(data, opts) {

Expand All @@ -20,7 +19,6 @@ module.exports.format = function(data, opts) {
opts = opts || {};

var report = '';
var deferred = Q.defer();

report += (opts.header || '');

Expand All @@ -34,7 +32,5 @@ module.exports.format = function(data, opts) {
'\n';
});

deferred.resolve(report);

return deferred.promise;
return report;
};
76 changes: 76 additions & 0 deletions test/test.json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* global describe, beforeEach, afterEach */
/* jshint expr:true, indent:2, maxlen:92 */

'use strict';

var fs = require('fs'),
sinon = require('sinon'),
chai = require('chai'),
git = require('../lib/gitlog'),
expect = chai.expect,

GitContributors = require('../').GitContributors;


var readIn = function readIn (file) {
return fs.readFileSync(file, 'utf-8');
};

var stubFixture = function stubFixture (file) {
sinon.stub(git, 'log').returns(readIn(file));
};


describe('git-contributors', function () {

describe('json support', function () {

beforeEach(function () {
});

afterEach(function () {
git.log.restore();
});

it('--markdown=false --json=true returns a stringified json object', function(done) {

var inFixture, outFixture;

inFixture = 'test/fixtures/actual/single-user-multiple-commit.log';
stubFixture(inFixture);

outFixture = readIn('test/fixtures/expected/single-user-multiple-commit.json');

// beautify string
outFixture = JSON.stringify(JSON.parse(outFixture));

GitContributors.list({cwd: '.', markdown: false, json: true}, function (err, result) {
expect(err).to.not.exist;
expect(result).to.deep.eql(outFixture);
done();
});
});

it('--markdown=true --json=true returns stringified array', function(done) {

var inFixture, outFixture;

inFixture = 'test/fixtures/actual/single-user-multiple-commit.log';

// outFixture = readIn('test/fixtures/expected/single-user-multiple-commit.md');

stubFixture(inFixture);

outFixture = '["+ John Doe <[email protected]> (3 - 100%)"]';

GitContributors.list({cwd: '.', markdown: true, json: true}, function (err, result) {

expect(err).to.not.exist;
expect(result).to.exist;
expect(result).to.eql(outFixture);

done();
});
});
});
});

0 comments on commit 478f3d7

Please sign in to comment.