From 6e943590523eb36f090c7c90f89ceb8c616a7ccd Mon Sep 17 00:00:00 2001 From: Leo Lamprecht Date: Tue, 29 Nov 2016 19:03:51 +0100 Subject: [PATCH] Support for private Git repos --- lib/git.js | 77 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 16 deletions(-) diff --git a/lib/git.js b/lib/git.js index 0cda2c9..c80306e 100644 --- a/lib/git.js +++ b/lib/git.js @@ -1,6 +1,7 @@ // Native import path from 'path' import url from 'url' +import childProcess from 'child_process' // Packages import fs from 'fs-promise' @@ -8,8 +9,67 @@ import download from 'download' import tmp from 'tmp-promise' import isURL from 'is-url' +const cloneRepo = (parts, tmpDir) => new Promise((resolve, reject) => { + let host + + switch (parts.type) { + case 'GitLab': + host = `gitlab.com` + break + case 'Bitbucket': + host = `bitbucket.org` + break + default: + host = `github.com` + } + + const url = `https://${host}/${parts.main}` + const ref = parts.ref || (parts.type === 'Bitbucket' ? 'default' : 'master') + const cmd = `git clone ${url} --single-branch ${ref}` + + childProcess.exec(cmd, {cwd: tmpDir.path}, (err, stdout) => { + if (err) { + reject(err) + } + + resolve(stdout) + }) +}) + +const renameRepoDir = async (pathParts, tmpDir) => { + const tmpContents = await fs.readdir(tmpDir.path) + + const oldTemp = path.join(tmpDir.path, tmpContents[0]) + const newTemp = path.join(tmpDir.path, pathParts.main.replace('/', '-')) + + await fs.rename(oldTemp, newTemp) + tmpDir.path = newTemp + + return tmpDir +} + const downloadRepo = async repoPath => { const pathParts = gitPathParts(repoPath) + + const tmpDir = await tmp.dir({ + // We'll remove it manually once deployment is done + keep: true, + // Recursively remove directory when calling respective method + unsafeCleanup: true + }) + + let gitInstalled = true + + try { + await cloneRepo(pathParts, tmpDir) + } catch (err) { + gitInstalled = false + } + + if (gitInstalled) { + return await renameRepoDir(pathParts, tmpDir) + } + let url switch (pathParts.type) { @@ -24,13 +84,6 @@ const downloadRepo = async repoPath => { url = `https://api.github.com/repos/${pathParts.main}/tarball/${pathParts.ref}` } - const tmpDir = await tmp.dir({ - // We'll remove it manually once deployment is done - keep: true, - // Recursively remove directory when calling respective method - unsafeCleanup: true - }) - try { await download(url, tmpDir.path, { extract: true @@ -40,15 +93,7 @@ const downloadRepo = async repoPath => { return false } - const tmpContents = await fs.readdir(tmpDir.path) - - const oldTemp = path.join(tmpDir.path, tmpContents[0]) - const newTemp = path.join(tmpDir.path, pathParts.main.replace('/', '-')) - - await fs.rename(oldTemp, newTemp) - tmpDir.path = newTemp - - return tmpDir + return await renameRepoDir(pathParts, tmpDir) } const capitalizePlatform = name => {