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

Add prompt for ESLint to CNA #42218

Merged
merged 1 commit into from
Oct 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/api-reference/create-next-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ Options:

Initialize as a JavaScript project.

--eslint

Initialize with eslint config.

--no-eslint

Initialize without eslint config.

--experimental-app

Initialize as a `app/` directory project.

--use-npm

Explicitly tell the CLI to bootstrap the app using npm
Expand Down
3 changes: 3 additions & 0 deletions packages/create-next-app/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ export async function createApp({
example,
examplePath,
typescript,
eslint,
experimentalApp,
}: {
appPath: string
packageManager: PackageManager
example?: string
examplePath?: string
typescript: boolean
eslint: boolean
experimentalApp: boolean
}): Promise<void> {
let repoInfo: RepoInfo | undefined
Expand Down Expand Up @@ -216,6 +218,7 @@ export async function createApp({
mode,
packageManager,
isOnline,
eslint,
})
}

Expand Down
26 changes: 23 additions & 3 deletions packages/create-next-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const program = new Commander.Command(packageJson.name)
`

Initialize as a JavaScript project.
`
)
.option(
'--eslint',
`

Initialize with eslint config.
`
)
.option(
Expand Down Expand Up @@ -148,16 +155,14 @@ async function run(): Promise<void> {
/**
* If the user does not provide the necessary flags, prompt them for whether
* to use TS or JS.
*
* @todo Allow appDir to support TS or JS, currently TS-only and disables all
* --ts, --js features.
*/
if (!example && !program.typescript && !program.javascript) {
if (ciInfo.isCI) {
// default to JavaScript in CI as we can't prompt to
// prevent breaking setup flows
program.javascript = true
program.typescript = false
program.eslint = false
} else {
const styledTypeScript = chalk.hex('#007acc')('TypeScript')
const { typescript } = await prompts(
Expand All @@ -180,6 +185,19 @@ async function run(): Promise<void> {
},
}
)

if (!program.eslint) {
const styledEslint = chalk.hex('#007acc')('ESLint')
const { eslint } = await prompts({
type: 'toggle',
name: 'eslint',
message: `Would you like to use ${styledEslint} with this project?`,
initial: false,
active: 'Yes',
inactive: 'No',
})
program.eslint = Boolean(eslint)
}
/**
* Depending on the prompt response, set the appropriate program flags.
*/
Expand All @@ -195,6 +213,7 @@ async function run(): Promise<void> {
example: example && example !== 'default' ? example : undefined,
examplePath: program.examplePath,
typescript: program.typescript,
eslint: program.eslint,
experimentalApp: program.experimentalApp,
})
} catch (reason) {
Expand All @@ -218,6 +237,7 @@ async function run(): Promise<void> {
appPath: resolvedProjectPath,
packageManager,
typescript: program.typescript,
eslint: program.eslint,
experimentalApp: program.experimentalApp,
})
}
Expand Down
35 changes: 15 additions & 20 deletions packages/create-next-app/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const installTemplate = async ({
isOnline,
template,
mode,
eslint,
}: InstallTemplateArgs) => {
console.log(chalk.bold(`Using ${packageManager}.`))

Expand Down Expand Up @@ -62,21 +63,24 @@ export const installTemplate = async ({
* Default dependencies.
*/
const dependencies = ['react', 'react-dom', 'next']
/**
* Default devDependencies.
*/
const devDependencies = ['eslint', 'eslint-config-next']
/**
* TypeScript projects will have type definitions and other devDependencies.
*/
if (mode === 'ts') {
devDependencies.push(
dependencies.push(
'typescript',
'@types/react',
'@types/node',
'@types/react-dom'
)
}

/**
* Default eslint dependencies.
*/
if (eslint) {
dependencies.push('eslint', 'eslint-config-next')
}
/**
* Install package.json dependencies if they exist.
*/
Expand All @@ -90,24 +94,10 @@ export const installTemplate = async ({

await install(root, dependencies, installFlags)
}
/**
* Install package.json devDependencies if they exist.
*/
if (devDependencies.length) {
console.log()
console.log('Installing devDependencies:')
for (const devDependency of devDependencies) {
console.log(`- ${chalk.cyan(devDependency)}`)
}
console.log()

const devInstallFlags = { devDependencies: true, ...installFlags }
await install(root, devDependencies, devInstallFlags)
}
/**
* Copy the template files to the target directory.
*/
console.log('\nInitializing project with template: ', template, '\n')
console.log('\nInitializing project with template:', template, '\n')
const templatePath = path.join(__dirname, template, mode)
await cpy('**', root, {
parents: true,
Expand All @@ -129,6 +119,11 @@ export const installTemplate = async ({
}
},
})

if (!eslint) {
// remove un-necessary template file if eslint is not desired
await fs.promises.unlink(path.join(root, '.eslintrc.json'))
}
}

export * from './types'
1 change: 1 addition & 0 deletions packages/create-next-app/templates/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface InstallTemplateArgs {

template: TemplateType
mode: TemplateMode
eslint: boolean
}