From d60023883a0e799764c7497671fe8adfb23c40ac Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Tue, 2 May 2023 21:01:23 +0200 Subject: [PATCH] Bring back changes to customizeAlias from #49063 --- packages/create-next-app/index.ts | 36 +++++++++++++------ .../create-next-app/templates.test.ts | 18 ++++++++-- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/packages/create-next-app/index.ts b/packages/create-next-app/index.ts index b991836c17da8..253a7a39d4dac 100644 --- a/packages/create-next-app/index.ts +++ b/packages/create-next-app/index.ts @@ -225,6 +225,7 @@ async function run(): Promise { tailwind: true, srcDir: false, importAlias: '@/*', + customizeImportAlias: false, } const getPrefOrDefault = (field: string) => preferences[field] ?? defaults[field] @@ -340,19 +341,32 @@ async function run(): Promise { program.importAlias = '@/*' } else { const styledImportAlias = chalk.hex('#007acc')('import alias') - const { importAlias } = await prompts({ + const { customizeImportAlias } = await prompts({ onState: onPromptState, - type: 'text', - name: 'importAlias', - message: `What ${styledImportAlias} would you like configured?`, - initial: getPrefOrDefault('importAlias'), - validate: (value) => - /.+\/\*/.test(value) - ? true - : 'Import alias must follow the pattern /*', + type: 'toggle', + name: 'customizeImportAlias', + message: `Would you like to customize the default ${styledImportAlias}?`, + initial: getPrefOrDefault('customizeImportAlias'), + active: 'Yes', + inactive: 'No', }) - program.importAlias = importAlias - preferences.importAlias = importAlias + if (!customizeImportAlias) { + program.importAlias = '@/*' + } else { + const { importAlias } = await prompts({ + onState: onPromptState, + type: 'text', + name: 'importAlias', + message: `What ${styledImportAlias} would you like configured?`, + initial: getPrefOrDefault('importAlias'), + validate: (value) => + /.+\/\*/.test(value) + ? true + : 'Import alias must follow the pattern /*', + }) + program.importAlias = importAlias + preferences.importAlias = importAlias + } } } } diff --git a/test/integration/create-next-app/templates.test.ts b/test/integration/create-next-app/templates.test.ts index 6cf9f0d48d441..7d1e71c60a94f 100644 --- a/test/integration/create-next-app/templates.test.ts +++ b/test/integration/create-next-app/templates.test.ts @@ -19,9 +19,16 @@ import { import { Span } from 'next/dist/trace' import { useTempDir } from '../../../test/lib/use-temp-dir' -import { fetchViaHTTP, findPort, killApp, launchApp } from 'next-test-utils' +import { + check, + fetchViaHTTP, + findPort, + killApp, + launchApp, +} from 'next-test-utils' import resolveFrom from 'resolve-from' import { createNextInstall } from '../../../test/lib/create-next-install' +import ansiEscapes from 'ansi-escapes' const startsWithoutError = async ( appDir: string, @@ -272,11 +279,18 @@ describe('create-next-app templates', () => { /** * Bind the exit listener. */ - await new Promise((resolve) => { + await new Promise(async (resolve) => { childProcess.on('exit', async (exitCode) => { expect(exitCode).toBe(0) resolve() }) + let output = '' + childProcess.stdout.on('data', (data) => { + output += data + process.stdout.write(data) + }) + childProcess.stdin.write(ansiEscapes.cursorForward() + '\n') + await check(() => output, /What import alias would you like configured/) childProcess.stdin.write('@/something/*\n') })