From 3a6b8e4385ee85d8d8349af69bc59875ed306ccd Mon Sep 17 00:00:00 2001 From: Justin Dalrymple Date: Fri, 21 Jul 2017 15:20:09 -0400 Subject: [PATCH] Updating functions to async await and adding proper cloning functionality --- lib/Hg.js | 124 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 68 insertions(+), 56 deletions(-) diff --git a/lib/Hg.js b/lib/Hg.js index 18bacbd8..87b9d791 100644 --- a/lib/Hg.js +++ b/lib/Hg.js @@ -23,70 +23,82 @@ function cloneMultipleAndMerge(fromRepos, combinedRepo) { const mergedRepos = []; return Promise.each(fromRepos, (fromRepo) => { - if (fromRepo.constructor !== String) { - throw new TypeError('Incorrect type of from parameter. Clone source in array is an invalid type. Must be an Object'); - } - - let name = Path.basename(fromRepo); - - if (mergedRepos.includes(name)) { - name += `-${ShortID.generate()}`; - } - - return combinedRepo.pull({ source: fromRepo, force: true }) - .then(() => combinedRepo.update({ clean: true, revision: 'default' })) - .then(() => Globby(['*', '!.hg'], { dot: true, cwd: combinedRepo.path })) - .then(files => moveFiles(combinedRepo.path, Path.join(combinedRepo.path, name), files)) - .then(() => combinedRepo.add()) - .then(() => combinedRepo.remove({ after: true })) - .catch((errorInfo) => { - if (!errorInfo.error.message.includes('still exists')) throw errorInfo.error; - }) - .then(() => combinedRepo.commit(`Moving repository ${name} into folder ${name}`)) - .then(() => { - if (!mergedRepos.length) return Promise.resolve(); - - return combinedRepo.merge() - .then(() => combinedRepo.commit(`Merging ${name} into combined`)) - .catch((errorInfo) => { - if (!errorInfo.error.message.includes('nothing to merge') && - !errorInfo.error.message.includes('merging with a working directory ancestor')) { - throw errorInfo.error; - } - }); - }) - .then(() => { - mergedRepos.push(name); - }); - }) - .then(() => combinedRepo); + if (fromRepo.constructor !== String || fromRepo.constructor !== Object) { + throw new TypeError('Incorrect type of from parameter. Clone source in array is an invalid type. Must be an String or an Object'); + } + + let name = Path.basename(fromRepo); + + if (mergedRepos.includes(name)) { + name += `-${ShortID.generate()}`; + } + + return combinedRepo.pull({ source: fromRepo, force: true }) + .then(() => combinedRepo.update({ clean: true, revision: 'default' })) + .then(() => Globby(['*', '!.hg'], { dot: true, cwd: combinedRepo.path })) + .then(files => moveFiles(combinedRepo.path, Path.join(combinedRepo.path, name), files)) + .then(() => combinedRepo.add()) + .then(() => combinedRepo.remove({ after: true })) + .catch((errorInfo) => { + if (!errorInfo.error.message.includes('still exists')) throw errorInfo.error; + }) + .then(() => combinedRepo.commit(`Moving repository ${name} into folder ${name}`)) + .then(() => { + if (!mergedRepos.length) return Promise.resolve(); + + return combinedRepo.merge() + .then(() => combinedRepo.commit(`Merging ${name} into combined`)) + .catch((errorInfo) => { + if (!errorInfo.error.message.includes('nothing to merge') && + !errorInfo.error.message.includes('merging with a working directory ancestor')) { + throw errorInfo.error; + } + }); + }) + .then(() => { + mergedRepos.push(name); + }); + }) + .then(() => combinedRepo); } -function cloneSingleOrMultiple(from, to, pythonPath) { +async function cloneSingleOrMultiple(from, to, pythonPath) { const newRepo = new HgRepo(to, pythonPath); switch (from.constructor) { case String: - { - return Command.run('hg clone', newRepo.path, [from, newRepo.path]) - .then(() => newRepo) - .catch((results) => { - if (results.error.message.includes('not found')) { - throw new TypeError('Incorrect type of from parameter. Clone source not found'); - } - }); + try { + await Command.run('hg clone', newRepo.path, [from, newRepo.path]) + } catch (error) { + if (results.error.message.includes('not found')) { + throw new TypeError('Incorrect type of from parameter. Clone source not found'); + } } - case Array: - { - return newRepo.init() - .then(() => cloneMultipleAndMerge(from, newRepo)); + return newRepo; + case Object: + let url; + + if (from.password && from.username) { + url = `https://${from.username}:${from.password}@${from.url.split('@').pop()}`; + } else { + url = from.url; } - default: - { - return Promise.reject(new TypeError('Incorrect type of from parameter. Must be an array or an object')); + try { + await Command.run('hg clone', newRepo.path, [url, newRepo.path]) + } catch (error) { + if (error.error.message.includes('not found')) { + throw new TypeError('Incorrect type of from parameter. Clone source not found'); + } } + + return newRepo; + case Array: + return newRepo.init() + .then(() => cloneMultipleAndMerge(from, newRepo)); + default: + return Promise.reject(new TypeError('Incorrect type of from parameter. Must be an array or an object')); } } @@ -108,8 +120,8 @@ class Hg { const repo = new HgRepo(to, this.pythonPath); return repo.init() - .then(() => repo) - .asCallback(done); + .then(() => repo) + .asCallback(done); } gitify({ gitRepoPath = undefined } = {}, done = undefined) { @@ -130,4 +142,4 @@ class Hg { } } -module.exports = Hg; +module.exports = Hg; \ No newline at end of file