feat(testing): use helper to determine project name and root directory in cypress project generator (#18608)

This commit is contained in:
Leosvel Pérez Espinosa 2023-08-14 19:42:36 +01:00 committed by GitHub
parent e717fab6d5
commit 3627df4d03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 37 deletions

View File

@ -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",
@ -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",
@ -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"

View File

@ -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

View File

@ -2,15 +2,12 @@ import {
addDependenciesToPackageJson,
addProjectConfiguration,
convertNxGenerator,
extractLayoutDirectory,
formatFiles,
generateFiles,
GeneratorCallback,
getProjects,
getWorkspaceLayout,
joinPathFragments,
logger,
names,
offsetFromRoot,
ProjectConfiguration,
readProjectConfiguration,
@ -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;
@ -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
@ -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);
}
@ -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 {

View File

@ -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;

View File

@ -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",