From b2ca83e072c7930e3792f3e05e2211686e61a502 Mon Sep 17 00:00:00 2001 From: Justin Dalrymple Date: Mon, 24 Jul 2017 14:17:40 -0400 Subject: [PATCH] Split up the cloning functions --- .eslintrc | 3 +- src/Hg.js | 96 +++++++++++++++++++++++++++---------------------------- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/.eslintrc b/.eslintrc index 4ec973f3..2bae76f7 100644 --- a/.eslintrc +++ b/.eslintrc @@ -6,6 +6,7 @@ }, "extends": "airbnb", rules:{ - arrow-body-style: [2, "as-needed"] + arrow-body-style: [2, "as-needed"], + no-await-in-loop: [1] } } \ No newline at end of file diff --git a/src/Hg.js b/src/Hg.js index 6d6add22..9f989f22 100644 --- a/src/Hg.js +++ b/src/Hg.js @@ -22,7 +22,7 @@ function moveFiles(source, destination, files) { async function cloneMultipleAndMerge(fromRepos, combinedRepo) { const mergedRepos = []; - for (repo of fromRepos) { + for (let fromRepo of fromRepos) { 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'); } @@ -33,28 +33,28 @@ async function cloneMultipleAndMerge(fromRepos, combinedRepo) { name += `-${ShortID.generate()}`; } - await combinedRepo.pull({ source: fromRepo, force: true }) - await combinedRepo.update({ clean: true, revision: 'default' }) + await combinedRepo.pull({ source: fromRepo, force: true }); + await combinedRepo.update({ clean: true, revision: 'default' }); - let files = await Globby(['*', '!.hg'], { dot: true, cwd: combinedRepo.path }) + const files = await Globby(['*', '!.hg'], { dot: true, cwd: combinedRepo.path }); - await moveFiles(combinedRepo.path, Path.join(combinedRepo.path, name), files) - await combinedRepo.add() + await moveFiles(combinedRepo.path, Path.join(combinedRepo.path, name), files); + await combinedRepo.add(); try { - await combinedRepo.remove({ after: true }) + await combinedRepo.remove({ after: true }); } catch (errorInfo) { if (!errorInfo.error.message.includes('still exists')) throw errorInfo.error; } - await combinedRepo.commit(`Moving repository ${name} into folder ${name}`) + await combinedRepo.commit(`Moving repository ${name} into folder ${name}`); - if (!mergedRepos.length) return; + if (!mergedRepos.length) break; - await combinedRepo.merge() + await combinedRepo.merge(); try { - await combinedRepo.commit(`Merging ${name} into combined`) + await 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')) { @@ -68,43 +68,28 @@ async function cloneMultipleAndMerge(fromRepos, combinedRepo) { return combinedRepo; } -async function cloneSingleOrMultiple(from, to, pythonPath) { - switch (from.constructor) { - case Array: - { - const newRepo = new HgRepo(to, pythonPath); - - await newRepo.init(); - - return cloneMultipleAndMerge(from, newRepo); - } - case Object: - { - const newRepo = new HgRepo(to || { - url: from.url, - password: from.password, - username: from.username, - }, pythonPath); - - const url = Utils.buildRepoURL(from); - - await Command.run('hg clone', newRepo.path, [url, newRepo.path]); - - return newRepo; - } - case String: - { - const newRepo = new HgRepo(to || { - url: from, - }, pythonPath); +async function cloneSingle(from, to, pythonPath) { + let repo; + let url; + + if (from.constructor === Object) { + repo = new HgRepo(to || { + url: from.url, + password: from.password, + username: from.username, + }, pythonPath); + + url = Utils.buildRepoURL(from); + } else { + url = from; + repo = new HgRepo(to || { + url: from, + }, pythonPath); + } - await Command.run('hg clone', newRepo.path, [from, newRepo.path]); + await Command.run('hg clone', repo.path, [url, repo.path]); - return newRepo; - } - default: - return new TypeError('Incorrect type of from parameter. Must be an array or an object'); - } + return repo; } class Hg { @@ -113,10 +98,21 @@ class Hg { } async clone(from, to = undefined, done = undefined) { - try { - const repo = await cloneSingleOrMultiple(from, to, this.pythonPath); + let repo; - return Utils.asCallback(repo, done); + try { + switch (from.constructor) { + case Array: + { + repo = await cloneMultipleAndMerge(from, to); + break; + } + case String || Object: + repo = await cloneSingle(from, to, this.pythonPath); + break; + default: + return new TypeError('Incorrect type of from parameter. Must be an array or an object'); + } } catch (e) { if (e.message.includes('not found')) { throw new TypeError('Incorrect type of from parameter. Clone source not found'); @@ -124,6 +120,8 @@ class Hg { throw e; } } + + return Utils.asCallback(repo, done); } async create(to, done = undefined) {