Skip to content

Commit

Permalink
feat(misc): remove usages of @nx/cypress:cypress-project internally
Browse files Browse the repository at this point in the history
Replace with call to `@nx/cypress:configuration`. The deprecated
generator will be removed in Nx 18.
  • Loading branch information
jaysoo committed Oct 20, 2023
1 parent 85bda9a commit 334bd04
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@
"description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.",
"default": false
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside workspace.json.",
"type": "boolean",
"default": true,
"x-deprecated": "Nx only supports standaloneConfig"
},
"skipPackageJson": {
"type": "boolean",
"default": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
"enum": ["eslint", "none"],
"default": "eslint"
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean",
"default": true,
"x-deprecated": "Nx only supports standaloneConfig"
},
"ciTargetName": {
"type": "string",
"description": "The name of the devServerTarget to use for the Cypress CI configuration. Used to control if using <storybook-project>:static-storybook:ci or <storybook-project>:storybook:ci",
Expand All @@ -49,6 +43,11 @@
"type": "boolean",
"default": false,
"x-priority": "internal"
},
"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"]
}
},
"required": ["name"],
Expand Down
17 changes: 9 additions & 8 deletions packages/angular/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ import {
} from '@nx/devkit';
import { nxVersion } from '../../../utils/versions';
import type { NormalizedSchema } from './normalized-schema';
import { cypressProjectGenerator } from '@nx/cypress';
import { configurationGenerator } from '@nx/cypress';

export async function addE2e(tree: Tree, options: NormalizedSchema) {
if (options.e2eTestRunner === 'cypress') {
// TODO: This can call `@nx/web:static-config` generator when ready
addFileServerTarget(tree, options, 'serve-static');

await cypressProjectGenerator(tree, {
name: options.e2eProjectName,
addProjectConfiguration(tree, options.e2eProjectName, {
root: options.e2eProjectRoot,
sourceRoot: joinPathFragments(options.e2eProjectRoot, 'src'),
targets: {},
implicitDependencies: [options.name],
});
await configurationGenerator(tree, {
project: options.e2eProjectName,
directory: options.e2eProjectRoot,
// the name and root are already normalized, instruct the generator to use them as is
projectNameAndRootFormat: 'as-provided',
project: options.name,
linter: options.linter,
standaloneConfig: options.standaloneConfig,
skipPackageJson: options.skipPackageJson,
skipFormat: true,
});
Expand Down
7 changes: 6 additions & 1 deletion packages/cypress/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { configurationGenerator } from './src/generators/configuration/configuration';
import { componentConfigurationGenerator } from './src/generators/component-configuration/component-configuration';
import { cypressProjectGenerator as _cypressProjectGenerator } from './src/generators/cypress-project/cypress-project';

export { configurationGenerator, componentConfigurationGenerator };

// Maintain backwards compatibility with the old names in case community plugins used them.
// TODO(v18): Remove old name
/** @deprecated Use `configurationGenerator` instead. */
export const cypressComponentConfiguration = componentConfigurationGenerator;

export { configurationGenerator as cypressE2EConfigurationGenerator };
export { cypressProjectGenerator } from './src/generators/cypress-project/cypress-project';
// TODO(v18): Remove project generator
/** @deprecated Add a new project and call `configurationGenerator` instead. */
export const cypressProjectGenerator = _cypressProjectGenerator;
export { cypressInitGenerator } from './src/generators/init/init';
export { migrateCypressProject } from './src/generators/migrate-to-cypress-11/migrate-to-cypress-11';
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export interface Schema {
js?: boolean;
skipFormat?: boolean;
setParserOptionsProject?: boolean;
standaloneConfig?: boolean;
skipPackageJson?: boolean;
bundler?: 'webpack' | 'vite' | 'none';
}
6 changes: 0 additions & 6 deletions packages/cypress/src/generators/cypress-project/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@
"description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.",
"default": false
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside workspace.json.",
"type": "boolean",
"default": true,
"x-deprecated": "Nx only supports standaloneConfig"
},
"skipPackageJson": {
"type": "boolean",
"default": false,
Expand Down
23 changes: 10 additions & 13 deletions packages/next/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import {
addProjectConfiguration,
ensurePackage,
getPackageManagerCommand,
joinPathFragments,
Tree,
} from '@nx/devkit';
import { addProjectConfiguration, ensurePackage, getPackageManagerCommand, joinPathFragments, Tree } from '@nx/devkit';
import { Linter } from '@nx/eslint';

import { nxVersion } from '../../../utils/versions';
import { NormalizedSchema } from './normalize-options';

export async function addE2e(host: Tree, options: NormalizedSchema) {
if (options.e2eTestRunner === 'cypress') {
const { cypressProjectGenerator } = ensurePackage<
const { configurationGenerator } = ensurePackage<
typeof import('@nx/cypress')
>('@nx/cypress', nxVersion);
return cypressProjectGenerator(host, {
addProjectConfiguration(host, options.e2eProjectName, {
root: options.e2eProjectRoot,
sourceRoot: joinPathFragments(options.e2eProjectRoot, 'src'),
targets: {},
implicitDependencies: [options.projectName],
});
return configurationGenerator(host, {
...options,
linter: Linter.EsLint,
name: options.e2eProjectName,
project: options.e2eProjectName,
directory: options.e2eProjectRoot,
// the name and root are already normalized, instruct the generator to use them as is
projectNameAndRootFormat: 'as-provided',
project: options.projectName,
skipFormat: true,
});
} else if (options.e2eTestRunner === 'playwright') {
Expand Down
21 changes: 14 additions & 7 deletions packages/react/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,33 @@ export async function addE2e(
options: NormalizedSchema
): Promise<GeneratorCallback> {
switch (options.e2eTestRunner) {
case 'cypress':
case 'cypress': {
webStaticServeGenerator(tree, {
buildTarget: `${options.projectName}:build`,
targetName: 'serve-static',
});

const { cypressProjectGenerator } = ensurePackage<
const { configurationGenerator } = ensurePackage<
typeof import('@nx/cypress')
>('@nx/cypress', nxVersion);

return await cypressProjectGenerator(tree, {
addProjectConfiguration(tree, options.e2eProjectName, {
root: options.e2eProjectRoot,
sourceRoot: joinPathFragments(options.e2eProjectRoot, 'src'),
targets: {},
implicitDependencies: [options.projectName],
});

return await configurationGenerator(tree, {
...options,
name: options.e2eProjectName,
project: options.e2eProjectName,
directory: options.e2eProjectRoot,
// the name and root are already normalized, instruct the generator to use them as is
projectNameAndRootFormat: 'as-provided',
project: options.projectName,
bundler: options.bundler === 'rspack' ? 'webpack' : options.bundler,
skipFormat: true,
});
case 'playwright':
}
case 'playwright': {
const { configurationGenerator } = ensurePackage<
typeof import('@nx/playwright')
>('@nx/playwright', nxVersion);
Expand All @@ -58,6 +64,7 @@ export async function addE2e(
}`,
webServerAddress: 'http://localhost:4200',
});
}
case 'none':
default:
return () => {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version';
import { readJson, readProjectConfiguration, Tree } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Linter } from '@nx/eslint';
import { libraryGenerator } from '@nx/js';
import { cypressProjectGenerator } from './cypress-project';

jest.mock('@nx/cypress/src/utils/cypress-version');
describe('@nx/storybook:cypress-project', () => {
let tree: Tree;
let mockedInstalledCypressVersion: jest.Mock<
ReturnType<typeof installedCypressVersion>
> = installedCypressVersion as never;

beforeEach(async () => {
mockedInstalledCypressVersion.mockReturnValue(10);
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
await libraryGenerator(tree, {
name: 'test-ui-lib',
Expand All @@ -36,19 +30,6 @@ describe('@nx/storybook:cypress-project', () => {
expect(cypressConfig).toMatchSnapshot();
});

it('should update cypress.json file if present', async () => {
mockedInstalledCypressVersion.mockReturnValue(9);

await cypressProjectGenerator(tree, {
name: 'test-ui-lib',
linter: Linter.EsLint,
});

expect(tree.exists('apps/test-ui-lib-e2e/cypress.json')).toBeTruthy();
const cypressConfig = readJson(tree, 'apps/test-ui-lib-e2e/cypress.json');
expect(cypressConfig.baseUrl).toEqual('http://localhost:4400');
});

it('should update `angular.json` file', async () => {
await cypressProjectGenerator(tree, {
name: 'test-ui-lib',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { getE2eProjectName } from '@nx/cypress/src/utils/project-name';
import {
cypressInitGenerator as _cypressInitGenerator,
cypressProjectGenerator as _cypressProjectGenerator,
} from '@nx/cypress';
import {
getE2eProjectName,
getUnscopedLibName,
} from '@nx/cypress/src/utils/project-name';
import {
addProjectConfiguration,
ensurePackage,
formatFiles,
generateFiles,
GeneratorCallback,
joinPathFragments,
readJson,
readProjectConfiguration,
runTasksInSerial,
Expand All @@ -18,9 +14,11 @@ import {
updateProjectConfiguration,
} from '@nx/devkit';
import { Linter } from '@nx/eslint';

import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';
import { join } from 'path';

import { safeFileDelete } from '../../utils/utilities';
import { nxVersion } from '../../utils/versions';

export interface CypressConfigureSchema {
name: string;
Expand All @@ -30,31 +28,61 @@ export interface CypressConfigureSchema {
standaloneConfig?: boolean;
ciTargetName?: string;
skipFormat?: boolean;
projectNameAndRootFormat?: 'as-provided' | 'derived';
}

export async function cypressProjectGenerator(
tree: Tree,
schema: CypressConfigureSchema
) {
return await cypressProjectGeneratorInternal(tree, {
projectNameAndRootFormat: 'derived',
...schema,
});
}

export async function cypressProjectGeneratorInternal(
tree: Tree,
schema: CypressConfigureSchema
) {
const { configurationGenerator, cypressInitGenerator } = ensurePackage<
typeof import('@nx/cypress')
>('@nx/cypress', nxVersion);

const e2eName = schema.name ? `${schema.name}-e2e` : undefined;
const { projectName, projectRoot } = await determineProjectNameAndRootOptions(
tree,
{
name: e2eName,
projectType: 'application',
directory: schema.directory,
projectNameAndRootFormat: schema.projectNameAndRootFormat,
callingGenerator: '@nx/storybook:cypress-project',
}
);
const libConfig = readProjectConfiguration(tree, schema.name);
const libRoot = libConfig.root;
const cypressProjectName = `${
schema.directory ? getUnscopedLibName(libRoot) : schema.name
}-e2e`;

const tasks: GeneratorCallback[] = [];

if (!projectAlreadyHasCypress(tree)) {
tasks.push(await _cypressInitGenerator(tree, {}));
tasks.push(await cypressInitGenerator(tree, {}));
}

const installTask = await _cypressProjectGenerator(tree, {
name: cypressProjectName,
project: schema.name,
addProjectConfiguration(tree, projectName, {
root: projectRoot,
projectType: 'application',
sourceRoot: joinPathFragments(projectRoot, 'src'),
targets: {},
implicitDependencies: [projectName],
});

const installTask = await configurationGenerator(tree, {
project: projectName,
js: schema.js,
linter: schema.linter,
directory: schema.directory,
standaloneConfig: schema.standaloneConfig,
directory: projectRoot,
devServerTarget: `${schema.name}:storybook`,
skipFormat: true,
});
tasks.push(installTask);
Expand Down
11 changes: 5 additions & 6 deletions packages/storybook/src/generators/cypress-project/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
"enum": ["eslint", "none"],
"default": "eslint"
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean",
"default": true,
"x-deprecated": "Nx only supports standaloneConfig"
},
"ciTargetName": {
"type": "string",
"description": "The name of the devServerTarget to use for the Cypress CI configuration. Used to control if using <storybook-project>:static-storybook:ci or <storybook-project>:storybook:ci",
Expand All @@ -49,6 +43,11 @@
"type": "boolean",
"default": false,
"x-priority": "internal"
},
"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"]
}
},
"required": ["name"]
Expand Down
Loading

0 comments on commit 334bd04

Please sign in to comment.