Skip to content

Commit

Permalink
refactor binary: make use of lib to remove redundancies
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlinse authored and David Linse committed Apr 23, 2022
1 parent 4f85dcd commit c7f530b
Showing 1 changed file with 14 additions and 151 deletions.
165 changes: 14 additions & 151 deletions bin/git-contributors
Original file line number Diff line number Diff line change
Expand Up @@ -12,166 +12,29 @@

var fs = require('fs'),
path = require('path'),
git = require('../lib/gitlog'),
_ = require('lodash');
var Q = require('q');
_ = require('lodash'),
cli = require('../lib/_cli');

var verifyRepositoryExists = function() {
'use strict';

var deferred = Q.defer();
var repo = path.join(program.cwd, '.git');
var ok = fs.existsSync(program.cwd) && fs.existsSync(repo);

if (ok) { deferred.resolve(program); }
else {
deferred.reject(
new Error('Could not find .git repository at "'+ program.cwd +'"')
);
}

return deferred.promise;
};

var processLog = function(stdout) {
'use strict';

var deferred = Q.defer();

var list,
entries = _.compact(stdout.split('\n')),
total = _.size(entries);

list = _.map(_.uniq(entries), function(committer) {

var parts = committer.split(' '),
email = _.last(parts),
author = _.without(parts, email).join(' '),
commits = _.size(_.filter(entries, function(e) {
return e.indexOf(author) > -1 && e.indexOf(email) > -1;
})),
percentage = (commits / total * 100);

return {
commits: commits,
name: author,
email: email,
percent: parseFloat(Math.max(0.1, percentage).toFixed(1), 10)
};
});

list = _.sortBy(list, function(committer) {
return -committer.commits;
});

deferred.resolve(list);

return deferred.promise;
};

var format = function(data) {
'use strict';

var deferred = Q.defer();

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

if (program.json === true) {
data = JSON.stringify(data);
}
deferred.resolve(data);
var pkg = path.resolve(__dirname + '/../package.json');

return deferred.promise;
};
var version = JSON.parse(fs.readFileSync(pkg, 'utf-8')).version;

var filter = function(data) {
'use strict';

var deferred = Q.defer();

var stripEmail = function(el) {
return _.omit(el, 'email');
};

if (program.email === false) {
data = _.map(data, stripEmail);
}

deferred.resolve(data);
var program = {};

return deferred.promise;
};
program = _.merge(cli.parse(process.argv, version), program);
program.cwd = _.first(program.args);

//--

var program = {
timeout: 5000,
cwd: '.',
maxBuffer: 25000 * 1024
};
var GitContributors = require('../lib/git-contributors').GitContributors;


var GitContributors = function GitContributors () {};

GitContributors.prototype.list = function(opts, cb) {
GitContributors.list(program, function(err, result) {
'use strict';

program.markdown = null;
program.json = null;

if (_.isString(opts)) {
program.cwd = opts;
}
else {
program = _.merge(program, opts);
}

Q()
.then(verifyRepositoryExists)
.then(git.log)
.then(processLog)
.then(filter)
.then(format)
.done(function(result) {
cb(null, result);
}, function(err) {
cb(err, null);
});
};

module.exports.GitContributors = new GitContributors();


// binary

if (!module.parent) {

var fs = require('fs');
var pkg = path.resolve(__dirname + '/../package.json');

var version = JSON.parse(fs.readFileSync(pkg, 'utf-8')).version;

program = _.merge(require('../lib/_cli').parse(process.argv, version), program);
program.cwd = _.first(program.args);

var success = function(result) {
'use strict';
console.log(result);
};

Q()
.then(verifyRepositoryExists)
.then(git.log)
.then(processLog)
.then(filter)
.then(format)
.then(success)
.catch(function(err){
'use strict';
if (err) {
console.log(err.message);
process.exit(1);
});
}
}

console.log(result);
process.exit(0);
});

0 comments on commit c7f530b

Please sign in to comment.