From 9d7746f9ed51d5a1407c3fc133bd876553b1849d Mon Sep 17 00:00:00 2001 From: Leo Lamprecht Date: Sun, 27 Nov 2016 22:14:56 +0100 Subject: [PATCH] Support for deploying GitHub URLs --- lib/github.js | 33 +++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 34 insertions(+) diff --git a/lib/github.js b/lib/github.js index a257a3c..8c3436d 100644 --- a/lib/github.js +++ b/lib/github.js @@ -1,13 +1,16 @@ // Native import path from 'path' +import url from 'url' // Packages import fs from 'fs-promise' import download from 'download' import tmp from 'tmp-promise' +import isURL from 'is-url' const downloadRepo = async repoPath => { const pathParts = gitPathParts(repoPath) + console.log(pathParts) const url = `https://api.github.com/repos/${pathParts.main}/tarball/${pathParts.ref}` const tmpDir = await tmp.dir({ @@ -32,9 +35,29 @@ const downloadRepo = async repoPath => { return tmpDir } +const splittedURL = fullURL => { + const pathParts = url.parse(fullURL).path.split('/') + pathParts.shift() + + // Set path to repo... + const main = pathParts[0] + '/' + pathParts[1] + + // ...and then remove it from the parts + pathParts.splice(0, 2) + + return { + main, + ref: pathParts.length >= 2 ? pathParts[1] : '' + } +} + export const gitPathParts = main => { let ref = '' + if (isURL(main)) { + return splittedURL(main) + } + if (main.split('/')[1].includes('#')) { const parts = main.split('#') @@ -50,6 +73,16 @@ export const isRepoPath = path => { return false } + if (isURL(path)) { + const urlParts = url.parse(path) + const slashCount = (urlParts.path.match(new RegExp('/', 'g')) || []).length + const notBare = slashCount >= 2 + + if (urlParts.host === 'github.com' && notBare) { + return true + } + } + return /[^\s\\]\/[^\s\\]/g.test(path) } diff --git a/package.json b/package.json index 3b6c4e8..fb74cd2 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,7 @@ "graceful-fs": "4.1.11", "ignore": "3.2.0", "ini": "1.3.4", + "is-url": "1.2.2", "minimist": "1.2.0", "ms": "0.7.2", "node-fetch": "1.6.3",