Skip to content

Commit

Permalink
Updating functions to async await and adding proper cloning functiona…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
jdalrymple committed Jul 21, 2017
1 parent d409fd5 commit 3a6b8e4
Showing 1 changed file with 68 additions and 56 deletions.
124 changes: 68 additions & 56 deletions lib/Hg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

Expand All @@ -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) {
Expand All @@ -130,4 +142,4 @@ class Hg {
}
}

module.exports = Hg;
module.exports = Hg;

0 comments on commit 3a6b8e4

Please sign in to comment.