Skip to content

Commit

Permalink
feat(storybook): infer Cypress project name from app source (#5404)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarmatei committed Apr 22, 2021
1 parent 90b83da commit bd21728
Show file tree
Hide file tree
Showing 32 changed files with 96 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ Type: `string`

A directory where the Cypress project will be placed. Placed at the root by default.

### cypressName

Type: `string`

A custom name for the Cypress project. Inferred from 'name' by default.

### generateCypressSpecs

Type: `boolean`
Expand Down
6 changes: 0 additions & 6 deletions docs/angular/api-react/generators/storybook-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ Type: `string`

A directory where the Cypress project will be placed. Placed at the root by default.

### cypressName

Type: `string`

A custom name for the Cypress project. Inferred from 'name' by default.

### generateCypressSpecs

Type: `boolean`
Expand Down
6 changes: 0 additions & 6 deletions docs/angular/api-storybook/generators/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ Type: `string`

A directory where the Cypress project will be placed. Added at root by default.

### cypressName

Type: `string`

A custom name for the Cypress project. Inferred from 'name' by default.

### js

Default: `false`
Expand Down
6 changes: 0 additions & 6 deletions docs/angular/api-storybook/generators/cypress-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ nx g cypress-project ... --dry-run

## Options

### cypressName

Type: `string`

The name of the Cypress project. Inferred from 'name' by default

### directory

Type: `string`
Expand Down
6 changes: 0 additions & 6 deletions docs/node/api-angular/generators/storybook-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ Type: `string`

A directory where the Cypress project will be placed. Placed at the root by default.

### cypressName

Type: `string`

A custom name for the Cypress project. Inferred from 'name' by default.

### generateCypressSpecs

Type: `boolean`
Expand Down
6 changes: 0 additions & 6 deletions docs/node/api-react/generators/storybook-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ Type: `string`

A directory where the Cypress project will be placed. Placed at the root by default.

### cypressName

Type: `string`

A custom name for the Cypress project. Inferred from 'name' by default.

### generateCypressSpecs

Type: `boolean`
Expand Down
6 changes: 0 additions & 6 deletions docs/node/api-storybook/generators/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ Type: `string`

A directory where the Cypress project will be placed. Added at root by default.

### cypressName

Type: `string`

A custom name for the Cypress project. Inferred from 'name' by default.

### js

Default: `false`
Expand Down
6 changes: 0 additions & 6 deletions docs/node/api-storybook/generators/cypress-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ nx g cypress-project ... --dry-run

## Options

### cypressName

Type: `string`

The name of the Cypress project. Inferred from 'name' by default

### directory

Type: `string`
Expand Down
6 changes: 0 additions & 6 deletions docs/react/api-angular/generators/storybook-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ Type: `string`

A directory where the Cypress project will be placed. Placed at the root by default.

### cypressName

Type: `string`

A custom name for the Cypress project. Inferred from 'name' by default.

### generateCypressSpecs

Type: `boolean`
Expand Down
6 changes: 0 additions & 6 deletions docs/react/api-react/generators/storybook-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ Type: `string`

A directory where the Cypress project will be placed. Placed at the root by default.

### cypressName

Type: `string`

A custom name for the Cypress project. Inferred from 'name' by default.

### generateCypressSpecs

Type: `boolean`
Expand Down
6 changes: 0 additions & 6 deletions docs/react/api-storybook/generators/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ Type: `string`

A directory where the Cypress project will be placed. Added at root by default.

### cypressName

Type: `string`

A custom name for the Cypress project. Inferred from 'name' by default.

### js

Default: `false`
Expand Down
6 changes: 0 additions & 6 deletions docs/react/api-storybook/generators/cypress-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ nx g cypress-project ... --dry-run

## Options

### cypressName

Type: `string`

The name of the Cypress project. Inferred from 'name' by default

### directory

Type: `string`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ Array [
"/.storybook/main.js",
"/.storybook/tsconfig.json",
"/.storybook/webpack.config.js",
"/apps/one/two/lib-test/cypress.json",
"/apps/one/two/lib-test/tsconfig.e2e.json",
"/apps/one/two/lib-test/tsconfig.json",
"/apps/one/two/lib-test/.eslintrc.json",
"/apps/one/two/lib-test/src/fixtures/example.json",
"/apps/one/two/lib-test/src/plugins/index.js",
"/apps/one/two/lib-test/src/support/commands.ts",
"/apps/one/two/lib-test/src/support/index.ts",
"/apps/one/two/lib-test/src/integration/test-button/test-button.component.spec.ts",
"/apps/one/two/lib-test/src/integration/test-other/test-other.component.spec.ts",
"/apps/one/two/lib-test/src/integration/barrel-button/barrel-button.component.spec.ts",
"/apps/one/two/lib-test/src/integration/variable-declare-button/variable-declare-button.component.spec.ts",
"/apps/one/two/lib-test/src/integration/variable-declare-view/variable-declare-view.component.spec.ts",
"/apps/one/two/lib-test/src/integration/nested-button/nested-button.component.spec.ts",
"/apps/one/two/test-ui-lib-e2e/cypress.json",
"/apps/one/two/test-ui-lib-e2e/tsconfig.e2e.json",
"/apps/one/two/test-ui-lib-e2e/tsconfig.json",
"/apps/one/two/test-ui-lib-e2e/.eslintrc.json",
"/apps/one/two/test-ui-lib-e2e/src/fixtures/example.json",
"/apps/one/two/test-ui-lib-e2e/src/plugins/index.js",
"/apps/one/two/test-ui-lib-e2e/src/support/commands.ts",
"/apps/one/two/test-ui-lib-e2e/src/support/index.ts",
"/apps/one/two/test-ui-lib-e2e/src/integration/test-button/test-button.component.spec.ts",
"/apps/one/two/test-ui-lib-e2e/src/integration/test-other/test-other.component.spec.ts",
"/apps/one/two/test-ui-lib-e2e/src/integration/barrel-button/barrel-button.component.spec.ts",
"/apps/one/two/test-ui-lib-e2e/src/integration/variable-declare-button/variable-declare-button.component.spec.ts",
"/apps/one/two/test-ui-lib-e2e/src/integration/variable-declare-view/variable-declare-view.component.spec.ts",
"/apps/one/two/test-ui-lib-e2e/src/integration/nested-button/nested-button.component.spec.ts",
]
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ describe('schematic:configuration', () => {
generateCypressSpecs: true,
generateStories: true,
cypressDirectory: 'one/two',
cypressName: 'lib-test',
},
appTree
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
Rule,
schematic,
noop,
Tree,
} from '@angular-devkit/schematics';
import { StorybookStoriesSchema } from '../stories/stories';
import { StorybookConfigureSchema } from './schema';
import { wrapAngularDevkitSchematic } from '@nrwl/devkit/ngcli-adapter';
import { getE2eProjectName } from '@nrwl/cypress';
import { getE2eProjectName } from '@nrwl/cypress/src/utils/project-name';
import { getWorkspace } from '@nrwl/workspace';

export default function (schema: StorybookConfigureSchema): Rule {
if (schema.generateCypressSpecs && !schema.generateStories) {
Expand All @@ -24,18 +26,20 @@ export default function (schema: StorybookConfigureSchema): Rule {
configureCypress: schema.configureCypress,
linter: schema.linter,
cypressDirectory: schema.cypressDirectory,
cypressName: schema.cypressName,
}),
schema.generateStories ? generateStories(schema) : noop(),
]);
}

function generateStories(schema: StorybookConfigureSchema): Rule {
return (tree, context) => {
const e2eProjectName = getE2eProjectName({
name: schema.cypressName || `${schema.name}-e2e`,
directory: schema.cypressDirectory,
});
return async (tree: Tree, context) => {
const workspace = await getWorkspace(tree);
const project = workspace.projects.get(schema.name);
const e2eProjectName = getE2eProjectName(
schema.name,
project.root,
schema.cypressDirectory
);
return schematic<StorybookStoriesSchema>('stories', {
name: schema.name,
generateCypressSpecs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ export interface StorybookConfigureSchema {
generateCypressSpecs: boolean;
linter: Linter;
cypressDirectory?: string;
cypressName?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
"type": "string",
"description": "A directory where the Cypress project will be placed. Placed at the root by default."
},
"cypressName": {
"type": "string",
"description": "A custom name for the Cypress project. Inferred from 'name' by default."
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
Expand Down
5 changes: 1 addition & 4 deletions packages/cypress/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export {
cypressProjectGenerator,
getE2eProjectName,
} from './src/generators/cypress-project/cypress-project';
export { cypressProjectGenerator } from './src/generators/cypress-project/cypress-project';
export { cypressInitGenerator } from './src/generators/init/init';
export { conversionGenerator } from './src/generators/convert-tslint-to-eslint/convert-tslint-to-eslint';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readJson, readProjectConfiguration, Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { cypressProjectGenerator, getE2eProjectName } from './cypress-project';
import { cypressProjectGenerator } from './cypress-project';
import { Schema } from './schema';
import { Linter } from '@nrwl/linter';

Expand Down Expand Up @@ -295,17 +295,5 @@ describe('schematic:cypress-project', () => {
'apps/one/two/other-e2e/src/integration/app.spec.ts',
].forEach((path) => expect(tree.exists(path)).toBeTruthy());
});
test.each`
name | directory | projectName
${'app-e2e'} | ${'one/two'} | ${'one-two-app-e2e'}
${'app-e2e'} | ${'one'} | ${'one-app-e2e'}
${'app-e2e'} | ${undefined} | ${'app-e2e'}
${'app'} | ${undefined} | ${'app'}
`(
'name $name and directory $directory should result in project name $projectName',
({ name, directory, projectName }) => {
expect(getE2eProjectName({ name, directory })).toEqual(projectName);
}
);
});
});
23 changes: 5 additions & 18 deletions packages/cypress/src/generators/cypress-project/cypress-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
joinPathFragments,
names,
offsetFromRoot,
readProjectConfiguration,
toJS,
Tree,
updateJson,
Expand All @@ -19,6 +20,7 @@ import { join } from 'path';
// app
import { Schema } from './schema';
import { eslintPluginCypressVersion } from '../../utils/versions';
import { filePathPrefix } from '../../utils/project-name';

export interface CypressProjectSchema extends Schema {
projectName: string;
Expand Down Expand Up @@ -136,26 +138,11 @@ export async function cypressProjectGenerator(host: Tree, schema: Schema) {
return installTask;
}

export function getE2eProjectName({
name,
directory,
}: {
name: string;
directory?: string;
}) {
return `${directory ? filePathToProjectName(directory, name) : name}`;
}

function filePathToProjectName(directory: string, name: string) {
return `${names(directory).fileName}-${name}`.replace(
new RegExp('/', 'g'),
'-'
);
}

function normalizeOptions(host: Tree, options: Schema): CypressProjectSchema {
const { appsDir } = getWorkspaceLayout(host);
const projectName = getE2eProjectName(options);
const projectName = options.directory
? `${filePathPrefix(options.directory)}-${options.name}`
: options.name;
const projectRoot = options.directory
? joinPathFragments(
appsDir,
Expand Down
22 changes: 22 additions & 0 deletions packages/cypress/src/utils/project-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { names } from '@nrwl/devkit';

export function getUnscopedLibName(libRoot: string) {
return libRoot.substr(libRoot.lastIndexOf('/') + 1);
}

export function getE2eProjectName(
targetProjectName: string,
targetLibRoot: string,
cypressDirectory?: string
) {
if (cypressDirectory) {
return `${filePathPrefix(cypressDirectory)}-${getUnscopedLibName(
targetLibRoot
)}-e2e`;
}
return `${targetProjectName}-e2e`;
}

export function filePathPrefix(directory: string) {
return `${names(directory).fileName}`.replace(new RegExp('/', 'g'), '-');
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,18 @@ describe('react:storybook-configuration', () => {
configureCypress: true,
generateCypressSpecs: true,
cypressDirectory: 'one/two',
cypressName: 'app-test-e2e',
});
[
'apps/one/two/app-test-e2e/cypress.json',
'apps/one/two/app-test-e2e/src/fixtures/example.json',
'apps/one/two/app-test-e2e/src/plugins/index.js',
'apps/one/two/app-test-e2e/src/support/commands.ts',
'apps/one/two/app-test-e2e/src/support/index.ts',
'apps/one/two/app-test-e2e/tsconfig.e2e.json',
'apps/one/two/app-test-e2e/tsconfig.json',
'apps/one/two/app-test-e2e/.eslintrc.json',
'apps/one/two/app-test-e2e/src/integration/test-ui-lib/test-ui-lib.spec.ts',
'apps/one/two/app-test-e2e/src/integration/my-component/my-component.spec.ts',
'apps/one/two/test-ui-lib-e2e/cypress.json',
'apps/one/two/test-ui-lib-e2e/src/fixtures/example.json',
'apps/one/two/test-ui-lib-e2e/src/plugins/index.js',
'apps/one/two/test-ui-lib-e2e/src/support/commands.ts',
'apps/one/two/test-ui-lib-e2e/src/support/index.ts',
'apps/one/two/test-ui-lib-e2e/tsconfig.e2e.json',
'apps/one/two/test-ui-lib-e2e/tsconfig.json',
'apps/one/two/test-ui-lib-e2e/.eslintrc.json',
'apps/one/two/test-ui-lib-e2e/src/integration/test-ui-lib/test-ui-lib.spec.ts',
'apps/one/two/test-ui-lib-e2e/src/integration/my-component/my-component.spec.ts',
].forEach((file) => {
expect(appTree.exists(file)).toBeTruthy();
});
Expand Down

0 comments on commit bd21728

Please sign in to comment.