From f3f34c6806f472c6f8d6ac573593f35e425210f6 Mon Sep 17 00:00:00 2001 From: vedi Date: Tue, 14 Apr 2020 11:05:34 +0300 Subject: [PATCH 1/2] Remove async library --- lib/git/git.js | 451 +++++++++++++++++--------------------- lib/git/js-git-service.js | 56 ++--- lib/helper.js | 31 ++- lib/hg/hg.js | 99 +++++---- lib/identify.js | 28 +-- lib/svn/svn.js | 110 +++++----- lib/vizion.js | 136 +++++++----- package.json | 3 +- test/unit/git.test.js | 160 +++++--------- 9 files changed, 511 insertions(+), 563 deletions(-) diff --git a/lib/git/git.js b/lib/git/git.js index 0d5f8fa..0899460 100644 --- a/lib/git/git.js +++ b/lib/git/git.js @@ -1,5 +1,4 @@ var fs = require('fs'); -var waterfall = require('async/waterfall'); var exec = require('child_process').exec; var ini = require('ini'); var path = require('path'); @@ -12,319 +11,277 @@ var git = {}; var TIMEOUT = 5000; var MAXBUFFER = 1024 * 64; // 16KB -git.parseGitConfig = function (folder, cb) { - fs.readFile(path.join(folder, '.git/config'), 'utf-8', function (err, data) { - if (err) { - return cb(err); - } +git.parseGitConfig = async function (folder) { + return new Promise((resolve, reject) => { + fs.readFile(path.join(folder, '.git/config'), 'utf-8', function (err, data) { + if (err) { + return reject(err); + } - var config = ini.parse(data); - cb(null, config); + var config = ini.parse(data); + resolve(config); + }); }); }; -git.getUrl = function (folder, cb) { - git.parseGitConfig(folder, function (err, config) { - if (err) { - return cb(err); - } - - var data = {}; +git.getUrl = async function (folder) { + const config = await git.parseGitConfig(folder); + var data = {}; + data.type = 'git'; + data.url = helper.get(config, 'remote "origin".url'); + return data; +}; - data.type = 'git'; - data.url = helper.get(config, 'remote "origin".url'); +git.getCommitInfo = function (folder, data) { + return new Promise((resolve, reject) => { + jsGitService.getHeadCommit(folder, function (err, commit) { + if (err) { + return reject(err); + } - cb(null, data); + data.revision = helper.get(commit, 'hash'); + data.comment = helper.get(commit, 'message'); + resolve(data); + }); }); }; -git.getCommitInfo = function (folder, data, cb) { - jsGitService.getHeadCommit(folder, function (err, commit) { - if (err) { - return cb(err); - } +git.getStaged = function (folder, data) { + return new Promise((resolve, reject) => { + exec(cliCommand(folder, 'git status -s'), {timeout: TIMEOUT, maxBuffer: MAXBUFFER}, + function (err, stdout, stderr) { + if (err) { + return reject(err); + } - data.revision = helper.get(commit, 'hash'); - data.comment = helper.get(commit, 'message'); - cb(null, data); + data.unstaged = (stdout === '') ? false : true; + return resolve(data); + }); }); }; -git.getStaged = function (folder, data, cb) { - exec(cliCommand(folder, 'git status -s'), {timeout: TIMEOUT, maxBuffer: MAXBUFFER}, - function (err, stdout, stderr) { +git.getBranch = function (folder, data) { + return new Promise((resolve, reject) => { + fs.readFile(path.join(folder, '.git/HEAD'), 'utf-8', function (err, content) { if (err) { - return cb(err); + return reject(err); } - data.unstaged = (stdout === '') ? false : true; - return cb(null, data); - }); -}; - -git.getBranch = function (folder, data, cb) { - fs.readFile(path.join(folder, '.git/HEAD'), 'utf-8', function (err, content) { - if (err) { - return cb(err); - } + var regex = /ref: refs\/heads\/(.*)/; + var match = regex.exec(content); + data.branch = match ? match[1] : 'HEAD'; - var regex = /ref: refs\/heads\/(.*)/; - var match = regex.exec(content); - data.branch = match ? match[1] : 'HEAD'; - - return cb(null, data); + return resolve(data); + }); }); }; -git.getRemote = function (folder, data, cb) { - git.parseGitConfig(folder, function (err, config) { - if (err) { - return cb(err); - } +git.getRemote = async function (folder, data) { + const config = await git.parseGitConfig(folder); - data.remotes = []; + data.remotes = []; - Object.keys(config).map(function (key) { - var regex = /remote "(.*)"/; - var match = regex.exec(key); - if (match) { - data.remotes.push(match[1]); - } - }); + Object.keys(config).map(function (key) { + var regex = /remote "(.*)"/; + var match = regex.exec(key); + if (match) { + data.remotes.push(match[1]); + } + }); - data.remote = (data.remotes.indexOf('origin') === -1) ? data.remotes[0] : 'origin'; + data.remote = (data.remotes.indexOf('origin') === -1) ? data.remotes[0] : 'origin'; - cb(null, data); - }); + return data; }; -git.isCurrentBranchOnRemote = function (folder, data, cb) { - jsGitService.getRefHash(folder, data.branch, data.remote, function (err, hash) { - if (err) { - return cb(err); - } +git.isCurrentBranchOnRemote = function (folder, data) { + return new Promise((resolve, reject) => { + jsGitService.getRefHash(folder, data.branch, data.remote, function (err, hash) { + if (err) { + return reject(err); + } - data.branch_exists_on_remote = !!hash; + data.branch_exists_on_remote = !!hash; - return cb(null, data); + return resolve(data); + }); }); }; -git.getPrevNext = function (folder, data, cb) { - var remote = data.branch_exists_on_remote ? data.remote : null; +git.getPrevNext = function (folder, data) { + return new Promise((resolve, reject) => { + var remote = data.branch_exists_on_remote ? data.remote : null; - jsGitService.getCommitHistory(folder, 100, data.branch, remote, function (err, commitHistory) { - if (err) { - return cb(err); - } + jsGitService.getCommitHistory(folder, 100, data.branch, remote, function (err, commitHistory) { + if (err) { + return reject(err); + } - var currentCommitIndex = commitHistory.findIndex(({ hash }) => hash === data.revision); + var currentCommitIndex = commitHistory.findIndex(({ hash }) => hash === data.revision); - if (currentCommitIndex === -1) { - data.ahead = true; - data.next_rev = null; - data.prev_rev = null; - } - else { - data.ahead = false; - data.next_rev = (currentCommitIndex === 0) ? null : commitHistory[currentCommitIndex - 1].hash; - data.prev_rev = (currentCommitIndex === (commitHistory.length - 1)) ? null : commitHistory[currentCommitIndex + 1].hash; - } + if (currentCommitIndex === -1) { + data.ahead = true; + data.next_rev = null; + data.prev_rev = null; + } + else { + data.ahead = false; + data.next_rev = (currentCommitIndex === 0) ? null : commitHistory[currentCommitIndex - 1].hash; + data.prev_rev = (currentCommitIndex === (commitHistory.length - 1)) ? null : commitHistory[currentCommitIndex + 1].hash; + } - cb(null, data); + resolve(data); + }); }); }; -git.getUpdateTime = function (folder, data, cb) { - fs.stat(folder + ".git", function (err, stats) { - if (err) { - return cb(err); - } +git.getUpdateTime = function (folder, data) { + return new Promise((resolve, reject) => { + fs.stat(folder + ".git", function (err, stats) { + if (err) { + return reject(err); + } - data.update_time = helper.trimNewLine(stats.mtime); - return cb(null, data); + data.update_time = helper.trimNewLine(stats.mtime); + return resolve(data); + }); }); }; -git.getTags = function (folder, data, cb) { +git.getTags = function (folder, data) { + return new Promise((resolve, reject) => { exec(cliCommand(folder, 'git tag'), {timeout: TIMEOUT, maxBuffer: MAXBUFFER}, - function (err, stdout, stderr) { - if (err) { - return cb(err); - } + function (err, stdout, stderr) { + if (err) { + return reject(err); + } - if (stdout.length) { - data.tags = stdout.split('\n'); - data.tags.pop(); - data.tags = data.tags.slice(0, 10); - } - return cb(null, data); - }); + if (stdout.length) { + data.tags = stdout.split('\n'); + data.tags.pop(); + data.tags = data.tags.slice(0, 10); + } + return resolve(data); + }); + }); }; -git.parse = function (folder, cb) { - waterfall([ - git.getUrl.bind(null, folder), - git.getCommitInfo.bind(null, folder), - git.getStaged.bind(null, folder), - git.getBranch.bind(null, folder), - git.getRemote.bind(null, folder), - git.isCurrentBranchOnRemote.bind(null, folder), - git.getPrevNext.bind(null, folder), - git.getUpdateTime.bind(null, folder), - git.getTags.bind(null, folder)], - function (err, data) { - if (err) { - return cb(err); - } - - return cb(null, data); - }); +git.parse = async function (folder) { + let data = await git.getUrl(folder); + data = await git.getCommitInfo(folder, data); + data = await git.getStaged(folder, data); + data = await git.getBranch(folder, data); + data = await git.getRemote(folder, data); + data = await git.isCurrentBranchOnRemote(folder, data); + data = await git.getPrevNext(folder, data); + data = await git.getUpdateTime(folder, data); + data = await git.getTags(folder, data); + return data; }; -git.isUpdated = function (folder, cb) { - waterfall([ - git.getCommitInfo.bind(null, folder, {}), - git.getBranch.bind(null, folder), - git.getRemote.bind(null, folder), - git.isCurrentBranchOnRemote.bind(null, folder), - ], - function (err, data) { - if (err) { - return cb(err); - } +git.isUpdated = async function (folder) { + let data = await git.getCommitInfo(folder, {}); + data = await git.getBranch(folder, data); + data = await git.getRemote(folder, data); + data = await git.isCurrentBranchOnRemote(folder, data); + return new Promise((resolve, reject) => { + exec(cliCommand(folder, 'git remote update'), {timeout: 60000, maxBuffer: MAXBUFFER}, + function (err, stdout, stderr) { + if (err) { + return reject(err); + } - exec(cliCommand(folder, 'git remote update'), {timeout: 60000, maxBuffer: MAXBUFFER}, - function (err, stdout, stderr) { + var remote = data.branch_exists_on_remote ? data.remote : null; + jsGitService.getLastCommit(folder, data.branch, remote, function (err, commit) { if (err) { - return cb(err); + return reject(err); } - var remote = data.branch_exists_on_remote ? data.remote : null; - jsGitService.getLastCommit(folder, data.branch, remote, function (err, commit) { - if (err) { - return cb(err); - } - - var res = { - new_revision: commit.hash, - current_revision: data.revision, - is_up_to_date: (commit.hash === data.revision) - }; - return cb(null, res); - }); + var res = { + new_revision: commit.hash, + current_revision: data.revision, + is_up_to_date: (commit.hash === data.revision) + }; + return resolve(res); }); - }); + }); + }); }; -git.revert = function (args, cb) { +git.revert = async function (args) { var ret = {}; var command = cliCommand(args.folder, "git reset --hard " + args.revision); ret.output = ''; ret.output += command + '\n'; ret.success = true; - exec(command, {timeout: TIMEOUT, maxBuffer: MAXBUFFER}, - function (err, stdout, stderr) { - ret.output += stdout; - if (err !== null || stderr.substring(0, 6) === 'fatal:') - ret.success = false; - return cb(null, ret); - }); + return new Promise(((resolve, reject) => { + exec(command, {timeout: TIMEOUT, maxBuffer: MAXBUFFER}, + function (err, stdout, stderr) { + ret.output += stdout; + if (err !== null || stderr.substring(0, 6) === 'fatal:') + ret.success = false; + return resolve(ret); + }); + })); }; -git.update = function (folder, cb) { - git.isUpdated(folder, function (err, data) { - if (err) { - return cb(err); - } - - var res = {}; - if (data.is_up_to_date === true) { - res.success = false; - res.current_revision = data.new_revision; - return cb(null, res); - } - else { - git.revert({folder: folder, revision: data.new_revision}, - function (err, dt) { - if (err) { - return cb(err); - } - - res.output = dt.output; - res.success = dt.success; - res.current_revision = (dt.success) ? data.new_revision : data.current_revision; - return cb(null, res); - }); - } - }); +git.update = async function (folder) { + const data = await git.isUpdated(folder); + var res = {}; + if (data.is_up_to_date === true) { + res.success = false; + res.current_revision = data.new_revision; + return res; + } else { + const meta = await git.revert({folder: folder, revision: data.new_revision}); + res.output = meta.output; + res.success = meta.success; + res.current_revision = (meta.success) ? data.new_revision : data.current_revision; + return res; + } }; -git.prev = function (folder, cb) { - waterfall([ - git.getCommitInfo.bind(null, folder, {}), - git.getBranch.bind(null, folder), - git.getRemote.bind(null, folder), - git.isCurrentBranchOnRemote.bind(null, folder), - git.getPrevNext.bind(null, folder), - ], function (err, data) { - if (err) { - return cb(err); - } - - var res = {}; - if (data.prev_rev !== null) { - git.revert({folder: folder, revision: data.prev_rev}, function (err, meta) { - if (err) { - return cb(err); - } - - res.output = meta.output; - res.success = meta.success; - res.current_revision = (res.success) ? data.prev_rev : data.revision; - return cb(null, res); - }); - } - else { - res.success = false; - res.current_revision = data.revision; - return cb(null, res); - } - }); +git.prev = async function (folder) { + let data = await git.getCommitInfo(folder, {}); + data = await git.getBranch(folder, data); + data = await git.getRemote(folder, data); + data = await git.isCurrentBranchOnRemote(folder, data); + data = await git.getPrevNext(folder, data); + + var res = {}; + if (data.prev_rev !== null) { + const meta = await git.revert({folder: folder, revision: data.prev_rev}); + res.output = meta.output; + res.success = meta.success; + res.current_revision = (res.success) ? data.prev_rev : data.revision; + return res; + } else { + res.success = false; + res.current_revision = data.revision; + return res; + } }; -git.next = function (folder, cb) { - waterfall([ - git.getCommitInfo.bind(null, folder, {}), - git.getBranch.bind(null, folder), - git.getRemote.bind(null, folder), - git.isCurrentBranchOnRemote.bind(null, folder), - git.getPrevNext.bind(null, folder), - ], function (err, data) { - if (err) { - return cb(err); - } - - var res = {}; - if (data.next_rev !== null) { - git.revert({folder: folder, revision: data.next_rev}, function (err, meta) { - if (err) { - return cb(err); - } - - res.output = meta.output; - res.success = meta.success; - res.current_revision = (res.success) ? data.next_rev : data.revision; - return cb(null, res); - }); - } - else { - res.success = false; - res.current_revision = data.revision; - return cb(null, res); - } - }); +git.next = async function (folder) { + let data = await git.getCommitInfo(folder, {}); + data = await git.getBranch(folder, data); + data = await git.getRemote(folder, data); + data = await git.isCurrentBranchOnRemote(folder, data); + data = await git.getPrevNext(folder, data); + var res = {}; + if (data.next_rev !== null) { + const meta = await git.revert({folder: folder, revision: data.next_rev}); + res.output = meta.output; + res.success = meta.success; + res.current_revision = (res.success) ? data.next_rev : data.revision; + return res; + } + else { + res.success = false; + res.current_revision = data.revision; + return res; + } }; module.exports = git; diff --git a/lib/git/js-git-service.js b/lib/git/js-git-service.js index 230b0e0..b986647 100644 --- a/lib/git/js-git-service.js +++ b/lib/git/js-git-service.js @@ -1,5 +1,4 @@ var path = require('path'); -var whilst = require('async/whilst'); var helper = require('../helper.js'); var jsGitService = {}; @@ -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); } @@ -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); + } }); }; diff --git a/lib/helper.js b/lib/helper.js index 1fa0239..db759cc 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -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, }; diff --git a/lib/hg/hg.js b/lib/hg/hg.js index 7b55f73..83033e0 100644 --- a/lib/hg/hg.js +++ b/lib/hg/hg.js @@ -1,7 +1,5 @@ var exec = require("child_process").exec; - var fs = require("fs"); - var cliCommand = require('../cliCommand.js'); var halt = false; @@ -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); - } - }); -} +}; diff --git a/lib/identify.js b/lib/identify.js index 44ce77b..b96806e 100644 --- a/lib/identify.js +++ b/lib/identify.js @@ -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 }; }; diff --git a/lib/svn/svn.js b/lib/svn/svn.js index e4e2de6..2e44377 100644 --- a/lib/svn/svn.js +++ b/lib/svn/svn.js @@ -1,16 +1,16 @@ var fs = require('fs'); -var waterfall = require('async/waterfall'); var exec = require('child_process').exec; -var cliCommand = require('../cliCommand.js'); +var cliCommand = require('../cliCommand'); +var helper = require('../helper'); var svn = {}; -svn.parse = function(folder, cb) { - var getMeta = function(cb) { +svn.parse = async function(folder) { + var getMeta = () => new Promise((resolve, reject) => { exec(cliCommand(folder, "svn info"), function(err, stdout, stderr) { if(err !== null) - return cb(err); + return reject(err); var data = {}; data.type = 'svn'; data.url = stdout.match(/Repository Root: ([^\n]+)/); @@ -38,15 +38,15 @@ svn.parse = function(folder, cb) { var date = new Date(match[1]); data.update_time = date; } - return cb(null, data); + return resolve(data); }); - } + }); - var getRevComment = function(data, cb) { + var getRevComment = (data) => new Promise((resolve, reject) => { var rev = data.revision || "BASE"; exec(cliCommand(folder, "svn log -r " + rev), function(err, stdout, stderr) { if(err !== null) - return cb(err); + return reject(err); if (rev === "BASE") { data.revision = stdout.match(/^(r[0-9]+)\s\|/m); if (data.revision) data.revision = data.revision[1]; @@ -59,73 +59,75 @@ svn.parse = function(folder, cb) { data.update_time[1].split(" | ")[2] ); } - cb(null, data); + resolve(data); }); - } + }); - var getDate = function(data, cb) { + var getDate = (data) => new Promise((resolve, reject) => { if (data.update_time) - return cb(null, data); + return resolve(data); fs.stat(folder+".svn", function(err, stats) { if(err !== null) - return cb(err); + return reject(err); data.update_time = stats.mtime; - return cb(null, data); + return resolve(data); }); - } - - waterfall([getMeta, getRevComment, getDate], - function(err, data) { - if (err !== null) - return cb(err); - return cb(null, data); }); -} -svn.isUpdated = function(folder, cb) { + const meta = await getMeta(); + const revComment = await getRevComment(meta); + + return getDate(revComment); +}; + +svn.isUpdated = async function(folder) { var res = {}; var getRev = function(str) { var matches = str.match(/Changed Rev: ([^\n]+)/); if (matches) matches = matches[1]; return matches; - } + }; - exec(cliCommand(folder, "svn info"), function(err, stdout, stderr) { - if(err !== null) - return cb(err); - var current_rev = getRev(stdout); - exec(cliCommand(folder, "svn info -r HEAD"), function(err, stdout, stderr) { + return new Promise((resolve, reject) => { + exec(cliCommand(folder, "svn info"), function(err, stdout, stderr) { if(err !== null) - return cb(err); - var recent_rev = getRev(stdout); - res.is_up_to_date = (recent_rev === current_rev); - res.new_revision = recent_rev; - res.current_revision = current_rev; - return cb(null, res); + return reject(err); + var current_rev = getRev(stdout); + exec(cliCommand(folder, "svn info -r HEAD"), function(err, stdout, stderr) { + if(err !== null) + return reject(err); + var recent_rev = getRev(stdout); + res.is_up_to_date = (recent_rev === current_rev); + res.new_revision = recent_rev; + res.current_revision = current_rev; + return resolve(res); + }); }); }); -} +}; -svn.update = function(folder, cb) { +svn.update = async function(folder) { var res = {}; - exec(cliCommand(folder, "svn update"), function(err, stdout, stderr) { - if(err !== null) - return cb(err); - var new_rev = stdout.match(/Updated to revision ([^\.]+)/); - if (new_rev === null) - { - res.success = false; - var old_rev = stdout.match(/At revision ([^\.]+)/); - res.current_revision = (old_rev) ? old_rev[1] : null; - } - else { - res.success = true; - res.current_revision = new_rev[1]; - } - return cb(null, res); + return new Promise((resolve, reject) => { + exec(cliCommand(folder, "svn update"), function(err, stdout, stderr) { + if(err !== null) + return reject(err); + var new_rev = stdout.match(/Updated to revision ([^\.]+)/); + if (new_rev === null) + { + res.success = false; + var old_rev = stdout.match(/At revision ([^\.]+)/); + res.current_revision = (old_rev) ? old_rev[1] : null; + } + else { + res.success = true; + res.current_revision = new_rev[1]; + } + return resovle(res); + }); }); -} +}; module.exports = svn; diff --git a/lib/vizion.js b/lib/vizion.js index 99a9df1..d37d0ac 100644 --- a/lib/vizion.js +++ b/lib/vizion.js @@ -1,80 +1,104 @@ var ALL = {}; var vizion = {}; -ALL.hg = require('./hg/hg.js'); -ALL.git = require('./git/git.js'); -ALL.svn = require('./svn/svn.js'); +ALL.hg = require('./hg/hg'); +ALL.git = require('./git/git'); +ALL.svn = require('./svn/svn'); // Add more revision control tools here -var identify = require('./identify.js'); - - -vizion.analyze = function(argv, cb) { - var _folder = (argv.folder != undefined) ? argv.folder : '.'; - - identify(_folder, function(type, folder) { - if (ALL[type]) - return ALL[type].parse(folder, cb); - else - return cb('Error vizion::analyze() for given folder: '+folder); - }); +var identify = require('./identify'); + +vizion.analyze = async function(argv, cb) { + try { + var _folder = (argv.folder !== undefined) ? argv.folder : '.'; + var { type, folder } = await identify(_folder); + if (ALL[type]) { + const result = await ALL[type].parse(folder); + cb(null, result); + } else { + cb('Error vizion::analyze() for given folder: '+folder); + } + } catch (err) { + cb(err); + } }; -vizion.isUpToDate = function(argv, cb) { - var _folder = (argv.folder != undefined) ? argv.folder : '.'; - - identify(_folder, function(type, folder) { - if (ALL[type]) - return ALL[type].isUpdated(folder, cb); - else +vizion.isUpToDate = async function(argv, cb) { + try { + var _folder = (argv.folder !== undefined) ? argv.folder : '.'; + var { type, folder } = await identify(_folder); + if (ALL[type]) { + const result = await ALL[type].isUpdated(folder); + cb(null, result); + } else{ return cb('Error vizion::isUpToDate() for given folder: '+folder); - }); + } + } catch (err) { + cb(err); + } }; -vizion.update = function(argv, cb) { - var _folder = (argv.folder != undefined) ? argv.folder : '.'; - - identify(_folder, function(type, folder) { - if (ALL[type]) - return ALL[type].update(folder, cb); - else +vizion.update = async function(argv, cb) { + try { + var _folder = (argv.folder !== undefined) ? argv.folder : '.'; + var { type, folder } = await identify(_folder); + if (ALL[type]) { + const result = await ALL[type].update(folder); + cb(null, result); + } else { return cb('Error vizion::update() for given folder: '+folder); - }); + } + } catch (err) { + cb(err); + } }; -vizion.revertTo = function(argv, cb) { - var revision = (argv.revision) ? argv.revision : false; - var _folder = (argv.folder != undefined) ? argv.folder : '.'; +vizion.revertTo = async function(argv, cb) { + try { + var revision = (argv.revision) ? argv.revision : false; + var _folder = (argv.folder !== undefined) ? argv.folder : '.'; - if (!(revision && /^[A-Fa-f0-9]+$/.test(revision))) return cb({msg: 'Cannot revert to an invalid commit revision', path: _folder}); + if (!(revision && /^[A-Fa-f0-9]+$/.test(revision))) return cb({msg: 'Cannot revert to an invalid commit revision', path: _folder}); - identify(_folder, function(type, folder) { - if (ALL[type]) - return ALL[type].revert({folder: folder, revision: revision}, cb); - else + var { type, folder } = await identify(_folder); + if (ALL[type]) { + const result = await ALL[type].revert({folder: folder, revision: revision}); + cb(null, result); + } else { return cb('Error vizion::analyze() for given folder: '+folder); - }); + } + } catch (err) { + cb(err); + } }; -vizion.prev = function(argv, cb) { - var _folder = (argv.folder != undefined) ? argv.folder : '.'; - - identify(_folder, function(type, folder) { - if (ALL[type]) - return ALL[type].prev(folder, cb); - else +vizion.prev = async function(argv, cb) { + try { + var _folder = (argv.folder !== undefined) ? argv.folder : '.'; + var { type, folder } = await identify(_folder); + if (ALL[type]) { + const result = await ALL[type].prev(folder); + cb(null, result); + } else { return cb('Error vizion::prev() for given folder: '+folder); - }); + } + } catch (err) { + cb(err); + } }; -vizion.next = function(argv, cb) { - var _folder = (argv.folder != undefined) ? argv.folder : '.'; - - identify(_folder, function(type, folder) { - if (ALL[type]) - return ALL[type].next(folder, cb); - else +vizion.next = async function(argv, cb) { + try { + var _folder = (argv.folder !== undefined) ? argv.folder : '.'; + var { type, folder } = await identify(_folder); + if (ALL[type]) { + const result = await ALL[type].next(folder); + cb(null, result); + } else { return cb('Error vizion::next() for given folder: '+folder); - }); + } + } catch (err) { + cb(err); + } }; diff --git a/package.json b/package.json index 6544fd2..cb4b4c3 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vizion", "version": "2.2.0", "engines": { - "node": ">=4.0" + "node": ">=8.0" }, "author": { "name": "Keymetrics", @@ -30,7 +30,6 @@ "test": "mocha" }, "dependencies": { - "async": "^2.6.3", "git-node-fs": "^1.0.0", "ini": "^1.3.5", "js-git": "^0.7.8" diff --git a/test/unit/git.test.js b/test/unit/git.test.js index ecf0e9c..50fb315 100644 --- a/test/unit/git.test.js +++ b/test/unit/git.test.js @@ -3,8 +3,8 @@ var fs = require('fs'); var sinon = require('sinon'); var ini = require('ini'); -var git = require("../../lib/git/git.js"); -var jsGitService = require("../../lib/git/js-git-service.js"); +var git = require("../../lib/git/git"); +var jsGitService = require("../../lib/git/js-git-service"); describe('Unit: git', function () { @@ -25,22 +25,16 @@ describe('Unit: git', function () { cb(null, data); }); - parseStub = sinon.stub(ini, 'parse').callsFake(function (myData) { + parseStub = sinon.stub(ini, 'parse').callsFake(async function (myData) { expect(myData).to.eq(data); return config; }); }); - it('ok', function it(done) { - git.parseGitConfig(folder, function (err, myConfig) { - if (err) { - return done(err); - } - - expect(myConfig).to.eq(config); - done(); - }) + it('ok', async function it() { + const myConfig = await git.parseGitConfig(folder); + expect(myConfig).to.eq(config); }); after(function afterTest() { @@ -59,24 +53,17 @@ describe('Unit: git', function () { var parseGitConfigStub; before(function beforeTest() { - parseGitConfigStub = sinon.stub(git, 'parseGitConfig').callsFake(function (myFolder, cb) { + parseGitConfigStub = sinon.stub(git, 'parseGitConfig').callsFake(async function (myFolder) { expect(myFolder).to.eq(folder); - - cb(null, config); + return config; }); }); - it('ok', function it(done) { - git.getUrl(folder, function (err, data) { - if (err) { - return done(err); - } - - expect(data).to.deep.eq({ - "type": "git", - "url": "test-url" - }); - done(); + it('ok', async function it() { + const data = await git.getUrl(folder); + expect(data).to.deep.eq({ + "type": "git", + "url": "test-url" }); }); @@ -103,17 +90,11 @@ describe('Unit: git', function () { }); }); - it('ok', function it(done) { - git.getCommitInfo(folder, data, function (err, data) { - if (err) { - return done(err); - } - - expect(data).to.deep.eq({ - "revision": commit.hash, - "comment": commit.message - }); - done(); + it('ok', async function it() { + data = await git.getCommitInfo(folder, data); + expect(data).to.deep.eq({ + "revision": commit.hash, + "comment": commit.message }); }); @@ -139,16 +120,10 @@ describe('Unit: git', function () { }); }); - it('ok', function it(done) { - git.getBranch(folder, data, function (err, data) { - if (err) { - return done(err); - } - - expect(data).to.deep.eq({ - "branch": "master", - }); - done(); + it('ok', async function it() { + data = await git.getBranch(folder, data); + expect(data).to.deep.eq({ + "branch": "master", }); }); @@ -171,27 +146,20 @@ describe('Unit: git', function () { var parseGitConfigStub; before(function beforeTest() { - parseGitConfigStub = sinon.stub(git, 'parseGitConfig').callsFake(function (myFolder, cb) { + parseGitConfigStub = sinon.stub(git, 'parseGitConfig').callsFake(async function (myFolder) { expect(myFolder).to.eq(folder); - - cb(null, config); + return config; }); }); - it('ok', function it(done) { - git.getRemote(folder, data, function (err, data) { - if (err) { - return done(err); - } - - expect(data).to.deep.eq({ - "remote": "origin", - "remotes": [ - "origin", - "other" - ] - }); - done(); + it('ok', async function it() { + data = await git.getRemote(folder, data); + expect(data).to.deep.eq({ + "remote": "origin", + "remotes": [ + "origin", + "other" + ] }); }); @@ -220,18 +188,12 @@ describe('Unit: git', function () { }); }); - it('ok', function it(done) { - git.isCurrentBranchOnRemote(folder, data, function (err, data) { - if (err) { - return done(err); - } - - expect(data).to.deep.eq({ - "branch": "my-branch", - "branch_exists_on_remote": false, - "remote": "my-remote" - }); - done(); + it('ok', async function it() { + data = await git.isCurrentBranchOnRemote(folder, data); + expect(data).to.deep.eq({ + "branch": "my-branch", + "branch_exists_on_remote": false, + "remote": "my-remote" }); }); @@ -251,18 +213,12 @@ describe('Unit: git', function () { }); }); - it('ok', function it(done) { - git.isCurrentBranchOnRemote(folder, data, function (err, data) { - if (err) { - return done(err); - } - - expect(data).to.deep.eq({ - "branch": "my-branch", - "branch_exists_on_remote": true, - "remote": "my-remote" - }); - done(); + it('ok', async function it() { + data = await git.isCurrentBranchOnRemote(folder, data); + expect(data).to.deep.eq({ + "branch": "my-branch", + "branch_exists_on_remote": true, + "remote": "my-remote" }); }); @@ -299,22 +255,16 @@ describe('Unit: git', function () { }); }); - it('ok', function it(done) { - git.getPrevNext(folder, data, function (err, data) { - if (err) { - return done(err); - } - - expect(data).to.deep.eq({ - "ahead": false, - "branch": "my-branch", - "branch_exists_on_remote": true, - "next_rev": "3", - "prev_rev": "1", - "remote": "my-remote", - "revision": "2" - }); - done(); + it('ok', async function it() { + data = await git.getPrevNext(folder, data); + expect(data).to.deep.eq({ + "ahead": false, + "branch": "my-branch", + "branch_exists_on_remote": true, + "next_rev": "3", + "prev_rev": "1", + "remote": "my-remote", + "revision": "2" }); }); @@ -323,4 +273,4 @@ describe('Unit: git', function () { }); }); -}); \ No newline at end of file +}); From 80d47c8b5211506b868d0eac8ec323b2de900dc7 Mon Sep 17 00:00:00 2001 From: vedi Date: Tue, 14 Apr 2020 11:33:50 +0300 Subject: [PATCH 2/2] Fix typo --- lib/svn/svn.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/svn/svn.js b/lib/svn/svn.js index 2e44377..a1f8514 100644 --- a/lib/svn/svn.js +++ b/lib/svn/svn.js @@ -125,7 +125,7 @@ svn.update = async function(folder) { res.success = true; res.current_revision = new_rev[1]; } - return resovle(res); + return resolve(res); }); }); };