Unverified Commit bc7d1836 authored by Dylan Brookes's avatar Dylan Brookes Committed by GitHub

Confirm if user wants example when creating app (#10543)

* Confirm if user wants example when creating app

* Only ask user to choose example if they provide --example flag

* Simplify the search

* Added test

* Use a select to pick the template

* Updated test

* check for lowercase and updated no example message

* Add message if no template is selected

* Updated tests

* Handle error if fetching the examples fails

* Fixed tests

* Updated test timeout

* Update examples.ts
Co-authored-by: 's avatarmerelinguist <merelinguist@users.noreply.github.com>
Co-authored-by: 's avatarLuis Alvarez <luis@zeit.co>
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
Co-authored-by: 's avatarJoe Haddad <joe.haddad@zeit.co>
parent bf184fce
...@@ -86,3 +86,10 @@ export function downloadAndExtractExample( ...@@ -86,3 +86,10 @@ export function downloadAndExtractExample(
tar.extract({ cwd: root, strip: 3 }, [`next.js-canary/examples/${name}`]) tar.extract({ cwd: root, strip: 3 }, [`next.js-canary/examples/${name}`])
) )
} }
export async function listExamples(): Promise<any> {
const res = await got(
'https://api.github.com/repositories/70107786/contents/examples'
)
return JSON.parse(res.body)
}
...@@ -9,6 +9,7 @@ import { createApp } from './create-app' ...@@ -9,6 +9,7 @@ import { createApp } from './create-app'
import { validateNpmName } from './helpers/validate-pkg' import { validateNpmName } from './helpers/validate-pkg'
import packageJson from './package.json' import packageJson from './package.json'
import { shouldUseYarn } from './helpers/should-use-yarn' import { shouldUseYarn } from './helpers/should-use-yarn'
import { listExamples } from './helpers/examples'
let projectPath: string = '' let projectPath: string = ''
...@@ -21,7 +22,7 @@ const program = new Commander.Command(packageJson.name) ...@@ -21,7 +22,7 @@ const program = new Commander.Command(packageJson.name)
}) })
.option('--use-npm') .option('--use-npm')
.option( .option(
'-e, --example <name>|<github-url>', '-e, --example [name]|[github-url]',
` `
An example to bootstrap the app with. You can use an example name An example to bootstrap the app with. You can use an example name
...@@ -98,6 +99,72 @@ async function run() { ...@@ -98,6 +99,72 @@ async function run() {
process.exit(1) process.exit(1)
} }
if (!program.example) {
const template = await prompts({
type: 'select',
name: 'value',
message: 'Pick a template',
choices: [
{ title: 'Default starter app', value: 'default' },
{ title: 'Example from the Next.js repo', value: 'example' },
],
})
if (!template.value) {
console.log()
console.log('Please specify the template')
process.exit(1)
}
if (template.value === 'example') {
let examplesJSON: any
try {
examplesJSON = await listExamples()
} catch (error) {
console.log()
console.log(
'Failed to fetch the list of examples with the following error:'
)
console.error(error)
console.log()
console.log('Switching to the default starter app')
console.log()
}
if (examplesJSON) {
const choices = examplesJSON.map((example: any) => ({
title: example.name,
value: example.name,
}))
// The search function built into `prompts` isn’t very helpful:
// someone searching for `styled-components` would get no results since
// the example is called `with-styled-components`, and `prompts` searches
// the beginnings of titles.
const nameRes = await prompts({
type: 'autocomplete',
name: 'exampleName',
message: 'Pick an example',
choices,
suggest: (input: any, choices: any) => {
const regex = new RegExp(input, 'i')
return choices.filter((choice: any) => regex.test(choice.title))
},
})
if (!nameRes.exampleName) {
console.log()
console.log(
'Please specify an example or use the default starter app.'
)
process.exit(1)
}
program.example = nameRes.exampleName
}
}
}
await createApp({ await createApp({
appPath: resolvedProjectPath, appPath: resolvedProjectPath,
useNpm: !!program.useNpm, useNpm: !!program.useNpm,
...@@ -130,6 +197,7 @@ async function notifyUpdate() { ...@@ -130,6 +197,7 @@ async function notifyUpdate() {
) )
console.log() console.log()
} }
process.exit()
} catch { } catch {
// ignore error // ignore error
} }
......
...@@ -13,23 +13,35 @@ const cwd = path.join( ...@@ -13,23 +13,35 @@ const cwd = path.join(
) )
const run = (...args) => execa('node', [cli, ...args], { cwd }) const run = (...args) => execa('node', [cli, ...args], { cwd })
const runStarter = (...args) => {
const res = run(...args)
res.stdout.on('data', data => {
const stdout = data.toString()
if (/Pick a template/.test(stdout)) {
res.stdin.write('\n')
}
})
return res
}
describe('create next app', () => { describe('create next app', () => {
beforeAll(async () => { beforeAll(async () => {
jest.setTimeout(1000 * 60) jest.setTimeout(1000 * 60 * 2)
await fs.mkdirp(cwd) await fs.mkdirp(cwd)
}) })
it('non-empty directory', async () => { it('non-empty directory', async () => {
const projectName = 'non-empty-directory' const projectName = 'non-empty-directory'
await fs.mkdirp(path.join(cwd, projectName)) await fs.mkdirp(path.join(cwd, projectName))
const pkg = path.join(cwd, projectName, 'package.json') const pkg = path.join(cwd, projectName, 'package.json')
fs.writeFileSync(pkg, '{ "foo": "bar" }') fs.writeFileSync(pkg, '{ "foo": "bar" }')
expect.assertions(1) expect.assertions(1)
try { try {
await run(projectName) await runStarter(projectName)
} catch (e) { } catch (e) {
expect(e.stdout).toMatch(/contains files that could conflict/) expect(e.stdout).toMatch(/contains files that could conflict/)
} }
...@@ -37,7 +49,7 @@ describe('create next app', () => { ...@@ -37,7 +49,7 @@ describe('create next app', () => {
it('empty directory', async () => { it('empty directory', async () => {
const projectName = 'empty-directory' const projectName = 'empty-directory'
const res = await run(projectName) const res = await runStarter(projectName)
expect(res.exitCode).toBe(0) expect(res.exitCode).toBe(0)
expect( expect(
...@@ -150,4 +162,42 @@ describe('create next app', () => { ...@@ -150,4 +162,42 @@ describe('create next app', () => {
fs.existsSync(path.join(cwd, projectName, '.gitignore')) fs.existsSync(path.join(cwd, projectName, '.gitignore'))
).toBeTruthy() ).toBeTruthy()
}) })
it('should allow to manually select an example', async () => {
const runExample = (...args) => {
const res = run(...args)
function pickExample(data) {
if (/hello-world/.test(data.toString())) {
res.stdout.removeListener('data', pickExample)
res.stdin.write('\n')
}
}
function searchExample(data) {
if (/Pick an example/.test(data.toString())) {
res.stdout.removeListener('data', searchExample)
res.stdin.write('hello-world')
res.stdout.on('data', pickExample)
}
}
function selectExample(data) {
if (/Pick a template/.test(data.toString())) {
res.stdout.removeListener('data', selectExample)
res.stdin.write('\u001b[B\n') // Down key and enter
res.stdout.on('data', searchExample)
}
}
res.stdout.on('data', selectExample)
return res
}
const res = await runExample('no-example')
expect(res.exitCode).toBe(0)
expect(res.stdout).toMatch(/Downloading files for example hello-world/)
})
}) })
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