Browse Source

Only do one network roundtrip to GitHub

master
Leo Lamprecht 8 years ago
parent
commit
fd0e39da0e
No known key found for this signature in database GPG Key ID: B08517883D5E0E10
  1. 57
      lib/github.js

57
lib/github.js

@ -3,34 +3,9 @@ import path from 'path'
// Packages // Packages
import fs from 'fs-promise' import fs from 'fs-promise'
import fetch from 'node-fetch'
import download from 'download' import download from 'download'
import tmp from 'tmp-promise' 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 downloadRepo = async repoPath => {
const url = `https://api.github.com/repos/${repoPath}/tarball` const url = `https://api.github.com/repos/${repoPath}/tarball`
@ -41,9 +16,14 @@ const downloadRepo = async repoPath => {
unsafeCleanup: true unsafeCleanup: true
}) })
await download(url, tmpDir.path, { try {
extract: true await download(url, tmpDir.path, {
}) extract: true
})
} catch (err) {
tmpDir.cleanup()
return false
}
const tmpContents = await fs.readdir(tmpDir.path) const tmpContents = await fs.readdir(tmpDir.path)
tmpDir.path = path.join(tmpDir.path, tmpContents[0]) tmpDir.path = path.join(tmpDir.path, tmpContents[0])
@ -60,29 +40,14 @@ export const isRepoPath = path => {
} }
export const onGitHub = async (path, debug) => { export const onGitHub = async (path, debug) => {
let repo = await exists(path) let tmpDir = false
try { try {
repo = await exists(path) tmpDir = await downloadRepo(path)
} catch (err) { } catch (err) {
if (debug) { if (debug) {
console.log(`Repository "${path}" does not exist on GitHub`) console.log(`Could not download "${path}" repo from 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 return tmpDir

Loading…
Cancel
Save