rsnext/packages/create-next-app/helpers/examples.ts
Brandon Duffany 1fbd20dc37
Allow top-level GitHub repo URLs as examples (#11691)
* Support top-level GitHub repo URLs

Adds support for top-level GitHub repo URLs, e.g.
https://github.com/username/my-cool-next-example.

This is already possible, but you have to enter "/tree/master" (or the
desired branch name) after the repo URL.

The change itself simply expands that URL to
https://github.com/username/cool-next-example/tree/$DEFAULT_BRANCH_NAME
where DEFAULT_BRANCH_NAME is the default branch configured for the repo
in GitHub.

* Fix formatting issues
2020-04-06 15:57:19 +02:00

85 lines
2.5 KiB
TypeScript

import got from 'got'
import promisePipe from 'promisepipe'
import tar from 'tar'
export type RepoInfo = {
username: string
name: string
branch: string
filePath: string
}
export async function isUrlOk(url: string) {
const res = await got(url).catch(e => e)
return res.statusCode === 200
}
export async function getRepoInfo(
url: URL,
examplePath?: string
): Promise<RepoInfo | undefined> {
const [, username, name, t, _branch, ...file] = url.pathname.split('/')
const filePath = examplePath ? examplePath.replace(/^\//, '') : file.join('/')
// Support repos whose entire purpose is to be a NextJS example, e.g.
// https://github.com/:username/:my-cool-nextjs-example-repo-name.
if (t === undefined) {
const infoResponse = await got(
`https://api.github.com/repos/${username}/${name}`
).catch(e => e)
if (infoResponse.statusCode !== 200) {
return
}
const info = JSON.parse(infoResponse.body)
return { username, name, branch: info['default_branch'], filePath }
}
// If examplePath is available, the branch name takes the entire path
const branch = examplePath
? `${_branch}/${file.join('/')}`.replace(new RegExp(`/${filePath}|/$`), '')
: _branch
if (username && name && branch && t === 'tree') {
return { username, name, branch, filePath }
}
}
export function hasRepo({ username, name, branch, filePath }: RepoInfo) {
const contentsUrl = `https://api.github.com/repos/${username}/${name}/contents`
const packagePath = `${filePath ? `/${filePath}` : ''}/package.json`
return isUrlOk(contentsUrl + packagePath + `?ref=${branch}`)
}
export function hasExample(name: string): Promise<boolean> {
return isUrlOk(
`https://api.github.com/repos/zeit/next.js/contents/examples/${encodeURIComponent(
name
)}/package.json`
)
}
export function downloadAndExtractRepo(
root: string,
{ username, name, branch, filePath }: RepoInfo
): Promise<void> {
return promisePipe(
got.stream(
`https://codeload.github.com/${username}/${name}/tar.gz/${branch}`
),
tar.extract(
{ cwd: root, strip: filePath ? filePath.split('/').length + 1 : 1 },
[`${name}-${branch}${filePath ? `/${filePath}` : ''}`]
)
)
}
export function downloadAndExtractExample(
root: string,
name: string
): Promise<void> {
return promisePipe(
got.stream('https://codeload.github.com/zeit/next.js/tar.gz/canary'),
tar.extract({ cwd: root, strip: 3 }, [`next.js-canary/examples/${name}`])
)
}