diff --git a/packages/cypress/src/generators/configuration/configuration.ts b/packages/cypress/src/generators/configuration/configuration.ts index 3671a480b416a..b42c025bae0cc 100644 --- a/packages/cypress/src/generators/configuration/configuration.ts +++ b/packages/cypress/src/generators/configuration/configuration.ts @@ -39,6 +39,7 @@ import { installedCypressVersion } from '../../utils/cypress-version'; import { typesNodeVersion, viteVersion } from '../../utils/versions'; import { addBaseCypressSetup } from '../base-setup/base-setup'; import cypressInitGenerator, { addPlugin } from '../init/init'; +import { promptWhenInteractive } from '@nx/devkit/src/generators/prompt'; export interface CypressE2EConfigSchema { project: string; @@ -194,6 +195,18 @@ async function normalizeOptions(tree: Tree, options: CypressE2EConfigSchema) { Rename or remove the existing e2e target.`); } + if ( + !options.baseUrl && + !options.devServerTarget && + !projectConfig?.targets?.serve + ) { + const { devServerTarget, baseUrl } = await promptForMissingServeData( + options.project + ); + options.devServerTarget = devServerTarget; + options.baseUrl = baseUrl; + } + if ( !options.baseUrl && !options.devServerTarget && @@ -228,6 +241,38 @@ In this case you need to provide a devServerTarget,':[: }; } +async function promptForMissingServeData(projectName: string) { + const { devServerTarget, port } = await promptWhenInteractive<{ + devServerTarget: string; + port: number; + }>( + [ + { + type: 'input', + name: 'devServerTarget', + message: + 'What is the name of the target used to serve the application locally?', + initial: `${projectName}:serve`, + }, + { + type: 'numeral', + name: 'port', + message: 'What port will the application be served on?', + initial: 3000, + }, + ], + { + devServerTarget: `${projectName}:serve`, + port: 3000, + } + ); + + return { + devServerTarget, + baseUrl: `http://localhost:${port}`, + }; +} + async function addFiles( tree: Tree, options: NormalizedSchema, diff --git a/packages/playwright/src/generators/configuration/configuration.ts b/packages/playwright/src/generators/configuration/configuration.ts index bbdfa4f0a7f6b..c8565a3912568 100644 --- a/packages/playwright/src/generators/configuration/configuration.ts +++ b/packages/playwright/src/generators/configuration/configuration.ts @@ -20,6 +20,7 @@ import { writeJson, } from '@nx/devkit'; import { resolveImportPath } from '@nx/devkit/src/generators/project-name-and-root-utils'; +import { promptWhenInteractive } from '@nx/devkit/src/generators/prompt'; import { getRelativePathToRootTsConfig } from '@nx/js'; import { normalizeLinterOption } from '@nx/js/src/utils/generator-prompts'; import { @@ -269,6 +270,13 @@ async function normalizeOptions( const linter = await normalizeLinterOption(tree, options.linter); + if (!options.webServerCommand || !options.webServerAddress) { + const { webServerCommand, webServerAddress } = + await promptForMissingServeData(options.project); + options.webServerCommand = webServerCommand; + options.webServerAddress = webServerAddress; + } + return { ...options, addPlugin, @@ -277,6 +285,37 @@ async function normalizeOptions( }; } +async function promptForMissingServeData(projectName: string) { + const { command, port } = await promptWhenInteractive<{ + command: string; + port: number; + }>( + [ + { + type: 'input', + name: 'command', + message: 'What command should be run to serve the application locally?', + initial: `npx nx serve ${projectName}`, + }, + { + type: 'numeral', + name: 'port', + message: 'What port will the application be served on?', + initial: 3000, + }, + ], + { + command: `npx nx serve ${projectName}`, + port: 3000, + } + ); + + return { + webServerCommand: command, + webServerAddress: `http://localhost:${port}`, + }; +} + function getBrowsersInstallTask() { return () => { output.log({