diff --git a/bin/now-deploy.js b/bin/now-deploy.js index 84f0922..332bf54 100755 --- a/bin/now-deploy.js +++ b/bin/now-deploy.js @@ -22,6 +22,7 @@ import toHumanPath from '../lib/utils/to-human-path' import promptOptions from '../lib/utils/prompt-options' import {handleError, error} from '../lib/error' import readMetaData from '../lib/read-metadata' +import {onGitHub, isRepoPath} from '../lib/github' const argv = minimist(process.argv.slice(2), { string: [ @@ -192,18 +193,35 @@ if (argv.h || argv.help) { async function sync(token) { const start = Date.now() + const rawPath = argv._[0] - if (!quiet) { - console.log(`> Deploying ${chalk.bold(toHumanPath(path))}`) + const stopDeployment = msg => { + error(msg) + process.exit(1) } try { await stat(path) } catch (err) { - error(`Could not read directory ${chalk.bold(path)}`) - process.exit(1) + const repo = await onGitHub(rawPath, debug) + + if (repo) { + path = repo + } else if (isRepoPath(rawPath)) { + stopDeployment(`This path neither exists, nor is there a repository named "${rawPath}" on GitHub`) + } else { + stopDeployment(`Could not read directory ${chalk.bold(path)}`) + } + + console.log(repo) + } + + if (!quiet) { + console.log(`> Deploying ${chalk.bold(toHumanPath(path))}`) } + process.exit() + let deploymentType let hasPackage diff --git a/lib/github.js b/lib/github.js new file mode 100644 index 0000000..12525e4 --- /dev/null +++ b/lib/github.js @@ -0,0 +1,82 @@ +// Packages +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 + + try { + request = await fetch(apiURL) + } catch(err) { + error(`Not able to check if repo exists - ${err.message}`) + return false + } + + const res = await request.json() + + if (!res.name) { + return false + } + + return res +} + +const downloadRepo = async repoPath => { + const url = `https://api.github.com/repos/${repoPath}/tarball` + + const tmpDir = await tmp.dir({ + keep: true + }) + + try { + await download(url, tmpDir.path, { + extract: true + }) + } catch (err) { + error(`Not able to download repo: ${err.stack}`) + } + + return tmpDir +} + +export const isRepoPath = path => { + if (!path) { + return false + } + + const slashCount = path.split('/').length - 1 + + if (!slashCount || slashCount > 1) { + return false + } + + return true +} + +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 + } + + const tmpDir = await downloadRepo(path) + console.log(tmpDir) + + return 'test' +} diff --git a/package.json b/package.json index ccda899..3b6c4e8 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ "copy-paste": "1.3.0", "cross-spawn": "5.0.1", "docker-file-parser": "0.1.0", + "download": "5.0.2", "email-prompt": "0.1.8", "email-validator": "1.0.7", "fs-promise": "1.0.0", @@ -89,7 +90,8 @@ "socket.io-client": "1.6.0", "spdy": "3.4.4", "split-array": "1.0.1", - "text-table": "0.2.0" + "text-table": "0.2.0", + "tmp-promise": "1.0.2" }, "devDependencies": { "alpha-sort": "1.0.2",