Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirm if user wants example when creating app #10543

Merged
merged 17 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/create-next-app/helpers/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ export function downloadAndExtractExample(
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'
).catch(e => e)
Timer marked this conversation as resolved.
Show resolved Hide resolved
return JSON.parse(res.body)
}
48 changes: 47 additions & 1 deletion packages/create-next-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createApp } from './create-app'
import { validateNpmName } from './helpers/validate-pkg'
import packageJson from './package.json'
import { shouldUseYarn } from './helpers/should-use-yarn'
import { listExamples } from './helpers/examples'

let projectPath: string = ''

Expand All @@ -21,7 +22,7 @@ const program = new Commander.Command(packageJson.name)
})
.option('--use-npm')
.option(
'-e, --example <name>|<github-url>',
'-e, --example [name]|[github-url]',
`

An example to bootstrap the app with. You can use an example name
Expand Down Expand Up @@ -98,6 +99,51 @@ async function run() {
process.exit(1)
}

// --example flag included, but no example path provided, i.e.:
// $ create-next-app --example
if (program.example && typeof program.example !== 'string') {
const wantsRes = await prompts({
type: 'confirm',
name: 'wantsExample',
message:
'You forgot to select an example, would you like to pick one now?',
initial: false,
})

if (wantsRes.wantsExample) {
const examplesJSON = await listExamples()
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) =>
choices.filter((choice: any) => choice.title.includes(input)),
})

if (!nameRes.exampleName) {
console.error(
'Could not locate an example with that name. Creating project from blank starter instead.'
)
}

program.example = nameRes.exampleName

// If the user says 'no' to choosing from examples, they are warned that the default
// template is being used.
} else {
console.warn(chalk.yellow('Creating project from blank starter instead.'))
}
}

await createApp({
appPath: resolvedProjectPath,
useNpm: !!program.useNpm,
Expand Down
28 changes: 28 additions & 0 deletions test/integration/create-next-app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,32 @@ describe('create next app', () => {
fs.existsSync(path.join(cwd, projectName, '.gitignore'))
).toBeTruthy()
})

it('Should ask for an example', async () => {
const question = async (...args) => {
return new Promise((resolve, reject) => {
const res = run(...args)

let timeout = setTimeout(() => {
if (!res.killed) {
res.kill()
reject(new Error('Missing request to select example name'))
}
}, 2000)

res.stdout.on('data', data => {
const stdout = data.toString()
if (stdout.includes('y/N')) {
res.kill()
clearTimeout(timeout)
resolve(stdout)
}
})
})
}

expect(await question('no-example', '--example')).toMatch(
/You forgot to select an example, would you like to pick one now/
)
})
})