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

feat(create-gatsby): Add -y flag #28468

Merged
merged 7 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions packages/create-gatsby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ yarn create gatsby
It will ask you questions about what you're building, and set up a Gatsby project for you.

_Note: this package is different from the Gatsby CLI, it is intended solely to create new sites._

## Options

If you'd like to set up a minimal site without answering any prompts you can run the following command, including your chosen site directory.

```
npm init gatsby -y <site-directory>
```
44 changes: 35 additions & 9 deletions packages/create-gatsby/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ const makeChoices = (
export const validateProjectName = async (
value: string
): Promise<string | boolean> => {
if (!value) {
return `You have not provided a directory name for your site. Please do so when running with the 'y' flag.`
}
value = value.trim()
if (INVALID_FILENAMES.test(value)) {
return `The destination "${value}" is not a valid filename. Please try again, avoiding special characters.`
Expand All @@ -69,7 +72,7 @@ export const validateProjectName = async (

// The enquirer types are not accurate
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const questions = (initialFolderName: string): any => [
export const questions = (initialFolderName: string, skip: boolean): any => [
{
type: `textinput`,
name: `project`,
Expand All @@ -79,6 +82,7 @@ export const questions = (initialFolderName: string): any => [
initial: initialFolderName,
format: (value: string): string => c.cyan(value),
validate: validateProjectName,
skip,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we're still calling this question with an initial folder name to process the results but skipping it so that the user isn't prompted. Theoretically we could create the data object directly, but this seemed cleaner.

},
{
type: `selectinput`,
Expand Down Expand Up @@ -140,6 +144,13 @@ export type PluginConfigMap = Record<string, Record<string, unknown>>
const removeKey = (plugin: string): string => plugin.split(`:`)[0]

export async function run(): Promise<void> {
const [flag, siteDirectory] = process.argv.slice(2)

let yesFlag = false
if (flag === `-y`) {
yesFlag = true
}

trackCli(`CREATE_GATSBY_START`)

const { version } = require(`../package.json`)
Expand Down Expand Up @@ -171,15 +182,30 @@ ${center(c.blueBright.bold.underline(`Welcome to Gatsby!`))}

enquirer.use(plugin)

const { name: siteName } = await enquirer.prompt({
type: `textinput`,
name: `name`,
message: `What would you like to call your site?`,
initial: `My Gatsby Site`,
format: (value: string): string => c.cyan(value),
} as any)
let data
let siteName
if (!yesFlag) {
;({ name: siteName } = await enquirer.prompt({
laurieontech marked this conversation as resolved.
Show resolved Hide resolved
type: `textinput`,
name: `name`,
message: `What would you like to call your site?`,
initial: `My Gatsby Site`,
format: (value: string): string => c.cyan(value),
} as any))

data = await enquirer.prompt(questions(makeNpmSafe(siteName), yesFlag))
} else {
const warn = await validateProjectName(siteDirectory)
if (typeof warn === `string`) {
reporter.warn(warn)
return
}
siteName = siteDirectory
data = await enquirer.prompt(
questions(makeNpmSafe(siteDirectory), yesFlag)[0]
)
}

const data = await enquirer.prompt(questions(makeNpmSafe(siteName)))
data.project = data.project.trim()

trackCli(`CREATE_GATSBY_SELECT_OPTION`, {
Expand Down