Unverified Commit 75559f14 authored by Luis Alvarez D's avatar Luis Alvarez D Committed by GitHub

Add --example=<github-url> to create-next-app (#10226)

* Initial support for URLs

* Added folder support

* Added --example-path

* Bug fix

* Also install deps

* Updated error message

* Bug fix and replace the file path

* Added tests
Co-authored-by: 's avatarShu Uesugi <shu@chibicode.com>
parent 0b88d17a
...@@ -5,7 +5,14 @@ import makeDir from 'make-dir' ...@@ -5,7 +5,14 @@ import makeDir from 'make-dir'
import os from 'os' import os from 'os'
import path from 'path' import path from 'path'
import { downloadAndExtractExample, hasExample } from './helpers/examples' import {
hasExample,
hasRepo,
getRepoInfo,
downloadAndExtractExample,
downloadAndExtractRepo,
RepoInfo,
} from './helpers/examples'
import { tryGitInit } from './helpers/git' import { tryGitInit } from './helpers/git'
import { install } from './helpers/install' import { install } from './helpers/install'
import { isFolderEmpty } from './helpers/is-folder-empty' import { isFolderEmpty } from './helpers/is-folder-empty'
...@@ -16,20 +23,69 @@ export async function createApp({ ...@@ -16,20 +23,69 @@ export async function createApp({
appPath, appPath,
useNpm, useNpm,
example, example,
examplePath,
}: { }: {
appPath: string appPath: string
useNpm: boolean useNpm: boolean
example?: string example?: string
examplePath?: string
}) { }) {
let repoInfo: RepoInfo | undefined
if (example) { if (example) {
const found = await hasExample(example) let repoUrl: URL | undefined
if (!found) {
console.error( try {
`Could not locate an example named ${chalk.red( repoUrl = new URL(example)
`"${example}"` } catch (error) {
)}. Please check your spelling and try again.` if (error.code !== 'ERR_INVALID_URL') {
) console.error(error)
process.exit(1) process.exit(1)
}
}
if (repoUrl) {
if (repoUrl.origin !== 'https://github.com') {
console.error(
`Invalid URL: ${chalk.red(
`"${example}"`
)}. Only GitHub repositories are supported. Please use a GitHub URL and try again.`
)
process.exit(1)
}
repoInfo = getRepoInfo(repoUrl, examplePath)
if (!repoInfo) {
console.error(
`Found invalid GitHub URL: ${chalk.red(
`"${example}"`
)}. Please fix the URL and try again.`
)
process.exit(1)
}
const found = await hasRepo(repoInfo)
if (!found) {
console.error(
`Could not locate the repository for ${chalk.red(
`"${example}"`
)}. Please check that the repository exists and try again.`
)
process.exit(1)
}
} else {
const found = await hasExample(example)
if (!found) {
console.error(
`Could not locate an example named ${chalk.red(
`"${example}"`
)}. Please check your spelling and try again.`
)
process.exit(1)
}
} }
} }
...@@ -53,13 +109,23 @@ export async function createApp({ ...@@ -53,13 +109,23 @@ export async function createApp({
process.chdir(root) process.chdir(root)
if (example) { if (example) {
console.log( if (repoInfo) {
`Downloading files for example ${chalk.cyan( console.log(
example `Downloading files from repo ${chalk.cyan(
)}. This might take a moment.` example
) )}. This might take a moment.`
console.log() )
await downloadAndExtractExample(root, example) console.log()
await downloadAndExtractRepo(root, repoInfo)
} else {
console.log(
`Downloading files for example ${chalk.cyan(
example
)}. This might take a moment.`
)
console.log()
await downloadAndExtractExample(root, example)
}
// Copy our default `.gitignore` if the application did not provide one // Copy our default `.gitignore` if the application did not provide one
const ignorePath = path.join(root, '.gitignore') const ignorePath = path.join(root, '.gitignore')
......
...@@ -2,20 +2,69 @@ import got from 'got' ...@@ -2,20 +2,69 @@ import got from 'got'
import promisePipe from 'promisepipe' import promisePipe from 'promisepipe'
import tar from 'tar' import tar from 'tar'
export async function hasExample(name: string): Promise<boolean> { export type RepoInfo = {
const res = await got( 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 function getRepoInfo(
url: URL,
examplePath?: string
): RepoInfo | undefined {
const [, username, name, t, _branch, ...file] = url.pathname.split('/')
const filePath = examplePath ? examplePath.replace(/^\//, '') : file.join('/')
// 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( `https://api.github.com/repos/zeit/next.js/contents/examples/${encodeURIComponent(
name name
)}/package.json` )}/package.json`
).catch(e => e) )
return res.statusCode === 200 }
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 async function downloadAndExtractExample( export function downloadAndExtractExample(
root: string, root: string,
name: string name: string
): Promise<void> { ): Promise<void> {
return await promisePipe( return promisePipe(
got.stream('https://codeload.github.com/zeit/next.js/tar.gz/canary'), got.stream('https://codeload.github.com/zeit/next.js/tar.gz/canary'),
tar.extract({ cwd: root, strip: 3 }, [`next.js-canary/examples/${name}`]) tar.extract({ cwd: root, strip: 3 }, [`next.js-canary/examples/${name}`])
) )
......
...@@ -21,8 +21,23 @@ const program = new Commander.Command(packageJson.name) ...@@ -21,8 +21,23 @@ const program = new Commander.Command(packageJson.name)
}) })
.option('--use-npm') .option('--use-npm')
.option( .option(
'-e, --example <example-path>', '-e, --example <name>|<github-url>',
'an example to bootstrap the app with' `
An example to bootstrap the app with. You can use an example name
from the official Next.js repo or a GitHub URL. The URL can use
any branch and/or subdirectory
`
)
.option(
'--example-path <path-to-example>',
`
In a rare case, your GitHub URL might contain a branch name with
a slash (e.g. bug/fix-1) and the path to the example (e.g. foo/bar).
In this case, you must specify the path to the example separately:
--example-path foo/bar
`
) )
.allowUnknownOption() .allowUnknownOption()
.parse(process.argv) .parse(process.argv)
...@@ -89,6 +104,7 @@ async function run() { ...@@ -89,6 +104,7 @@ async function run() {
example: example:
(typeof program.example === 'string' && program.example.trim()) || (typeof program.example === 'string' && program.example.trim()) ||
undefined, undefined,
examplePath: program.examplePath,
}) })
} }
......
...@@ -81,4 +81,77 @@ describe('create next app', () => { ...@@ -81,4 +81,77 @@ describe('create next app', () => {
fs.existsSync(path.join(cwd, projectName, '.gitignore')) fs.existsSync(path.join(cwd, projectName, '.gitignore'))
).toBeTruthy() ).toBeTruthy()
}) })
it('should allow example with GitHub URL', async () => {
const projectName = 'github-app'
const res = await run(
projectName,
'--example',
'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages'
)
expect(res.exitCode).toBe(0)
expect(
fs.existsSync(path.join(cwd, projectName, 'package.json'))
).toBeTruthy()
expect(
fs.existsSync(path.join(cwd, projectName, 'pages/index.js'))
).toBeTruthy()
expect(
fs.existsSync(path.join(cwd, projectName, 'pages/about.js'))
).toBeTruthy()
expect(
fs.existsSync(path.join(cwd, projectName, '.gitignore'))
).toBeTruthy()
})
it('should allow example with GitHub URL and example-path', async () => {
const projectName = 'github-example-path'
const res = await run(
projectName,
'--example',
'https://github.com/zeit/next-learn-demo/tree/master',
'--example-path',
'1-navigate-between-pages'
)
expect(res.exitCode).toBe(0)
expect(
fs.existsSync(path.join(cwd, projectName, 'package.json'))
).toBeTruthy()
expect(
fs.existsSync(path.join(cwd, projectName, 'pages/index.js'))
).toBeTruthy()
expect(
fs.existsSync(path.join(cwd, projectName, 'pages/about.js'))
).toBeTruthy()
expect(
fs.existsSync(path.join(cwd, projectName, '.gitignore'))
).toBeTruthy()
})
it('should use --example-path over the file path in the GitHub URL', async () => {
const projectName = 'github-example-path-2'
const res = await run(
projectName,
'--example',
'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages',
'--example-path',
'1-navigate-between-pages'
)
expect(res.exitCode).toBe(0)
expect(
fs.existsSync(path.join(cwd, projectName, 'package.json'))
).toBeTruthy()
expect(
fs.existsSync(path.join(cwd, projectName, 'pages/index.js'))
).toBeTruthy()
expect(
fs.existsSync(path.join(cwd, projectName, 'pages/about.js'))
).toBeTruthy()
expect(
fs.existsSync(path.join(cwd, projectName, '.gitignore'))
).toBeTruthy()
})
}) })
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