You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

89 lines
1.6 KiB

// Native
import path from 'path'
// Packages
import fs from 'fs-promise'
import fetch from 'node-fetch'
import download from 'download'
import tmp from 'tmp-promise'
// Ours
import {error} from './error'
const exists = async repoPath => {
const apiURL = `https://api.github.com/repos/${repoPath}`
let request
let response
try {
request = await fetch(apiURL)
response = await request.json()
} catch (err) {
error(`Not able to check if repo exists - ${err.message}`)
return false
}
if (!response.name) {
return false
}
return response
}
const downloadRepo = async repoPath => {
const url = `https://api.github.com/repos/${repoPath}/tarball`
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
})
await download(url, tmpDir.path, {
extract: true
})
const tmpContents = await fs.readdir(tmpDir.path)
tmpDir.path = path.join(tmpDir.path, tmpContents[0])
return tmpDir
}
export const isRepoPath = path => {
if (!path) {
return false
}
return /[^\s\\]\/[^\s\\]/g.test(path)
}
export const onGitHub = async (path, debug) => {
let repo = await exists(path)
try {
repo = await exists(path)
} catch (err) {
if (debug) {
console.log(`Repository "${path}" does not exist on GitHub`)
}
return false
}
if (!repo) {
return false
}
let tmpDir
try {
tmpDir = await downloadRepo(path)
} catch (err) {
error(`Not able to download repo: ${err.stack}`)
process.exit(1)
}
return tmpDir
}