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

Update default eslint choice for CNA setup #42371

Merged
merged 16 commits into from
Nov 3, 2022
64 changes: 35 additions & 29 deletions packages/create-next-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,53 +156,59 @@ async function run(): Promise<void> {
* If the user does not provide the necessary flags, prompt them for whether
* to use TS or JS.
*/
if (!example && !program.typescript && !program.javascript) {
if (!example) {
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
program.eslint = true
} else {
const styledTypeScript = chalk.hex('#007acc')('TypeScript')
const { typescript } = await prompts(
{
type: 'toggle',
name: 'typescript',
message: `Would you like to use ${styledTypeScript} with this project?`,
initial: true,
active: 'Yes',
inactive: 'No',
},
{
/**
* User inputs Ctrl+C or Ctrl+D to exit the prompt. We should close the
* process and not write to the file system.
*/
onCancel: () => {
console.error('Exiting.')
process.exit(1)
if (!program.typescript && !program.javascript) {
const styledTypeScript = chalk.hex('#007acc')('TypeScript')
const { typescript } = await prompts(
{
type: 'toggle',
name: 'typescript',
message: `Would you like to use ${styledTypeScript} with this project?`,
initial: true,
active: 'Yes',
inactive: 'No',
},
}
)
{
/**
* User inputs Ctrl+C or Ctrl+D to exit the prompt. We should close the
* process and not write to the file system.
*/
onCancel: () => {
console.error('Exiting.')
process.exit(1)
},
}
)

/**
* Depending on the prompt response, set the appropriate program flags.
*/
program.typescript = Boolean(typescript)
program.javascript = !Boolean(typescript)
}

if (!program.eslint) {
if (
!process.argv.includes('--eslint') &&
!process.argv.includes('--no-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,
initial: true,
active: 'Yes',
inactive: 'No',
})
program.eslint = Boolean(eslint)
}
/**
* Depending on the prompt response, set the appropriate program flags.
*/
program.typescript = Boolean(typescript)
program.javascript = !Boolean(typescript)
}
}

Expand Down
8 changes: 7 additions & 1 deletion test/integration/create-next-app/lib/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ export const projectSpecification: ProjectSpecification = {
js: {
deps: [],
devDeps: [],
files: ['app/page.jsx', 'app/layout.jsx', 'pages/api/hello.js'],
files: [
'app/page.jsx',
'app/head.jsx',
'app/layout.jsx',
'pages/api/hello.js',
],
},
ts: {
deps: ['@types/node', '@types/react', '@types/react-dom', 'typescript'],
devDeps: [],
files: [
'app/page.tsx',
'app/head.tsx',
'app/layout.tsx',
'pages/api/hello.ts',
'tsconfig.json',
Expand Down
39 changes: 36 additions & 3 deletions test/integration/create-next-app/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { ChildProcess, spawn, SpawnOptions } from 'child_process'
import { existsSync } from 'fs'
import { resolve } from 'path'
import glob from 'glob'

import { getProjectSetting, projectSpecification } from './specification'
import { CustomTemplateOptions, ProjectDeps, ProjectFiles } from './types'
Expand All @@ -18,7 +19,21 @@ const cli = require.resolve('create-next-app/dist/index.js')
*/
export const createNextApp = (args: string[], options?: SpawnOptions) => {
console.log(`[TEST] $ ${cli} ${args.join(' ')}`, { options })
return spawn('node', [cli].concat(args), options ?? {})
return spawn('node', [cli].concat(args), {
...options,
env: {
...process.env,
...options.env,
// unset CI env as this skips the auto-install behavior
// being tested
CI: '',
CIRCLECI: '',
GITHUB_ACTIONS: '',
CONTINUOUS_INTEGRATION: '',
RUN_ID: '',
BUILD_NUMBER: '',
},
})
}

/**
Expand Down Expand Up @@ -46,7 +61,16 @@ export const projectFilesShouldExist = ({
}: ProjectFiles) => {
const projectRoot = resolve(cwd, projectName)
for (const file of files) {
expect(existsSync(resolve(projectRoot, file))).toBe(true)
try {
expect(existsSync(resolve(projectRoot, file))).toBe(true)
} catch (err) {
require('console').error(
`missing expected file ${file}`,
glob.sync('**/*', { cwd, ignore: '**/node_modules/**' }),
files
)
throw err
}
}
}

Expand All @@ -57,7 +81,16 @@ export const projectFilesShouldNotExist = ({
}: ProjectFiles) => {
const projectRoot = resolve(cwd, projectName)
for (const file of files) {
expect(existsSync(resolve(projectRoot, file))).toBe(false)
try {
expect(existsSync(resolve(projectRoot, file))).toBe(false)
} catch (err) {
require('console').error(
`unexpected file present ${file}`,
glob.sync('**/*', { cwd, ignore: '**/node_modules/**' }),
files
)
throw err
}
}
}

Expand Down
29 changes: 16 additions & 13 deletions test/integration/create-next-app/templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {
createNextApp,
projectFilesShouldNotExist,
projectFilesShouldExist,
shouldBeJavascriptProject,
shouldBeTemplateProject,
shouldBeTypescriptProject,
Expand All @@ -33,21 +33,24 @@ describe('create-next-app templates', () => {
/**
* Bind the exit listener.
*/
childProcess.on('exit', (exitCode) => {
expect(exitCode).toBe(0)
await new Promise<void>((resolve) => {
childProcess.on('exit', (exitCode) => {
expect(exitCode).toBe(0)
/**
* Verify it correctly emitted a TS project by looking for tsconfig.
*/
projectFilesShouldExist({
cwd,
projectName,
files: ['tsconfig.json'],
})
resolve()
})
/**
* Verify it correctly emitted a TS project by looking for tsconfig.
* Simulate "Y" for TypeScript.
*/
projectFilesShouldNotExist({
cwd,
projectName,
files: ['tsconfig.json'],
})
childProcess.stdin.write('N\n')
})
/**
* Simulate "Y" for TypeScript.
*/
childProcess.stdin.write('N\n')
})
})

Expand Down