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": { "simpleModuleName": {
"description": "Keep the module name simple (when using `--directory`).", "description": "Keep the module name simple (when using `--directory`).",
"type": "boolean", "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 "default": false
}, },
"addModuleSpec": { "addModuleSpec": {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -45,6 +45,12 @@
"simpleModuleName": { "simpleModuleName": {
"description": "Keep the module name simple (when using `--directory`).", "description": "Keep the module name simple (when using `--directory`).",
"type": "boolean", "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 "default": false
}, },
"addModuleSpec": { "addModuleSpec": {

View File

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

View File

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

View File

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