Skip to content

Commit

Permalink
feat(testing): use helper to determine project name and root director…
Browse files Browse the repository at this point in the history
…y in cypress project generator (#18608)
  • Loading branch information
leosvelperez committed Aug 14, 2023
1 parent e717fab commit 3627df4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-project",
"factory": "./src/generators/cypress-project/cypress-project#cypressProjectGenerator",
"factory": "./src/generators/cypress-project/cypress-project#cypressProjectGeneratorInternal",
"schema": {
"$schema": "http://json-schema.org/schema",
"$id": "NxCypressProjectGeneratorSchema",
Expand Down Expand Up @@ -30,6 +30,11 @@
"description": "A directory where the project is placed.",
"x-priority": "important"
},
"projectNameAndRootFormat": {
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
"type": "string",
"enum": ["as-provided", "derived"]
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
Expand Down Expand Up @@ -78,7 +83,7 @@
},
"description": "Add a Cypress E2E Project.",
"hidden": true,
"implementation": "/packages/cypress/src/generators/cypress-project/cypress-project#cypressProjectGenerator.ts",
"implementation": "/packages/cypress/src/generators/cypress-project/cypress-project#cypressProjectGeneratorInternal.ts",
"aliases": [],
"path": "/packages/cypress/src/generators/cypress-project/schema.json",
"type": "generator"
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"hidden": true
},
"cypress-project": {
"factory": "./src/generators/cypress-project/cypress-project#cypressProjectGenerator",
"factory": "./src/generators/cypress-project/cypress-project#cypressProjectGeneratorInternal",
"schema": "./src/generators/cypress-project/schema.json",
"description": "Add a Cypress E2E Project.",
"hidden": true
Expand Down
69 changes: 36 additions & 33 deletions packages/cypress/src/generators/cypress-project/cypress-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import {
addDependenciesToPackageJson,
addProjectConfiguration,
convertNxGenerator,
extractLayoutDirectory,
formatFiles,
generateFiles,
GeneratorCallback,
getProjects,
getWorkspaceLayout,
joinPathFragments,
logger,
names,
offsetFromRoot,
ProjectConfiguration,
readProjectConfiguration,
Expand All @@ -20,20 +17,17 @@ import {
Tree,
updateJson,
} from '@nx/devkit';
import { Linter } from '@nx/linter';

import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';
import { checkAndCleanWithSemver } from '@nx/devkit/src/utils/semver';
import { getRelativePathToRootTsConfig } from '@nx/js';

import { Linter } from '@nx/linter';
import { join } from 'path';
import { major } from 'semver';
import { addLinterToCyProject } from '../../utils/add-linter';
import { installedCypressVersion } from '../../utils/cypress-version';
import { filePathPrefix } from '../../utils/project-name';
import { cypressVersion, viteVersion } from '../../utils/versions';
import { cypressInitGenerator } from '../init/init';
// app
import { Schema } from './schema';
import { addLinterToCyProject } from '../../utils/add-linter';
import { checkAndCleanWithSemver } from '@nx/devkit/src/utils/semver';
import { major } from 'semver';

export interface CypressProjectSchema extends Schema {
projectName: string;
Expand Down Expand Up @@ -174,7 +168,20 @@ function addProject(tree: Tree, options: CypressProjectSchema) {
* @deprecated use cypressE2EConfigurationGenerator instead
**/
export async function cypressProjectGenerator(host: Tree, schema: Schema) {
const options = normalizeOptions(host, schema);
return await cypressProjectGeneratorInternal(host, {
projectNameAndRootFormat: 'derived',
...schema,
});
}

/**
* @deprecated use cypressE2EConfigurationGenerator instead
**/
export async function cypressProjectGeneratorInternal(
host: Tree,
schema: Schema
) {
const options = await normalizeOptions(host, schema);
const tasks: GeneratorCallback[] = [];
const cypressVersion = installedCypressVersion();
// if there is an installed cypress version, then we don't call
Expand Down Expand Up @@ -211,19 +218,16 @@ export async function cypressProjectGenerator(host: Tree, schema: Schema) {
return runTasksInSerial(...tasks);
}

function normalizeOptions(host: Tree, options: Schema): CypressProjectSchema {
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory
);
const appsDir = layoutDirectory ?? getWorkspaceLayout(host).appsDir;
let projectName: string;
let projectRoot: string;
async function normalizeOptions(
host: Tree,
options: Schema
): Promise<CypressProjectSchema> {
let maybeRootProject: ProjectConfiguration;
let isRootProject = false;

const projects = getProjects(host);
// nx will set the project option for generators when ran within a project.
// since the root project will always be set for standlone projects we can just check it here.
// since the root project will always be set for standalone projects we can just check it here.
if (options.project) {
maybeRootProject = projects.get(options.project);
}
Expand All @@ -234,22 +238,21 @@ function normalizeOptions(host: Tree, options: Schema): CypressProjectSchema {
(!maybeRootProject &&
Array.from(projects.values()).some((config) => config.root === '.'))
) {
projectName = options.name;
projectRoot = options.name;
isRootProject = true;
} else {
projectName = filePathPrefix(
projectDirectory ? `${projectDirectory}-${options.name}` : options.name
);
projectRoot = projectDirectory
? joinPathFragments(
appsDir,
names(projectDirectory).fileName,
options.name
)
: joinPathFragments(appsDir, options.name);
}

let { projectName, projectRoot } = await determineProjectNameAndRootOptions(
host,
{
name: options.name,
projectType: 'application',
directory: isRootProject ? options.name : options.directory,
projectNameAndRootFormat: isRootProject
? 'as-provided'
: options.projectNameAndRootFormat,
}
);

options.linter = options.linter || Linter.EsLint;
options.bundler = options.bundler || 'webpack';
return {
Expand Down
4 changes: 3 additions & 1 deletion packages/cypress/src/generators/cypress-project/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Linter } from '@nx/linter';
import type { ProjectNameAndRootFormat } from '@nx/devkit/src/generators/project-name-and-root-utils';
import type { Linter } from '@nx/linter';

export interface Schema {
project?: string;
baseUrl?: string;
name: string;
directory?: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
linter?: Linter;
js?: boolean;
skipFormat?: boolean;
Expand Down
5 changes: 5 additions & 0 deletions packages/cypress/src/generators/cypress-project/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
"description": "A directory where the project is placed.",
"x-priority": "important"
},
"projectNameAndRootFormat": {
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
"type": "string",
"enum": ["as-provided", "derived"]
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
Expand Down

1 comment on commit 3627df4

@vercel
Copy link

@vercel vercel bot commented on 3627df4 Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx.dev
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.