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(gatsby-cli): add GATSBY_EXPERIMENTAL_GATSBY_NEW_FLOW flag to use create-gatsby when no options are provided #27954

Merged
merged 12 commits into from Nov 13, 2020
Merged
12 changes: 1 addition & 11 deletions docs/docs/gatsby-cli.md
Expand Up @@ -54,17 +54,7 @@ gatsby new my-awesome-site
gatsby new my-awesome-blog-site https://github.com/gatsbyjs/gatsby-starter-blog
```

- If you leave out both of the arguments, the CLI will run an interactive shell asking for these inputs:

```shell
gatsby new
? What is your project called? › my-gatsby-project
? What starter would you like to use? › - Use arrow-keys. Return to submit.
❯ gatsby-starter-default
gatsby-starter-hello-world
gatsby-starter-blog
(Use a different starter)
```
- If you leave out both of the arguments, the CLI will run an interactive experience from the `create-gatsby` package that will prompt you with questions about the kind of site you're building, and install the plugins you'll need automatically.

See the [Gatsby starters docs](/docs/gatsby-starters/) for more details.

Expand Down
6 changes: 6 additions & 0 deletions integration-tests/gatsby-cli/__tests__/new.js
Expand Up @@ -78,4 +78,10 @@ describe(`gatsby new`, () => {

expect(code).toBe(1)
})

it(`an empty gatsby new (without args) runs create-gatsby`, () => {
gillkyle marked this conversation as resolved.
Show resolved Hide resolved
const [_, logs] = GatsbyCLI.from(cwd).invoke([`new`])

logs.should.contain(`Welcome to Gatsby!`)
})
})
1 change: 1 addition & 0 deletions packages/gatsby-cli/package.json
Expand Up @@ -19,6 +19,7 @@
"common-tags": "^1.8.0",
"configstore": "^5.0.1",
"convert-hrtime": "^3.0.0",
"create-gatsby": "0.0.0-6",
"envinfo": "^7.7.3",
"execa": "^3.4.0",
"fs-exists-cached": "^1.0.0",
Expand Down
15 changes: 10 additions & 5 deletions packages/gatsby-cli/src/create-cli.ts
@@ -1,9 +1,6 @@
import path from "path"
import resolveCwd from "resolve-cwd"
import yargs from "yargs"
import report from "./reporter"
import { setStore } from "./reporter/redux"
import { getLocalGatsbyVersion } from "./util/version"
import envinfo from "envinfo"
import { sync as existsSync } from "fs-exists-cached"
import clipboardy from "clipboardy"
Expand All @@ -13,9 +10,13 @@ import {
setTelemetryEnabled,
isTrackingEnabled,
} from "gatsby-telemetry"
import { startGraphQLServer } from "gatsby-recipes"
import { run as runCreateGatsby } from "create-gatsby"
import report from "./reporter"
import { setStore } from "./reporter/redux"
import { getLocalGatsbyVersion } from "./util/version"
import { initStarter } from "./init-starter"
import { recipesHandler } from "./recipes"
import { startGraphQLServer } from "gatsby-recipes"
import { getPackageManager, setPackageManager } from "./util/package-manager"
import reporter from "./reporter"

Expand Down Expand Up @@ -504,7 +505,11 @@ export const createCli = (argv: Array<string>): yargs.Arguments => {
const starterStr = starter ? String(starter) : undefined
const rootPathStr = rootPath ? String(rootPath) : undefined

await initStarter(starterStr, rootPathStr)
if (!starterStr && !rootPathStr) {
await runCreateGatsby()
} else {
await initStarter(starterStr, rootPathStr)
}
}),
})
.command({
Expand Down
56 changes: 2 additions & 54 deletions packages/gatsby-cli/src/init-starter.ts
@@ -1,4 +1,3 @@
import opn from "better-opn"
import { execSync } from "child_process"
import execa from "execa"
import { sync as existsSync } from "fs-exists-cached"
Expand All @@ -7,7 +6,6 @@ import { trackCli, trackError } from "gatsby-telemetry"
import hostedGitInfo from "hosted-git-info"
import isValid from "is-valid-path"
import sysPath from "path"
import prompts from "prompts"
import url from "url"
import { updateSiteMetadata } from "gatsby-core-utils"
import report from "./reporter"
Expand Down Expand Up @@ -208,57 +206,17 @@ const clone = async (
interface IGetPaths {
starterPath: string
rootPath: string
selectedOtherStarter: boolean
}

const getPaths = async (
starterPath?: string,
rootPath?: string
): Promise<IGetPaths> => {
let selectedOtherStarter = false

// if no args are passed, prompt user for path and starter
if (!starterPath && !rootPath) {
const response = await prompts.prompt([
{
type: `text`,
name: `path`,
message: `What is your project called?`,
initial: `my-gatsby-project`,
},
{
type: `select`,
name: `starter`,
message: `What starter would you like to use?`,
choices: [
{ title: `gatsby-starter-default`, value: `gatsby-starter-default` },
{
title: `gatsby-starter-hello-world`,
value: `gatsby-starter-hello-world`,
},
{ title: `gatsby-starter-blog`, value: `gatsby-starter-blog` },
{ title: `(Use a different starter)`, value: `different` },
],
initial: 0,
},
])
// exit gracefully if responses aren't provided
if (!response.starter || !response.path.trim()) {
throw new Error(
`Please mention both starter package and project name along with path(if its not in the root)`
)
}

selectedOtherStarter = response.starter === `different`
starterPath = `gatsbyjs/${response.starter}`
rootPath = response.path
}

// set defaults if no root or starter has been set yet
rootPath = rootPath || process.cwd()
starterPath = starterPath || `gatsbyjs/gatsby-starter-default`

return { starterPath, rootPath, selectedOtherStarter }
return { starterPath, rootPath }
}

const successMessage = (path: string): void => {
Expand All @@ -277,20 +235,10 @@ export async function initStarter(
starter?: string,
root?: string
): Promise<void> {
const { starterPath, rootPath, selectedOtherStarter } = await getPaths(
starter,
root
)
const { starterPath, rootPath } = await getPaths(starter, root)

const urlObject = url.parse(rootPath)

if (selectedOtherStarter) {
report.info(
`Opening the starter library at https://gatsby.dev/starters?v=2...\nThe starter library has a variety of options for starters you can browse\n\nYou can then use the gatsby new command with the link to a repository of a starter you'd like to use, for example:\ngatsby new ${rootPath} https://github.com/gatsbyjs/gatsby-starter-default`
)
opn(`https://gatsby.dev/starters?v=2`)
return
}
if (urlObject.protocol && urlObject.host) {
trackError(`NEW_PROJECT_NAME_MISSING`)

Expand Down