// Native
import path from 'path'

// Packages
import fs from 'fs-promise'
import download from 'download'
import tmp from 'tmp-promise'

const downloadRepo = async repoPath => {
  const url = `https://api.github.com/repos/${repoPath}/tarball`

  const tmpDir = await tmp.dir({
    // We'll remove it manually once deployment is done
    keep: true,
    // Recursively remove directory when calling respective method
    unsafeCleanup: 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])

  return tmpDir
}

export const isRepoPath = path => {
  if (!path) {
    return false
  }

  return /[^\s\\]\/[^\s\\]/g.test(path)
}

export const onGitHub = async (path, debug) => {
  let tmpDir = false

  try {
    tmpDir = await downloadRepo(path)
  } catch (err) {
    if (debug) {
      console.log(`Could not download "${path}" repo from GitHub`)
    }
  }

  return tmpDir
}

@@ -184,63 +184,129 @@
 nst 
-url = %60https://api.github.com/repos/$%7BrepoPath%7D/tarball
+pathParts = gitPathParts(repoPath)%0A  const url = %60https://api.github.com/repos/$%7BpathParts.main%7D/tarball/$%7BpathParts.ref%7D
 %60%0A%0A 
@@ -764,24 +764,223 @@
 n tmpDir%0A%7D%0A%0A
+export const gitPathParts = main =%3E %7B%0A  let ref = ''%0A%0A  if (main.split('/')%5B1%5D.includes('#')) %7B%0A    const parts = main.split('#')%0A%0A    ref = parts%5B1%5D%0A    main = parts%5B0%5D%0A  %7D%0A%0A  return %7Bmain, ref%7D%0A%7D%0A%0A
 export const

Support for git references