2020-05-28 10:23:10 +02:00
|
|
|
/* eslint-disable import/no-extraneous-dependencies */
|
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'
|
2022-09-05 18:11:12 +02:00
|
|
|
import { join } from 'path'
|
|
|
|
import { tmpdir } from 'os'
|
2022-09-06 13:29:25 +02:00
|
|
|
import { createWriteStream, promises as fs } from 'fs'
|
2020-04-06 16:46:04 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-10 12:55:46 +02:00
|
|
|
export async function isUrlOk(url: string): Promise<boolean> {
|
2020-07-09 17:52:49 +02:00
|
|
|
const res = await got.head(url).catch((e) => e)
|
2020-02-27 16:32:33 +01:00
|
|
|
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
|
|
|
|
2022-08-17 03:59:32 +02:00
|
|
|
if (
|
|
|
|
// Support repos whose entire purpose is to be a NextJS example, e.g.
|
|
|
|
// https://github.com/:username/:my-cool-nextjs-example-repo-name.
|
|
|
|
t === undefined ||
|
|
|
|
// Support GitHub URL that ends with a trailing slash, e.g.
|
|
|
|
// https://github.com/:username/:my-cool-nextjs-example-repo-name/
|
|
|
|
// In this case "t" will be an empty string while the next part "_branch" will be undefined
|
|
|
|
(t === '' && _branch === undefined)
|
|
|
|
) {
|
2020-04-06 15:57:19 +02:00
|
|
|
const infoResponse = await got(
|
|
|
|
`https://api.github.com/repos/${username}/${name}`
|
2020-05-18 21:24:37 +02:00
|
|
|
).catch((e) => e)
|
2020-04-06 15:57:19 +02:00
|
|
|
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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 23:51:47 +02:00
|
|
|
export function hasRepo({
|
|
|
|
username,
|
|
|
|
name,
|
|
|
|
branch,
|
|
|
|
filePath,
|
|
|
|
}: RepoInfo): Promise<boolean> {
|
2020-02-27 16:32:33 +01:00
|
|
|
const contentsUrl = `https://api.github.com/repos/${username}/${name}/contents`
|
|
|
|
const packagePath = `${filePath ? `/${filePath}` : ''}/package.json`
|
|
|
|
|
|
|
|
return isUrlOk(contentsUrl + packagePath + `?ref=${branch}`)
|
|
|
|
}
|
|
|
|
|
2022-06-23 19:28:14 +02:00
|
|
|
export function existsInRepo(nameOrUrl: string): Promise<boolean> {
|
|
|
|
try {
|
|
|
|
const url = new URL(nameOrUrl)
|
|
|
|
return isUrlOk(url.href)
|
|
|
|
} catch {
|
|
|
|
return isUrlOk(
|
|
|
|
`https://api.github.com/repos/vercel/next.js/contents/examples/${encodeURIComponent(
|
|
|
|
nameOrUrl
|
|
|
|
)}`
|
|
|
|
)
|
|
|
|
}
|
2020-02-27 16:32:33 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 18:11:12 +02:00
|
|
|
async function downloadTar(url: string) {
|
2022-09-06 13:29:25 +02:00
|
|
|
const tempFile = join(tmpdir(), `next.js-cna-example.temp-${Date.now()}`)
|
2022-09-05 18:11:12 +02:00
|
|
|
await pipeline(got.stream(url), createWriteStream(tempFile))
|
|
|
|
return tempFile
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function downloadAndExtractRepo(
|
2020-02-27 16:32:33 +01:00
|
|
|
root: string,
|
|
|
|
{ username, name, branch, filePath }: RepoInfo
|
2022-09-05 18:11:12 +02:00
|
|
|
) {
|
|
|
|
const tempFile = await downloadTar(
|
|
|
|
`https://codeload.github.com/${username}/${name}/tar.gz/${branch}`
|
2020-02-27 16:32:33 +01:00
|
|
|
)
|
2022-09-05 18:11:12 +02:00
|
|
|
|
|
|
|
await tar.x({
|
|
|
|
file: tempFile,
|
|
|
|
cwd: root,
|
|
|
|
strip: filePath ? filePath.split('/').length + 1 : 1,
|
|
|
|
filter: (p) =>
|
|
|
|
p.startsWith(
|
2022-11-22 01:51:57 +01:00
|
|
|
`${name}-${branch.replace(/\//g, '-')}${
|
|
|
|
filePath ? `/${filePath}/` : '/'
|
|
|
|
}`
|
2022-09-05 18:11:12 +02:00
|
|
|
),
|
|
|
|
})
|
2022-09-06 13:29:25 +02:00
|
|
|
|
|
|
|
await fs.unlink(tempFile)
|
2019-07-19 21:55:30 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 18:11:12 +02:00
|
|
|
export async function downloadAndExtractExample(root: string, name: string) {
|
2020-05-26 18:39:18 +02:00
|
|
|
if (name === '__internal-testing-retry') {
|
|
|
|
throw new Error('This is an internal example for testing the CLI.')
|
|
|
|
}
|
|
|
|
|
2022-09-05 18:11:12 +02:00
|
|
|
const tempFile = await downloadTar(
|
|
|
|
'https://codeload.github.com/vercel/next.js/tar.gz/canary'
|
2020-06-01 21:27:05 +02:00
|
|
|
)
|
2022-09-05 18:11:12 +02:00
|
|
|
|
|
|
|
await tar.x({
|
|
|
|
file: tempFile,
|
|
|
|
cwd: root,
|
|
|
|
strip: 3,
|
2022-09-13 22:51:44 +02:00
|
|
|
filter: (p) => p.includes(`next.js-canary/examples/${name}/`),
|
2022-09-05 18:11:12 +02:00
|
|
|
})
|
2022-09-06 13:29:25 +02:00
|
|
|
|
|
|
|
await fs.unlink(tempFile)
|
2019-07-19 21:55:30 +02:00
|
|
|
}
|