Browse Source

Support for private Git repos

master
Leo Lamprecht 8 years ago
parent
commit
6e94359052
No known key found for this signature in database GPG Key ID: B08517883D5E0E10
  1. 77
      lib/git.js

77
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 => {

Loading…
Cancel
Save