fix(angular): deprecate simpleModuleName in library generator in favor of simpleName (#13726)

Fixes https://github.com/nrwl/nx/issues/13198
This commit is contained in:
Leosvel Pérez Espinosa 2022-12-09 10:48:23 +01:00 committed by GitHub
parent 34d6de9e0c
commit ecf9a80bae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 179 additions and 27 deletions

View File

@ -850,6 +850,12 @@
"simpleModuleName": {
"description": "Keep the module name simple (when using `--directory`).",
"type": "boolean",
"default": false,
"x-deprecated": "Use `simpleName` instead. It will be removed in v16."
},
"simpleName": {
"description": "Don't include the directory in the name of the module or standalone component entry of the library.",
"type": "boolean",
"default": false
},
"addModuleSpec": {

View File

@ -22,7 +22,7 @@ describe('karmaProject', () => {
buildable: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: false,
simpleName: false,
skipFormat: false,
unitTestRunner: UnitTestRunner.None,
});

View File

@ -372,3 +372,95 @@ import { secondRoutes } from '@proj/second';
{path: '', component: MyLibComponent}
]"
`;
exports[`lib --standalone should generate a library with a standalone component in a directory 1`] = `"export * from \\"./lib/my-dir-my-lib/my-dir-my-lib.component\\";"`;
exports[`lib --standalone should generate a library with a standalone component in a directory 2`] = `
"import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'proj-my-dir-my-lib',
standalone: true,
imports: [CommonModule],
templateUrl: './my-dir-my-lib.component.html',
styleUrls: ['./my-dir-my-lib.component.css']
})
export class MyDirMyLibComponent {
}
"
`;
exports[`lib --standalone should generate a library with a standalone component in a directory 3`] = `
"import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MyDirMyLibComponent } from './my-dir-my-lib.component';
describe('MyDirMyLibComponent', () => {
let component: MyDirMyLibComponent;
let fixture: ComponentFixture<MyDirMyLibComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ MyDirMyLibComponent ]
})
.compileComponents();
fixture = TestBed.createComponent(MyDirMyLibComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
"
`;
exports[`lib --standalone should generate a library with a standalone component in a directory with a simple name 1`] = `"export * from \\"./lib/my-lib/my-lib.component\\";"`;
exports[`lib --standalone should generate a library with a standalone component in a directory with a simple name 2`] = `
"import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'proj-my-lib',
standalone: true,
imports: [CommonModule],
templateUrl: './my-lib.component.html',
styleUrls: ['./my-lib.component.css']
})
export class MyLibComponent {
}
"
`;
exports[`lib --standalone should generate a library with a standalone component in a directory with a simple name 3`] = `
"import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MyLibComponent } from './my-lib.component';
describe('MyLibComponent', () => {
let component: MyLibComponent;
let fixture: ComponentFixture<MyLibComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ MyLibComponent ]
})
.compileComponents();
fixture = TestBed.createComponent(MyLibComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
"
`;

View File

@ -14,17 +14,13 @@ import { Linter } from '@nrwl/linter';
import { UnitTestRunner } from '../../../utils/test-runners';
import { normalizePrefix } from '../../utils/project';
export function normalizeOptions(
host: Tree,
schema: Partial<Schema>
): NormalizedSchema {
export function normalizeOptions(host: Tree, schema: Schema): NormalizedSchema {
// Create a schema with populated default values
const options: Schema = {
buildable: false,
linter: Linter.EsLint,
name: '', // JSON validation will ensure this is set
publishable: false,
simpleModuleName: false,
simpleName: false,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
// Publishable libs cannot use `full` yet, so if its false then use the passed value or default to `full`
@ -53,7 +49,8 @@ export function normalizeOptions(
const projectName = fullProjectDirectory
.replace(new RegExp('/', 'g'), '-')
.replace(/-\d+/g, '');
const fileName = options.simpleModuleName ? name : projectName;
const fileName =
options.simpleName || options.simpleModuleName ? name : projectName;
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);
const moduleName = `${names(fileName).className}Module`;
@ -114,7 +111,7 @@ export function normalizeOptions(
return {
libraryOptions,
componentOptions: {
name: libraryOptions.name,
name: fileName,
standalone: libraryOptions.standalone,
displayBlock,
inlineStyle,

View File

@ -6,7 +6,7 @@ export interface NormalizedSchema {
name: string;
addTailwind?: boolean;
skipFormat?: boolean;
simpleModuleName?: boolean;
simpleName?: boolean;
addModuleSpec?: boolean;
directory?: string;
sourceDir?: string;

View File

@ -44,7 +44,7 @@ describe('lib', () => {
linter: Linter.EsLint,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
simpleModuleName: false,
simpleName: false,
strict: true,
...opts,
});
@ -558,7 +558,7 @@ describe('lib', () => {
await runLibraryGeneratorWithOpts({
name: 'myLib2',
directory: 'myDir',
simpleModuleName: true,
simpleName: true,
});
// ASSERT
@ -693,7 +693,7 @@ describe('lib', () => {
};
await runLibraryGeneratorWithOpts({
directory: 'myDir',
simpleModuleName: true,
simpleName: true,
publishable: true,
importPath: '@myorg/lib',
});
@ -766,7 +766,7 @@ describe('lib', () => {
directory: 'myDir',
routing: true,
lazy: true,
simpleModuleName: true,
simpleName: true,
});
// ASSERT
@ -811,7 +811,7 @@ describe('lib', () => {
directory: 'myDir',
routing: true,
lazy: true,
simpleModuleName: true,
simpleName: true,
parent: 'apps/myapp/src/app/app.module.ts',
});
@ -827,7 +827,7 @@ describe('lib', () => {
directory: 'myDir',
routing: true,
lazy: true,
simpleModuleName: true,
simpleName: true,
parent: 'apps/myapp/src/app/app.module.ts',
});
@ -938,7 +938,7 @@ describe('lib', () => {
await runLibraryGeneratorWithOpts({
name: 'myLib2',
directory: 'myDir',
simpleModuleName: true,
simpleName: true,
routing: true,
});
// ASSERT
@ -984,7 +984,7 @@ describe('lib', () => {
await runLibraryGeneratorWithOpts({
name: 'myLib2',
directory: 'myDir',
simpleModuleName: true,
simpleName: true,
routing: true,
parent: 'apps/myapp/src/app/app.module.ts',
});
@ -998,7 +998,7 @@ describe('lib', () => {
directory: 'myDir',
routing: true,
parent: 'apps/myapp/src/app/app.module.ts',
simpleModuleName: true,
simpleName: true,
});
const moduleContents3 = tree
@ -1442,6 +1442,53 @@ describe('lib', () => {
).toMatchSnapshot();
});
it('should generate a library with a standalone component in a directory', async () => {
await runLibraryGeneratorWithOpts({
standalone: true,
directory: 'my-dir',
});
expect(
tree.read('libs/my-dir/my-lib/src/index.ts', 'utf-8')
).toMatchSnapshot();
expect(
tree.read(
'libs/my-dir/my-lib/src/lib/my-dir-my-lib/my-dir-my-lib.component.ts',
'utf-8'
)
).toMatchSnapshot();
expect(
tree.read(
'libs/my-dir/my-lib/src/lib/my-dir-my-lib/my-dir-my-lib.component.spec.ts',
'utf-8'
)
).toMatchSnapshot();
});
it('should generate a library with a standalone component in a directory with a simple name', async () => {
await runLibraryGeneratorWithOpts({
standalone: true,
directory: 'my-dir',
simpleName: true,
});
expect(
tree.read('libs/my-dir/my-lib/src/index.ts', 'utf-8')
).toMatchSnapshot();
expect(
tree.read(
'libs/my-dir/my-lib/src/lib/my-lib/my-lib.component.ts',
'utf-8'
)
).toMatchSnapshot();
expect(
tree.read(
'libs/my-dir/my-lib/src/lib/my-lib/my-lib.component.spec.ts',
'utf-8'
)
).toMatchSnapshot();
});
it('should generate a library with a standalone component and have it flat with routing setup', async () => {
await runLibraryGeneratorWithOpts({
standalone: true,

View File

@ -30,7 +30,7 @@ import { updateTsConfig } from './lib/update-tsconfig';
import { addStandaloneComponent } from './lib/add-standalone-component';
import { Schema } from './schema';
export async function libraryGenerator(tree: Tree, schema: Partial<Schema>) {
export async function libraryGenerator(tree: Tree, schema: Schema) {
// Do some validation checks
if (!schema.routing && schema.lazy) {
throw new Error(`To use "--lazy" option, "--routing" must also be set.`);

View File

@ -5,7 +5,11 @@ export interface Schema {
name: string;
addTailwind?: boolean;
skipFormat?: boolean;
/**
* @deprecated Use `simpleName` instead. It will be removed in v16.
*/
simpleModuleName?: boolean;
simpleName?: boolean;
addModuleSpec?: boolean;
directory?: string;
sourceDir?: string;

View File

@ -45,6 +45,12 @@
"simpleModuleName": {
"description": "Keep the module name simple (when using `--directory`).",
"type": "boolean",
"default": false,
"x-deprecated": "Use `simpleName` instead. It will be removed in v16."
},
"simpleName": {
"description": "Don't include the directory in the name of the module or standalone component entry of the library.",
"type": "boolean",
"default": false
},
"addModuleSpec": {

View File

@ -18,7 +18,7 @@ describe('updateModuleName Rule', () => {
const updatedModulePath = '/libs/my/first/src/lib/my-first.module.ts';
await libraryGenerator(tree, {
name: 'my-first',
simpleModuleName: true,
simpleName: true,
});
const schema: Schema = {
projectName: 'my-first',
@ -54,7 +54,7 @@ describe('updateModuleName Rule', () => {
buildable: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: true,
simpleName: true,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
});
@ -63,7 +63,7 @@ describe('updateModuleName Rule', () => {
buildable: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: true,
simpleName: true,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
});
@ -169,7 +169,7 @@ describe('updateModuleName Rule', () => {
buildable: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: true,
simpleName: true,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
});
@ -214,7 +214,7 @@ describe('updateModuleName Rule', () => {
buildable: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: true,
simpleName: true,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
});

View File

@ -17,7 +17,7 @@ describe('@nrwl/angular:move', () => {
buildable: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: true,
simpleName: true,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
});

View File

@ -24,7 +24,7 @@ export async function createStorybookTestWorkspaceForLib(
buildable: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: false,
simpleName: false,
skipFormat: false,
unitTestRunner: UnitTestRunner.Jest,
});