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", "name": "cypress-project",
"factory": "./src/generators/cypress-project/cypress-project#cypressProjectGenerator", "factory": "./src/generators/cypress-project/cypress-project#cypressProjectGeneratorInternal",
"schema": { "schema": {
"$schema": "http://json-schema.org/schema", "$schema": "http://json-schema.org/schema",
"$id": "NxCypressProjectGeneratorSchema", "$id": "NxCypressProjectGeneratorSchema",
@ -30,6 +30,11 @@
"description": "A directory where the project is placed.", "description": "A directory where the project is placed.",
"x-priority": "important" "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": { "linter": {
"description": "The tool to use for running lint checks.", "description": "The tool to use for running lint checks.",
"type": "string", "type": "string",
@ -78,7 +83,7 @@
}, },
"description": "Add a Cypress E2E Project.", "description": "Add a Cypress E2E Project.",
"hidden": true, "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": [], "aliases": [],
"path": "/packages/cypress/src/generators/cypress-project/schema.json", "path": "/packages/cypress/src/generators/cypress-project/schema.json",
"type": "generator" "type": "generator"

View File

@ -41,7 +41,7 @@
"hidden": true "hidden": true
}, },
"cypress-project": { "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", "schema": "./src/generators/cypress-project/schema.json",
"description": "Add a Cypress E2E Project.", "description": "Add a Cypress E2E Project.",
"hidden": true "hidden": true

View File

@ -2,15 +2,12 @@ import {
addDependenciesToPackageJson, addDependenciesToPackageJson,
addProjectConfiguration, addProjectConfiguration,
convertNxGenerator, convertNxGenerator,
extractLayoutDirectory,
formatFiles, formatFiles,
generateFiles, generateFiles,
GeneratorCallback, GeneratorCallback,
getProjects, getProjects,
getWorkspaceLayout,
joinPathFragments, joinPathFragments,
logger, logger,
names,
offsetFromRoot, offsetFromRoot,
ProjectConfiguration, ProjectConfiguration,
readProjectConfiguration, readProjectConfiguration,
@ -20,20 +17,17 @@ import {
Tree, Tree,
updateJson, updateJson,
} from '@nx/devkit'; } 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 { getRelativePathToRootTsConfig } from '@nx/js';
import { Linter } from '@nx/linter';
import { join } from 'path'; import { join } from 'path';
import { major } from 'semver';
import { addLinterToCyProject } from '../../utils/add-linter';
import { installedCypressVersion } from '../../utils/cypress-version'; import { installedCypressVersion } from '../../utils/cypress-version';
import { filePathPrefix } from '../../utils/project-name';
import { cypressVersion, viteVersion } from '../../utils/versions'; import { cypressVersion, viteVersion } from '../../utils/versions';
import { cypressInitGenerator } from '../init/init'; import { cypressInitGenerator } from '../init/init';
// app
import { Schema } from './schema'; 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 { export interface CypressProjectSchema extends Schema {
projectName: string; projectName: string;
@ -174,7 +168,20 @@ function addProject(tree: Tree, options: CypressProjectSchema) {
* @deprecated use cypressE2EConfigurationGenerator instead * @deprecated use cypressE2EConfigurationGenerator instead
**/ **/
export async function cypressProjectGenerator(host: Tree, schema: Schema) { 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 tasks: GeneratorCallback[] = [];
const cypressVersion = installedCypressVersion(); const cypressVersion = installedCypressVersion();
// if there is an installed cypress version, then we don't call // 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); return runTasksInSerial(...tasks);
} }
function normalizeOptions(host: Tree, options: Schema): CypressProjectSchema { async function normalizeOptions(
const { layoutDirectory, projectDirectory } = extractLayoutDirectory( host: Tree,
options.directory options: Schema
); ): Promise<CypressProjectSchema> {
const appsDir = layoutDirectory ?? getWorkspaceLayout(host).appsDir;
let projectName: string;
let projectRoot: string;
let maybeRootProject: ProjectConfiguration; let maybeRootProject: ProjectConfiguration;
let isRootProject = false; let isRootProject = false;
const projects = getProjects(host); const projects = getProjects(host);
// nx will set the project option for generators when ran within a project. // 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) { if (options.project) {
maybeRootProject = projects.get(options.project); maybeRootProject = projects.get(options.project);
} }
@ -234,22 +238,21 @@ function normalizeOptions(host: Tree, options: Schema): CypressProjectSchema {
(!maybeRootProject && (!maybeRootProject &&
Array.from(projects.values()).some((config) => config.root === '.')) Array.from(projects.values()).some((config) => config.root === '.'))
) { ) {
projectName = options.name;
projectRoot = options.name;
isRootProject = true; 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.linter = options.linter || Linter.EsLint;
options.bundler = options.bundler || 'webpack'; options.bundler = options.bundler || 'webpack';
return { 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 { export interface Schema {
project?: string; project?: string;
baseUrl?: string; baseUrl?: string;
name: string; name: string;
directory?: string; directory?: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
linter?: Linter; linter?: Linter;
js?: boolean; js?: boolean;
skipFormat?: boolean; skipFormat?: boolean;

View File

@ -32,6 +32,11 @@
"description": "A directory where the project is placed.", "description": "A directory where the project is placed.",
"x-priority": "important" "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": { "linter": {
"description": "The tool to use for running lint checks.", "description": "The tool to use for running lint checks.",
"type": "string", "type": "string",