Unverified Commit 15f52f5c authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Add `--yes` option to `create-next-app` (#68085)

> **YES,**
> **WE**
> **CNA.**

This is a small improvement for power users that run `create-next-app` frequently, using the same preferences. By specifying the `--yes` option along with the project name, they won't be prompted for any preferences. Instead, the previously used preferences are automatically chosen, using the defaults as fallback.
parent eba4b314
...@@ -95,24 +95,24 @@ Options: ...@@ -95,24 +95,24 @@ Options:
--use-npm --use-npm
Explicitly tell the CLI to bootstrap the app using npm Explicitly tell the CLI to bootstrap the application using npm
--use-pnpm --use-pnpm
Explicitly tell the CLI to bootstrap the app using pnpm Explicitly tell the CLI to bootstrap the application using pnpm
--use-yarn --use-yarn
Explicitly tell the CLI to bootstrap the app using Yarn Explicitly tell the CLI to bootstrap the application using Yarn
--use-bun --use-bun
Explicitly tell the CLI to bootstrap the app using Bun Explicitly tell the CLI to bootstrap the application using Bun
-e, --example [name]|[github-url] -e, --example [name]|[github-url]
An example to bootstrap the app with. You can use an example name An example to bootstrap the app with. You can use an example name
from the official Next.js repo or a public GitHub URL. The URL can use from the official Next.js repo or a GitHub URL. The URL can use
any branch and/or subdirectory any branch and/or subdirectory
--example-path <path-to-example> --example-path <path-to-example>
...@@ -130,7 +130,12 @@ Options: ...@@ -130,7 +130,12 @@ Options:
Explicitly tell the CLI to skip installing packages Explicitly tell the CLI to skip installing packages
-h, --help output usage information --yes
Use previous preferences or defaults for all options that were not
explicitly specified, without prompting.
-h, --help display help for command
``` ```
### Why use Create Next App? ### Why use Create Next App?
......
...@@ -31,7 +31,7 @@ You can also pass command line arguments to set up a new project ...@@ -31,7 +31,7 @@ You can also pass command line arguments to set up a new project
non-interactively. See `create-next-app --help`: non-interactively. See `create-next-app --help`:
```bash ```bash
Usage: create-next-app <project-directory> [options] Usage: create-next-app [project-directory] [options]
Options: Options:
-V, --version output the version number -V, --version output the version number
...@@ -67,26 +67,30 @@ Options: ...@@ -67,26 +67,30 @@ Options:
Specify import alias to use (default "@/*"). Specify import alias to use (default "@/*").
--empty
Initialize an empty project.
--use-npm --use-npm
Explicitly tell the CLI to bootstrap the app using npm Explicitly tell the CLI to bootstrap the application using npm
--use-pnpm --use-pnpm
Explicitly tell the CLI to bootstrap the app using pnpm Explicitly tell the CLI to bootstrap the application using pnpm
--use-yarn --use-yarn
Explicitly tell the CLI to bootstrap the app using Yarn Explicitly tell the CLI to bootstrap the application using Yarn
--use-bun --use-bun
Explicitly tell the CLI to bootstrap the app using Bun Explicitly tell the CLI to bootstrap the application using Bun
-e, --example [name]|[github-url] -e, --example [name]|[github-url]
An example to bootstrap the app with. You can use an example name An example to bootstrap the app with. You can use an example name
from the official Next.js repo or a public GitHub URL. The URL can use from the official Next.js repo or a GitHub URL. The URL can use
any branch and/or subdirectory any branch and/or subdirectory
--example-path <path-to-example> --example-path <path-to-example>
...@@ -100,7 +104,16 @@ Options: ...@@ -100,7 +104,16 @@ Options:
Explicitly tell the CLI to reset any stored preferences Explicitly tell the CLI to reset any stored preferences
-h, --help output usage information --skip-install
Explicitly tell the CLI to skip installing packages
--yes
Use previous preferences or defaults for all options that were not
explicitly specified, without prompting.
-h, --help display help for command
``` ```
### Why use Create Next App? ### Why use Create Next App?
......
...@@ -73,7 +73,7 @@ const program = new Command(packageJson.name) ...@@ -73,7 +73,7 @@ const program = new Command(packageJson.name)
'--eslint', '--eslint',
` `
Initialize with eslint config. Initialize with ESLint config.
` `
) )
.option( .option(
...@@ -170,6 +170,14 @@ const program = new Command(packageJson.name) ...@@ -170,6 +170,14 @@ const program = new Command(packageJson.name)
` `
Explicitly tell the CLI to skip installing packages Explicitly tell the CLI to skip installing packages
`
)
.option(
'--yes',
`
Use previous preferences or defaults for all options that were not
explicitly specified, without prompting.
` `
) )
.allowUnknownOption() .allowUnknownOption()
...@@ -271,10 +279,13 @@ async function run(): Promise<void> { ...@@ -271,10 +279,13 @@ async function run(): Promise<void> {
string, string,
boolean | string boolean | string
> >
/** /**
* If the user does not provide the necessary flags, prompt them for whether * If the user does not provide the necessary flags, prompt them for their
* to use TS or JS. * preferences, unless `--yes` option was specified, or when running in CI.
*/ */
const skipPrompt = ciInfo.isCI || program.yes
if (!example) { if (!example) {
const defaults: typeof preferences = { const defaults: typeof preferences = {
typescript: true, typescript: true,
...@@ -291,7 +302,7 @@ async function run(): Promise<void> { ...@@ -291,7 +302,7 @@ async function run(): Promise<void> {
preferences[field] ?? defaults[field] preferences[field] ?? defaults[field]
if (!program.typescript && !program.javascript) { if (!program.typescript && !program.javascript) {
if (ciInfo.isCI) { if (skipPrompt) {
// default to TypeScript in CI as we can't prompt to // default to TypeScript in CI as we can't prompt to
// prevent breaking setup flows // prevent breaking setup flows
program.typescript = getPrefOrDefault('typescript') program.typescript = getPrefOrDefault('typescript')
...@@ -330,7 +341,7 @@ async function run(): Promise<void> { ...@@ -330,7 +341,7 @@ async function run(): Promise<void> {
!process.argv.includes('--eslint') && !process.argv.includes('--eslint') &&
!process.argv.includes('--no-eslint') !process.argv.includes('--no-eslint')
) { ) {
if (ciInfo.isCI) { if (skipPrompt) {
program.eslint = getPrefOrDefault('eslint') program.eslint = getPrefOrDefault('eslint')
} else { } else {
const styledEslint = blue('ESLint') const styledEslint = blue('ESLint')
...@@ -352,7 +363,7 @@ async function run(): Promise<void> { ...@@ -352,7 +363,7 @@ async function run(): Promise<void> {
!process.argv.includes('--tailwind') && !process.argv.includes('--tailwind') &&
!process.argv.includes('--no-tailwind') !process.argv.includes('--no-tailwind')
) { ) {
if (ciInfo.isCI) { if (skipPrompt) {
program.tailwind = getPrefOrDefault('tailwind') program.tailwind = getPrefOrDefault('tailwind')
} else { } else {
const tw = blue('Tailwind CSS') const tw = blue('Tailwind CSS')
...@@ -374,7 +385,7 @@ async function run(): Promise<void> { ...@@ -374,7 +385,7 @@ async function run(): Promise<void> {
!process.argv.includes('--src-dir') && !process.argv.includes('--src-dir') &&
!process.argv.includes('--no-src-dir') !process.argv.includes('--no-src-dir')
) { ) {
if (ciInfo.isCI) { if (skipPrompt) {
program.srcDir = getPrefOrDefault('srcDir') program.srcDir = getPrefOrDefault('srcDir')
} else { } else {
const styledSrcDir = blue('`src/` directory') const styledSrcDir = blue('`src/` directory')
...@@ -393,7 +404,7 @@ async function run(): Promise<void> { ...@@ -393,7 +404,7 @@ async function run(): Promise<void> {
} }
if (!process.argv.includes('--app') && !process.argv.includes('--no-app')) { if (!process.argv.includes('--app') && !process.argv.includes('--no-app')) {
if (ciInfo.isCI) { if (skipPrompt) {
program.app = getPrefOrDefault('app') program.app = getPrefOrDefault('app')
} else { } else {
const styledAppDir = blue('App Router') const styledAppDir = blue('App Router')
...@@ -407,11 +418,12 @@ async function run(): Promise<void> { ...@@ -407,11 +418,12 @@ async function run(): Promise<void> {
inactive: 'No', inactive: 'No',
}) })
program.app = Boolean(appRouter) program.app = Boolean(appRouter)
preferences.app = Boolean(appRouter)
} }
} }
if (!program.turbo && !process.argv.includes('--no-turbo')) { if (!program.turbo && !process.argv.includes('--no-turbo')) {
if (ciInfo.isCI) { if (skipPrompt) {
program.turbo = getPrefOrDefault('turbo') program.turbo = getPrefOrDefault('turbo')
} else { } else {
const styledTurbo = blue('Turbopack') const styledTurbo = blue('Turbopack')
...@@ -434,7 +446,7 @@ async function run(): Promise<void> { ...@@ -434,7 +446,7 @@ async function run(): Promise<void> {
typeof program.importAlias !== 'string' || typeof program.importAlias !== 'string' ||
!importAliasPattern.test(program.importAlias) !importAliasPattern.test(program.importAlias)
) { ) {
if (ciInfo.isCI) { if (skipPrompt) {
// We don't use preferences here because the default value is @/* regardless of existing preferences // We don't use preferences here because the default value is @/* regardless of existing preferences
program.importAlias = defaults.importAlias program.importAlias = defaults.importAlias
} else if (process.argv.includes('--no-import-alias')) { } else if (process.argv.includes('--no-import-alias')) {
......
...@@ -174,4 +174,46 @@ describe('create-next-app prompts', () => { ...@@ -174,4 +174,46 @@ describe('create-next-app prompts', () => {
`) `)
}) })
}) })
it('should not prompt user for choice and use defaults if --yes is defined', async () => {
await useTempDir(async (cwd) => {
const projectName = 'yes-we-can'
const childProcess = createNextApp(
[projectName, '--yes'],
{
cwd,
},
nextTgzFilename
)
await new Promise<void>((resolve) => {
childProcess.on('exit', async (exitCode) => {
expect(exitCode).toBe(0)
projectFilesShouldExist({
cwd,
projectName,
files: [
'app',
'.eslintrc.json',
'package.json',
'tailwind.config.ts',
'tsconfig.json',
],
})
resolve()
})
})
const pkg = require(join(cwd, projectName, 'package.json'))
expect(pkg.name).toBe(projectName)
const tsConfig = require(join(cwd, projectName, 'tsconfig.json'))
expect(tsConfig.compilerOptions.paths).toMatchInlineSnapshot(`
{
"@/*": [
"./*",
],
}
`)
})
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment