Skip to content

Commit 5ffb6dd

Browse files
authored
feat(create-gatsby): Prompt for site name (#28401)
* feat(create-gatsby): Prompt for site name * Move kebabify into utils
1 parent eafbbab commit 5ffb6dd

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

packages/create-gatsby/src/index.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import { trackCli } from "./tracking"
1515
import crypto from "crypto"
1616
import { reporter } from "./reporter"
1717
import { setSiteMetadata } from "./site-metadata"
18+
import { kebabify } from "./utils"
1819

1920
const sha256 = (str: string): string =>
2021
crypto.createHash(`sha256`).update(str).digest(`hex`)
2122

2223
const md5 = (str: string): string =>
2324
crypto.createHash(`md5`).update(str).digest(`hex`)
25+
2426
/**
2527
* Hide string on windows (for emojis)
2628
*/
@@ -67,14 +69,14 @@ export const validateProjectName = async (
6769

6870
// The enquirer types are not accurate
6971
// eslint-disable-next-line @typescript-eslint/no-explicit-any
70-
export const questions: any = [
72+
export const questions = (initialFolderName: string): any => [
7173
{
7274
type: `textinput`,
7375
name: `project`,
7476
message: `What would you like to name the folder where your site will be created?`,
7577
hint: path.basename(process.cwd()),
7678
separator: `/`,
77-
initial: `my-gatsby-site`,
79+
initial: initialFolderName,
7880
format: (value: string): string => c.cyan(value),
7981
validate: validateProjectName,
8082
},
@@ -101,6 +103,7 @@ export const questions: any = [
101103
},
102104
]
103105
interface IAnswers {
106+
name: string
104107
project: string
105108
styling?: keyof typeof styles
106109
cms?: keyof typeof cmses
@@ -168,7 +171,15 @@ ${center(c.blueBright.bold.underline(`Welcome to Gatsby!`))}
168171

169172
enquirer.use(plugin)
170173

171-
const data = await enquirer.prompt(questions)
174+
const { name: siteName } = await enquirer.prompt({
175+
type: `textinput`,
176+
name: `name`,
177+
message: `What would you like to call your site?`,
178+
initial: `My Gatsby Site`,
179+
format: (value: string): string => c.cyan(value),
180+
} as any)
181+
182+
const data = await enquirer.prompt(questions(kebabify(siteName)))
172183
data.project = data.project.trim()
173184

174185
trackCli(`CREATE_GATSBY_SELECT_OPTION`, {
@@ -298,7 +309,12 @@ ${c.bold(`Thanks! Here's what we'll now do:`)}
298309
return
299310
}
300311

301-
await initStarter(DEFAULT_STARTER, data.project, packages.map(removeKey))
312+
await initStarter(
313+
DEFAULT_STARTER,
314+
data.project,
315+
packages.map(removeKey),
316+
siteName
317+
)
302318

303319
reporter.success(`Created site in ${c.green(data.project)}`)
304320

@@ -308,7 +324,7 @@ ${c.bold(`Thanks! Here's what we'll now do:`)}
308324
reporter.info(`${w(`🔌 `)}Setting-up plugins...`)
309325
await installPlugins(plugins, pluginConfig, fullPath, [])
310326
}
311-
await setSiteMetadata(fullPath, `title`, data.project)
327+
await setSiteMetadata(fullPath, `title`, siteName)
312328

313329
await gitSetup(data.project)
314330

@@ -318,7 +334,7 @@ ${c.bold(`Thanks! Here's what we'll now do:`)}
318334
reporter.info(
319335
stripIndent`
320336
${w(`🎉 `)}Your new Gatsby site ${c.bold(
321-
data.project
337+
siteName
322338
)} has been successfully created
323339
at ${c.bold(fullPath)}.
324340
`

packages/create-gatsby/src/init-starter.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ import { spin } from "tiny-spin"
77
import { getConfigStore } from "./get-config-store"
88
type PackageManager = "yarn" | "npm"
99
import c from "ansi-colors"
10-
import { clearLine } from "./utils"
10+
import { clearLine, kebabify } from "./utils"
1111

1212
const packageManagerConfigKey = `cli.packageManager`
1313

14-
const kebabify = (str: string): string =>
15-
str
16-
.replace(/([a-z])([A-Z])/g, `$1-$2`)
17-
.replace(/[^a-zA-Z]+/g, `-`)
18-
.toLowerCase()
19-
2014
export const getPackageManager = (
2115
npmConfigUserAgent?: string
2216
): PackageManager => {
@@ -92,7 +86,7 @@ const setNameInPackage = async (
9286
const packageJsonPath = path.join(sitePath, `package.json`)
9387
const packageJson = await fs.readJSON(packageJsonPath)
9488
packageJson.name = kebabify(name)
95-
packageJson.description = `My Gatsby site`
89+
packageJson.description = name
9690
try {
9791
const result = await execa(`git`, [`config`, `user.name`])
9892
if (result.failed) {
@@ -198,13 +192,14 @@ export async function gitSetup(rootPath: string): Promise<void> {
198192
export async function initStarter(
199193
starter: string,
200194
rootPath: string,
201-
packages: Array<string>
195+
packages: Array<string>,
196+
siteName: string
202197
): Promise<void> {
203198
const sitePath = path.resolve(rootPath)
204199

205200
await clone(starter, sitePath)
206201

207-
await setNameInPackage(sitePath, rootPath)
202+
await setNameInPackage(sitePath, siteName)
208203

209204
await install(rootPath, packages)
210205

packages/create-gatsby/src/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ export const clearLine = (count = 1): Promise<boolean> =>
1010
resolve()
1111
})
1212
})
13+
14+
export const kebabify = (str: string): string =>
15+
str
16+
.replace(/([a-z])([A-Z])/g, `$1-$2`)
17+
.replace(/[^a-zA-Z]+/g, `-`)
18+
.toLowerCase()

0 commit comments

Comments
 (0)