Browse Source

Basic implementation for GitHub deployments

master
Leo Lamprecht 8 years ago
parent
commit
04e50b24e4
No known key found for this signature in database GPG Key ID: B08517883D5E0E10
  1. 26
      bin/now-deploy.js
  2. 82
      lib/github.js
  3. 4
      package.json

26
bin/now-deploy.js

@ -22,6 +22,7 @@ import toHumanPath from '../lib/utils/to-human-path'
import promptOptions from '../lib/utils/prompt-options' import promptOptions from '../lib/utils/prompt-options'
import {handleError, error} from '../lib/error' import {handleError, error} from '../lib/error'
import readMetaData from '../lib/read-metadata' import readMetaData from '../lib/read-metadata'
import {onGitHub, isRepoPath} from '../lib/github'
const argv = minimist(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
string: [ string: [
@ -192,18 +193,35 @@ if (argv.h || argv.help) {
async function sync(token) { async function sync(token) {
const start = Date.now() const start = Date.now()
const rawPath = argv._[0]
if (!quiet) { const stopDeployment = msg => {
console.log(`> Deploying ${chalk.bold(toHumanPath(path))}`) error(msg)
process.exit(1)
} }
try { try {
await stat(path) await stat(path)
} catch (err) { } catch (err) {
error(`Could not read directory ${chalk.bold(path)}`) const repo = await onGitHub(rawPath, debug)
process.exit(1)
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 deploymentType
let hasPackage let hasPackage

82
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'
}

4
package.json

@ -73,6 +73,7 @@
"copy-paste": "1.3.0", "copy-paste": "1.3.0",
"cross-spawn": "5.0.1", "cross-spawn": "5.0.1",
"docker-file-parser": "0.1.0", "docker-file-parser": "0.1.0",
"download": "5.0.2",
"email-prompt": "0.1.8", "email-prompt": "0.1.8",
"email-validator": "1.0.7", "email-validator": "1.0.7",
"fs-promise": "1.0.0", "fs-promise": "1.0.0",
@ -89,7 +90,8 @@
"socket.io-client": "1.6.0", "socket.io-client": "1.6.0",
"spdy": "3.4.4", "spdy": "3.4.4",
"split-array": "1.0.1", "split-array": "1.0.1",
"text-table": "0.2.0" "text-table": "0.2.0",
"tmp-promise": "1.0.2"
}, },
"devDependencies": { "devDependencies": {
"alpha-sort": "1.0.2", "alpha-sort": "1.0.2",

Loading…
Cancel
Save