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 1 commit
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 @@ -20,3 +20,10 @@ export async 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)
}
50 changes: 50 additions & 0 deletions 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 Down Expand Up @@ -83,6 +84,55 @@ async function run() {
process.exit(1)
}

if (!program.example) {
const wantsRes = await prompts({
type: 'confirm',
name: 'wantsExample',
message: 'Would you like to create your an app from an example?',
initial: false,
})

if (wantsRes.wantsExample) {
const examplesJSON = await listExamples()
const options = examplesJSON.map((example: any) => {
return { 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.

// To solve this, we implement a basic fuzzy search here.
const fuzzyMatch = (pattern: string, str: string) => {
pattern = '.*' + pattern.split('').join('.*') + '.*'
const re = new RegExp(pattern)
return re.test(str)
}

const fuzzySuggest = (input: any, choices: any) =>
Promise.resolve(
choices.filter((choice: any) => fuzzyMatch(input, choice.title))
)

const nameRes = await prompts({
type: 'autocomplete',
name: 'exampleName',
message: 'Pick an example',
suggest: fuzzySuggest,
choices: options,
})

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

program.example = nameRes.exampleName
}
}

await createApp({
appPath: resolvedProjectPath,
useNpm: !!program.useNpm,
Expand Down