From 047cf65a95c8a2430ec89e4e10550fb13031bbbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Tue, 24 Oct 2023 13:55:07 +0200 Subject: [PATCH] feat(angular): remove usage of cypressProjectGenerator from ng-add generator --- .../__snapshots__/e2e.migrator.spec.ts.snap | 12 ++-- .../ng-add/migrators/projects/e2e.migrator.ts | 55 +++++-------------- .../generators/configuration/configuration.ts | 1 + 3 files changed, 21 insertions(+), 47 deletions(-) diff --git a/packages/angular/src/generators/ng-add/migrators/projects/__snapshots__/e2e.migrator.spec.ts.snap b/packages/angular/src/generators/ng-add/migrators/projects/__snapshots__/e2e.migrator.spec.ts.snap index 9e51d165ab2d81..ed51da17f6db20 100644 --- a/packages/angular/src/generators/ng-add/migrators/projects/__snapshots__/e2e.migrator.spec.ts.snap +++ b/packages/angular/src/generators/ng-add/migrators/projects/__snapshots__/e2e.migrator.spec.ts.snap @@ -14,11 +14,11 @@ export default defineConfig({ `; exports[`e2e migrator cypress with project root at "" cypress version >=10 should create a cypress.config.ts file when it does not exist 1`] = ` -"import { defineConfig } from 'cypress'; -import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset'; +"import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset'; +import { defineConfig } from 'cypress'; export default defineConfig({ - e2e: nxE2EPreset(__dirname) + e2e: nxE2EPreset(__filename, { cypressDir: 'src' }) }); " `; @@ -90,11 +90,11 @@ export default defineConfig({ `; exports[`e2e migrator cypress with project root at "projects/app1" cypress version >=10 should create a cypress.config.ts file when it does not exist 1`] = ` -"import { defineConfig } from 'cypress'; -import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset'; +"import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset'; +import { defineConfig } from 'cypress'; export default defineConfig({ - e2e: nxE2EPreset(__dirname) + e2e: nxE2EPreset(__filename, { cypressDir: 'src' }) }); " `; diff --git a/packages/angular/src/generators/ng-add/migrators/projects/e2e.migrator.ts b/packages/angular/src/generators/ng-add/migrators/projects/e2e.migrator.ts index c180bb46dab47b..3a170f9335d75c 100644 --- a/packages/angular/src/generators/ng-add/migrators/projects/e2e.migrator.ts +++ b/packages/angular/src/generators/ng-add/migrators/projects/e2e.migrator.ts @@ -1,4 +1,4 @@ -import { cypressProjectGenerator } from '@nx/cypress'; +import { configurationGenerator } from '@nx/cypress'; import { nxE2EPreset } from '@nx/cypress/plugins/cypress-preset'; import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version'; import type { @@ -9,11 +9,9 @@ import type { import { addProjectConfiguration, joinPathFragments, - names, offsetFromRoot, readJson, readProjectConfiguration, - removeProjectConfiguration, stripIndents, updateJson, updateProjectConfiguration, @@ -337,12 +335,20 @@ export class E2eMigrator extends ProjectMigrator { private async migrateCypressE2eProject(): Promise { const oldCypressConfigFilePath = this.getOldCypressConfigFilePath(); - await cypressProjectGenerator(this.tree, { - name: this.project.name, - project: this.appName, + addProjectConfiguration(this.tree, this.project.name, { + projectType: 'application', + root: this.project.newRoot, + sourceRoot: this.project.newSourceRoot, + targets: {}, + tags: [], + implicitDependencies: [this.appName], + }); + await configurationGenerator(this.tree, { + project: this.project.name, linter: this.isProjectUsingEsLint ? Linter.EsLint : Linter.None, - standaloneConfig: true, skipFormat: true, + // any target would do, we replace it later with the target existing in the project being migrated + devServerTarget: `${this.appName}:serve`, }); const cypressConfigFilePath = this.updateOrCreateCypressConfigFile( @@ -388,48 +394,15 @@ export class E2eMigrator extends ProjectMigrator { } private updateCypressProjectConfiguration(cypressConfigPath: string): void { - /** - * The `cypressProjectGenerator` function normalizes the project name. The - * migration keeps the names for existing projects as-is to avoid any - * confusion. The e2e project is technically new, but it's associated - * to an existing application, so we keep it familiar. - */ - const generatedProjectName = names(this.project.name).fileName; - if (this.project.name !== generatedProjectName) { - // If the names are different, we "rename" the newly added project. - this.projectConfig = readProjectConfiguration( - this.tree, - generatedProjectName - ); - - this.projectConfig.root = this.project.newRoot; - this.projectConfig.sourceRoot = this.project.newSourceRoot; - removeProjectConfiguration(this.tree, generatedProjectName); - addProjectConfiguration( - this.tree, - this.project.name, - { ...this.projectConfig }, - true - ); - } else { - this.projectConfig = readProjectConfiguration( - this.tree, - this.project.name - ); - } + this.projectConfig = readProjectConfiguration(this.tree, this.project.name); if (this.isProjectUsingEsLint) { // the generated cypress project always generates a "lint" target, // in case the app was using a different name for it, we use it - const lintTarget = this.projectConfig.targets.lint; if (this.lintTargetName && this.lintTargetName !== 'lint') { this.projectConfig.targets[this.lintTargetName] = this.projectConfig.targets.lint; } - lintTarget.options.lintFilePatterns = - lintTarget.options.lintFilePatterns.map((pattern) => - pattern.replace(`apps/${generatedProjectName}`, this.project.newRoot) - ); } [this.targetNames.e2e, 'cypress-run', 'cypress-open'].forEach((target) => { diff --git a/packages/cypress/src/generators/configuration/configuration.ts b/packages/cypress/src/generators/configuration/configuration.ts index 9c226839b86d50..5421214f37f59a 100644 --- a/packages/cypress/src/generators/configuration/configuration.ts +++ b/packages/cypress/src/generators/configuration/configuration.ts @@ -115,6 +115,7 @@ async function addFiles(tree: Tree, options: NormalizedSchema) { ext: options.js ? 'js' : 'ts', offsetFromRoot: offsetFromRoot(projectConfig.root), offsetFromProjectRoot, + projectRoot: projectConfig.root, tsConfigPath: hasTsConfig ? `${offsetFromProjectRoot}/tsconfig.json` : getRelativePathToRootTsConfig(tree, projectConfig.root),