From c8a2f50f3621988713b1fabbc1cc96b1e8ab5f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Wed, 12 Apr 2023 10:35:51 +0100 Subject: [PATCH] feat(angular): remove deprecated simpleModuleName option from library generator (#16219) --- .../packages/angular/generators/library.json | 6 - packages/angular/migrations.json | 6 + .../library/lib/normalize-options.ts | 3 +- .../src/generators/library/schema.d.ts | 4 - .../src/generators/library/schema.json | 6 - ...enerator-simple-module-name-option.spec.ts | 277 ++++++++++++++++++ ...ary-generator-simple-module-name-option.ts | 65 ++++ 7 files changed, 349 insertions(+), 18 deletions(-) create mode 100644 packages/angular/src/migrations/update-16-0-0/remove-library-generator-simple-module-name-option.spec.ts create mode 100644 packages/angular/src/migrations/update-16-0-0/remove-library-generator-simple-module-name-option.ts diff --git a/docs/generated/packages/angular/generators/library.json b/docs/generated/packages/angular/generators/library.json index adde6fab5bb11..e0332f642b5b0 100644 --- a/docs/generated/packages/angular/generators/library.json +++ b/docs/generated/packages/angular/generators/library.json @@ -45,12 +45,6 @@ "default": false, "x-priority": "internal" }, - "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", diff --git a/packages/angular/migrations.json b/packages/angular/migrations.json index 609ab47d476a9..8b3badaf7669a 100644 --- a/packages/angular/migrations.json +++ b/packages/angular/migrations.json @@ -164,6 +164,12 @@ "version": "15.9.0-beta.9", "description": "Update the file-server executor to use @nrwl/web:file-server", "factory": "./src/migrations/update-15-9-0/update-file-server-executor" + }, + "remove-library-generator-simple-module-name-option": { + "cli": "nx", + "version": "16.0.0-beta.1", + "description": "Replace the deprecated library generator 'simpleModuleName' option from generator defaults with 'simpleName'", + "factory": "./src/migrations/update-16-0-0/remove-library-generator-simple-module-name-option" } }, "packageJsonUpdates": { diff --git a/packages/angular/src/generators/library/lib/normalize-options.ts b/packages/angular/src/generators/library/lib/normalize-options.ts index faa5041ef8de5..0ae495de2a34b 100644 --- a/packages/angular/src/generators/library/lib/normalize-options.ts +++ b/packages/angular/src/generators/library/lib/normalize-options.ts @@ -47,8 +47,7 @@ export function normalizeOptions(host: Tree, schema: Schema): NormalizedSchema { const projectName = fullProjectDirectory .replace(new RegExp('/', 'g'), '-') .replace(/-\d+/g, ''); - const fileName = - options.simpleName || options.simpleModuleName ? name : projectName; + const fileName = options.simpleName ? name : projectName; const projectRoot = joinPathFragments(libsDir, fullProjectDirectory); const moduleName = `${names(fileName).className}Module`; diff --git a/packages/angular/src/generators/library/schema.d.ts b/packages/angular/src/generators/library/schema.d.ts index 178d0e627744a..7524a31209a65 100644 --- a/packages/angular/src/generators/library/schema.d.ts +++ b/packages/angular/src/generators/library/schema.d.ts @@ -5,10 +5,6 @@ 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; diff --git a/packages/angular/src/generators/library/schema.json b/packages/angular/src/generators/library/schema.json index 8463a226f44dd..e75089f097afa 100644 --- a/packages/angular/src/generators/library/schema.json +++ b/packages/angular/src/generators/library/schema.json @@ -45,12 +45,6 @@ "default": false, "x-priority": "internal" }, - "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", diff --git a/packages/angular/src/migrations/update-16-0-0/remove-library-generator-simple-module-name-option.spec.ts b/packages/angular/src/migrations/update-16-0-0/remove-library-generator-simple-module-name-option.spec.ts new file mode 100644 index 0000000000000..65025c659708a --- /dev/null +++ b/packages/angular/src/migrations/update-16-0-0/remove-library-generator-simple-module-name-option.spec.ts @@ -0,0 +1,277 @@ +import type { Tree } from '@nrwl/devkit'; +import { + addProjectConfiguration, + readNxJson, + readProjectConfiguration, + updateNxJson, +} from '@nrwl/devkit'; +import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; +import removeLibraryGeneratorSimpleModuleNameOption from './remove-library-generator-simple-module-name-option'; + +describe('removeLibraryGeneratorSimpleModuleNameOption', () => { + let tree: Tree; + + beforeEach(() => { + tree = createTreeWithEmptyWorkspace(); + }); + + describe('nx.json', () => { + it('should replace simpleModuleName with simpleName', async () => { + const nxJson = readNxJson(tree); + updateNxJson(tree, { + ...nxJson, + generators: { + '@nrwl/angular:library': { + simpleModuleName: true, + }, + }, + }); + + await removeLibraryGeneratorSimpleModuleNameOption(tree); + + const updatedNxJson = readNxJson(tree); + expect(updatedNxJson.generators['@nrwl/angular:library']).toStrictEqual({ + simpleName: true, + }); + }); + + it('should support nested library generator default', async () => { + const nxJson = readNxJson(tree); + updateNxJson(tree, { + ...nxJson, + generators: { + '@nrwl/angular': { + library: { + simpleModuleName: true, + }, + }, + }, + }); + + await removeLibraryGeneratorSimpleModuleNameOption(tree); + + const updatedNxJson = readNxJson(tree); + expect(updatedNxJson.generators['@nrwl/angular']).toStrictEqual({ + library: { + simpleName: true, + }, + }); + }); + + it('should keep simpleName if defined and remove simpleModuleName', async () => { + const nxJson = readNxJson(tree); + updateNxJson(tree, { + ...nxJson, + generators: { + '@nrwl/angular:library': { + simpleModuleName: true, + simpleName: false, + }, + }, + }); + + await removeLibraryGeneratorSimpleModuleNameOption(tree); + + const updatedNxJson = readNxJson(tree); + expect(updatedNxJson.generators['@nrwl/angular:library']).toStrictEqual({ + simpleName: false, + }); + }); + + it('should do nothing if simpleModuleName is not set', async () => { + const nxJson = readNxJson(tree); + updateNxJson(tree, { + ...nxJson, + generators: { + '@nrwl/angular:library': { + simpleName: true, + }, + }, + }); + + await removeLibraryGeneratorSimpleModuleNameOption(tree); + + const updatedNxJson = readNxJson(tree); + expect(updatedNxJson.generators['@nrwl/angular:library']).toStrictEqual({ + simpleName: true, + }); + }); + + it('should not throw when library generator defaults are not set', async () => { + const nxJson = readNxJson(tree); + updateNxJson(tree, { + ...nxJson, + generators: { + '@nrwl/angular:component': { + standalone: true, + }, + }, + }); + + await expect( + removeLibraryGeneratorSimpleModuleNameOption(tree) + ).resolves.not.toThrow(); + + const updatedNxJson = readNxJson(tree); + expect(updatedNxJson.generators).toStrictEqual({ + '@nrwl/angular:component': { + standalone: true, + }, + }); + }); + + it('should not throw when generators defaults are not set', async () => { + const nxJson = readNxJson(tree); + updateNxJson(tree, { ...nxJson, generators: undefined }); + + await expect( + removeLibraryGeneratorSimpleModuleNameOption(tree) + ).resolves.not.toThrow(); + + const updatedNxJson = readNxJson(tree); + expect(updatedNxJson.generators).toBeUndefined(); + }); + + it('should not throw when nx.json does not exist', async () => { + tree.delete('nx.json'); + + await expect( + removeLibraryGeneratorSimpleModuleNameOption(tree) + ).resolves.not.toThrow(); + + expect(tree.exists('nx.json')).toBe(false); + }); + }); + + describe('project configs', () => { + it('should replace simpleModuleName with simpleName', async () => { + const project = { + name: 'project', + root: '/', + targets: {}, + generators: { + '@nrwl/angular:library': { + simpleModuleName: true, + }, + }, + }; + addProjectConfiguration(tree, 'project', project); + + await removeLibraryGeneratorSimpleModuleNameOption(tree); + + const updatedProject = readProjectConfiguration(tree, 'project'); + expect(updatedProject.generators['@nrwl/angular:library']).toStrictEqual({ + simpleName: true, + }); + }); + + it('should support nested library generator default', async () => { + const project = { + name: 'project', + root: '/', + targets: {}, + generators: { + '@nrwl/angular': { + library: { + simpleModuleName: true, + }, + }, + }, + }; + addProjectConfiguration(tree, 'project', project); + + await removeLibraryGeneratorSimpleModuleNameOption(tree); + + const updatedProject = readProjectConfiguration(tree, 'project'); + expect(updatedProject.generators['@nrwl/angular']).toStrictEqual({ + library: { + simpleName: true, + }, + }); + }); + + it('should keep simpleName if defined and remove simpleModuleName', async () => { + const project = { + name: 'project', + root: '/', + targets: {}, + generators: { + '@nrwl/angular:library': { + simpleModuleName: true, + simpleName: false, + }, + }, + }; + addProjectConfiguration(tree, 'project', project); + + await removeLibraryGeneratorSimpleModuleNameOption(tree); + + const updatedProject = readProjectConfiguration(tree, 'project'); + expect(updatedProject.generators['@nrwl/angular:library']).toStrictEqual({ + simpleName: false, + }); + }); + + it('should do nothing if simpleModuleName is not set', async () => { + const project = { + name: 'project', + root: '/', + targets: {}, + generators: { + '@nrwl/angular:library': { + simpleName: true, + }, + }, + }; + addProjectConfiguration(tree, 'project', project); + + await removeLibraryGeneratorSimpleModuleNameOption(tree); + + const updatedProject = readProjectConfiguration(tree, 'project'); + expect(updatedProject.generators['@nrwl/angular:library']).toStrictEqual({ + simpleName: true, + }); + }); + + it('should not throw when library generator defaults are not set', async () => { + const project = { + name: 'project', + root: '/', + targets: {}, + generators: { + '@nrwl/angular:component': { + standalone: true, + }, + }, + }; + addProjectConfiguration(tree, 'project', project); + + await expect( + removeLibraryGeneratorSimpleModuleNameOption(tree) + ).resolves.not.toThrow(); + + const updatedProject = readProjectConfiguration(tree, 'project'); + expect(updatedProject.generators).toStrictEqual({ + '@nrwl/angular:component': { + standalone: true, + }, + }); + }); + + it('should not throw when generators defaults are not set', async () => { + const project = { + name: 'project', + root: '/', + targets: {}, + }; + addProjectConfiguration(tree, 'project', project); + + await expect( + removeLibraryGeneratorSimpleModuleNameOption(tree) + ).resolves.not.toThrow(); + + const updatedProject = readProjectConfiguration(tree, 'project'); + expect(updatedProject.generators).toBeUndefined(); + }); + }); +}); diff --git a/packages/angular/src/migrations/update-16-0-0/remove-library-generator-simple-module-name-option.ts b/packages/angular/src/migrations/update-16-0-0/remove-library-generator-simple-module-name-option.ts new file mode 100644 index 0000000000000..9c972315bc5a7 --- /dev/null +++ b/packages/angular/src/migrations/update-16-0-0/remove-library-generator-simple-module-name-option.ts @@ -0,0 +1,65 @@ +import type { + NxJsonConfiguration, + ProjectConfiguration, + Tree, +} from '@nrwl/devkit'; +import { + formatFiles, + getProjects, + readNxJson, + updateNxJson, + updateProjectConfiguration, +} from '@nrwl/devkit'; + +export default async function removeLibraryGeneratorSimpleModuleNameOption( + tree: Tree +): Promise { + const nxJson = readNxJson(tree); + + // update global config + const nxJsonUpdated = replaceSimpleModuleNameInConfig(nxJson); + if (nxJsonUpdated) { + updateNxJson(tree, nxJson); + } + + // update project configs + const projects = getProjects(tree); + for (const [name, project] of projects) { + const projectUpdated = replaceSimpleModuleNameInConfig(project); + if (projectUpdated) { + updateProjectConfiguration(tree, name, project); + } + } + + await formatFiles(tree); +} + +function replaceSimpleModuleNameInConfig( + configObject: { + generators?: + | NxJsonConfiguration['generators'] + | ProjectConfiguration['generators']; + } | null +): boolean { + if (!configObject?.generators) { + return false; + } + + let updated = false; + + if (configObject.generators['@nrwl/angular']?.['library']?.simpleModuleName) { + configObject.generators['@nrwl/angular']['library'].simpleName ??= + configObject.generators['@nrwl/angular']['library'].simpleModuleName; + delete configObject.generators['@nrwl/angular']['library'].simpleModuleName; + updated = true; + } else if ( + configObject.generators['@nrwl/angular:library']?.simpleModuleName + ) { + configObject.generators['@nrwl/angular:library'].simpleName ??= + configObject.generators['@nrwl/angular:library'].simpleModuleName; + delete configObject.generators['@nrwl/angular:library'].simpleModuleName; + updated = true; + } + + return updated; +}