2019-07-19 21:55:30 +02:00
|
|
|
import got from 'got'
|
|
|
|
import tar from 'tar'
|
2020-04-06 16:46:04 +02:00
|
|
|
import { Stream } from 'stream'
|
|
|
|
import { promisify } from 'util'
|
|
|
|
|
|
|
|
const pipeline = promisify(Stream.pipeline)
|
2019-07-19 21:55:30 +02:00
|
|
|
|
2020-02-27 16:32:33 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:57:19 +02:00
|
|
|
export async function getRepoInfo(
|
2020-02-27 16:32:33 +01:00
|
|
|
url: URL,
|
|
|
|
examplePath?: string
|
2020-04-06 15:57:19 +02:00
|
|
|
): Promise<RepoInfo | undefined> {
|
2020-02-27 16:32:33 +01:00
|
|
|
const [, username, name, t, _branch, ...file] = url.pathname.split('/')
|
|
|
|
const filePath = examplePath ? examplePath.replace(/^\//, '') : file.join('/')
|
2020-04-06 15:57:19 +02:00
|
|
|
|
|
|
|
// 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 }
|
|
|
|
}
|
|
|
|
|
2020-02-27 16:32:33 +01:00
|
|
|
// 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(
|
2019-07-19 21:55:30 +02:00
|
|
|
`https://api.github.com/repos/zeit/next.js/contents/examples/${encodeURIComponent(
|
|
|
|
name
|
|
|
|
)}/package.json`
|
2020-02-27 16:32:33 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function downloadAndExtractRepo(
|
|
|
|
root: string,
|
|
|
|
{ username, name, branch, filePath }: RepoInfo
|
|
|
|
): Promise<void> {
|
2020-04-06 16:46:04 +02:00
|
|
|
return pipeline(
|
2020-02-27 16:32:33 +01:00
|
|
|
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}` : ''}`]
|
|
|
|
)
|
|
|
|
)
|
2019-07-19 21:55:30 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 16:32:33 +01:00
|
|
|
export function downloadAndExtractExample(
|
2019-07-19 21:55:30 +02:00
|
|
|
root: string,
|
|
|
|
name: string
|
|
|
|
): Promise<void> {
|
2020-04-06 16:46:04 +02:00
|
|
|
return pipeline(
|
2019-07-19 21:55:30 +02:00
|
|
|
got.stream('https://codeload.github.com/zeit/next.js/tar.gz/canary'),
|
|
|
|
tar.extract({ cwd: root, strip: 3 }, [`next.js-canary/examples/${name}`])
|
|
|
|
)
|
|
|
|
}
|
2020-04-07 19:11:29 +02:00
|
|
|
|
|
|
|
export async function listExamples(): Promise<any> {
|
|
|
|
const res = await got(
|
|
|
|
'https://api.github.com/repositories/70107786/contents/examples'
|
|
|
|
)
|
|
|
|
return JSON.parse(res.body)
|
|
|
|
}
|