Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove async library #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
451 changes: 204 additions & 247 deletions lib/git/git.js

Large diffs are not rendered by default.

56 changes: 24 additions & 32 deletions lib/git/js-git-service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var path = require('path');
var whilst = require('async/whilst');
var helper = require('../helper.js');

var jsGitService = {};
Expand Down Expand Up @@ -102,7 +101,7 @@ jsGitService.getCommitHistory = function (folder, n, branch, remote, cb) {
ref = remote ? 'refs/remotes/origin/' + branch : 'refs/heads/' + branch;
}

jsGitService.getLastCommitByRef(repo, ref, function (err, commit) {
jsGitService.getLastCommitByRef(repo, ref, async function (err, commit) {
if (err) {
return cb(err);
}
Expand All @@ -114,38 +113,31 @@ jsGitService.getCommitHistory = function (folder, n, branch, remote, cb) {

var count = 1;
var parentCommitHash = helper.last(commit.parents); // last parent is the parent in the 'git log' meaning

whilst(
function () {
return count < n && parentCommitHash;
},
function (callback) {

jsGitService.getCommitByHash(repo, parentCommitHash, function (err, commit) {
if (err) {
return callback(err);
}
if (!commit) {
parentCommitHash = null;
return callback(null);
}

commit.hash = parentCommitHash; // add hash back to commit as not a property when loaded by hash

count++;
commitHistory.push(commit);
parentCommitHash = helper.last(commit.parents);
callback(null);
try {
while (count < n && parentCommitHash) {
await new Promise(async (resolve, reject) => {
jsGitService.getCommitByHash(repo, parentCommitHash, function (err, commit) {
if (err) {
return reject(err);
}
if (!commit) {
parentCommitHash = null;
return resolve();
}

commit.hash = parentCommitHash; // add hash back to commit as not a property when loaded by hash

count++;
commitHistory.push(commit);
parentCommitHash = helper.last(commit.parents);
resolve();
});
});
},
function (err) {
if (err) {
return cb(err);
}

return cb(null, commitHistory);
}
);
return cb(null, commitHistory);
} catch (err) {
return cb(err);
}
});
};

Expand Down
31 changes: 28 additions & 3 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,33 @@ function last(array) {
return length ? array[length - 1] : undefined;
}

function fromCallback(func) {
return new Promise(
(resolve, reject) => func((err, result) => (err ? reject(err) : resolve(result))),
);
}

async function findAsync(promises) {
var foundResult = undefined;
await promises.reduce(
(resultPromise, promise) => resultPromise.then((result) => {
if (!foundResult) {
if (result) {
foundResult = result;
} else {
return promise.then();
}
}
}),
Promise.resolve(),
);
return foundResult;
}

module.exports = {
get: get,
last: last,
trimNewLine: trimNewLine,
findAsync,
fromCallback,
get,
last,
trimNewLine,
};
99 changes: 49 additions & 50 deletions lib/hg/hg.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
var exec = require("child_process").exec;

var fs = require("fs");

var cliCommand = require('../cliCommand.js');

var halt = false;
Expand All @@ -24,56 +22,57 @@ function checkReturn(dataArray, cb) {
dataArray[key] = dataArray[key].replace(/\n/g, '');
}
});
cb(null, dataArray);
cb(dataArray);
}
};

}

module.exports.parse = function parseHg(folder, cb) {
var data = {};
module.exports.parse = async function parseHg(folder) {
return new Promise((resolve, reject) => {
var data = {};

data.type = 'mercurial';
data.commit_history = []; // temporary
data.type = 'mercurial';
data.commit_history = []; // temporary

exec(cliCommand(folder, "hg paths default"), function(err, stdout, stderr) {
if(err !== null) {
error("mercurial", "fetching path", stderr, cb);
}
else {
data.url = stdout;
checkReturn(data, cb);
}
exec(cliCommand(folder, "hg paths default"), function(err, stdout, stderr) {
if(err !== null) {
error("mercurial", "fetching path", stderr, reject);
}
else {
data.url = stdout;
checkReturn(data, resolve);
}
});
exec(cliCommand(folder, "hg log --limit 1 --template 'changeset: {rev}:{node|short}\nsummary: {desc}'"), function(err, stdout, stderr) {
if(err !== null) {
error("mercurial", "fetching log", stderr, reject);
}
else {
var changeset = stdout.match(/^changeset:\s+([^\n]+)$/m);
//date = stdout.match(/^date:\s+:([^\n]+)$/m);
var summary = stdout.match(/^summary:\s+([^\n]+)$/m);
data.revision = changeset[1];
data.comment = summary[1];
//data.update_time = date;
checkReturn(data, resolve);
}
});
exec(cliCommand(folder, "hg branch"), function(err, stdout, stderr) {
if(err !== null) {
error("mercurial", "fetching branch", stderr, reject);
}
else {
data.branch = stdout;
checkReturn(data, resolve);
}
});
fs.stat(folder+".hg", function(err, stats) {
if(err !== null) {
error("mercurial", "fetching stats", "no error available", reject);
}
else {
data.update_time = stats.mtime;
checkReturn(data, resolve);
}
});
});
exec(cliCommand(folder, "hg log --limit 1 --template 'changeset: {rev}:{node|short}\nsummary: {desc}'"), function(err, stdout, stderr) {
if(err !== null) {
error("mercurial", "fetching log", stderr, cb);
}
else {
var changeset = stdout.match(/^changeset:\s+([^\n]+)$/m);
//date = stdout.match(/^date:\s+:([^\n]+)$/m);
var summary = stdout.match(/^summary:\s+([^\n]+)$/m);
data.revision = changeset[1];
data.comment = summary[1];
//data.update_time = date;
checkReturn(data, cb);
}
});
exec(cliCommand(folder, "hg branch"), function(err, stdout, stderr) {
if(err !== null) {
error("mercurial", "fetching branch", stderr, cb);
}
else {
data.branch = stdout;
checkReturn(data, cb);
}
});
fs.stat(folder+".hg", function(err, stats) {
if(err !== null) {
error("mercurial", "fetching stats", "no error available", cb);
}
else {
data.update_time = stats.mtime;
checkReturn(data, cb);
}
});
}
};
28 changes: 14 additions & 14 deletions lib/identify.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
var fs = require('fs');
var eachSeries = require('async/eachSeries');
var helper = require('./helper');

module.exports = function(folder, cb) {
var allTypes = ['git', 'hg', 'svn'];

module.exports = async function(folder) {
if (folder[folder.length - 1] !== '/')
folder += '/';

eachSeries(['git', 'hg', 'svn'],
function(type, callback) {
fs.exists(folder+'.'+type, function(exists) {
if (exists)
return callback(type);
else
return callback();
});
},
function(final) {
return cb(final ? final : 'No versioning system found', folder);
});
const type = await helper.findAsync(allTypes.map(
async function (type) {
const exists = await new Promise((resolve) => {
fs.exists(folder + '.' + type, resolve);
});
return exists ? type : undefined;
}
));

return { type, folder };
};
Loading