Unverified Commit 1fbd20dc authored by Brandon Duffany's avatar Brandon Duffany Committed by GitHub

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
parent f33ba2c5
...@@ -54,7 +54,7 @@ export async function createApp({ ...@@ -54,7 +54,7 @@ export async function createApp({
process.exit(1) process.exit(1)
} }
repoInfo = getRepoInfo(repoUrl, examplePath) repoInfo = await getRepoInfo(repoUrl, examplePath)
if (!repoInfo) { if (!repoInfo) {
console.error( console.error(
......
...@@ -14,12 +14,26 @@ export async function isUrlOk(url: string) { ...@@ -14,12 +14,26 @@ export async function isUrlOk(url: string) {
return res.statusCode === 200 return res.statusCode === 200
} }
export function getRepoInfo( export async function getRepoInfo(
url: URL, url: URL,
examplePath?: string examplePath?: string
): RepoInfo | undefined { ): Promise<RepoInfo | undefined> {
const [, username, name, t, _branch, ...file] = url.pathname.split('/') const [, username, name, t, _branch, ...file] = url.pathname.split('/')
const filePath = examplePath ? examplePath.replace(/^\//, '') : file.join('/') 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 // If examplePath is available, the branch name takes the entire path
const branch = examplePath const branch = examplePath
? `${_branch}/${file.join('/')}`.replace(new RegExp(`/${filePath}|/$`), '') ? `${_branch}/${file.join('/')}`.replace(new RegExp(`/${filePath}|/$`), '')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment