From fd0e39da0ecb3598658aea27fdb09a295373b93d Mon Sep 17 00:00:00 2001 From: Leo Lamprecht Date: Sat, 26 Nov 2016 17:18:48 +0100 Subject: [PATCH] Only do one network roundtrip to GitHub --- lib/github.js | 57 ++++++++++----------------------------------------- 1 file changed, 11 insertions(+), 46 deletions(-) diff --git a/lib/github.js b/lib/github.js index 3135db4..7f97f69 100644 --- a/lib/github.js +++ b/lib/github.js @@ -3,34 +3,9 @@ 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` @@ -41,9 +16,14 @@ const downloadRepo = async repoPath => { unsafeCleanup: true }) - await download(url, tmpDir.path, { - extract: true - }) + try { + await download(url, tmpDir.path, { + extract: true + }) + } catch (err) { + tmpDir.cleanup() + return false + } const tmpContents = await fs.readdir(tmpDir.path) tmpDir.path = path.join(tmpDir.path, tmpContents[0]) @@ -60,29 +40,14 @@ export const isRepoPath = path => { } export const onGitHub = async (path, debug) => { - let repo = await exists(path) + let tmpDir = false try { - repo = await exists(path) + tmpDir = await downloadRepo(path) } catch (err) { 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