From 312351b03c5f4b9c34358ba5ddd6aab6c9550386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Mon, 25 Sep 2023 22:09:18 +0200 Subject: [PATCH] fix(angular): throw an error when generating a component and the specified module can't be found (#19324) --- .../generators/component/component.spec.ts | 31 +++++++++++++++++++ .../src/generators/component/lib/module.ts | 14 ++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/packages/angular/src/generators/component/component.spec.ts b/packages/angular/src/generators/component/component.spec.ts index 03834e2b5b7e7..c425e77f72035 100644 --- a/packages/angular/src/generators/component/component.spec.ts +++ b/packages/angular/src/generators/component/component.spec.ts @@ -781,6 +781,37 @@ describe('component Generator', () => { `); }); + it('should throw an error when the module is not found', async () => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); + addProjectConfiguration(tree, 'lib1', { + projectType: 'library', + sourceRoot: 'libs/lib1/src', + root: 'libs/lib1', + }); + tree.write( + 'libs/lib1/src/lib/lib.module.ts', + ` + import { NgModule } from '@angular/core'; + + @NgModule({ + declarations: [], + exports: [] + }) + export class LibModule {}` + ); + + // ACT & ASSERT + await expect( + componentGenerator(tree, { + name: 'example', + project: 'lib1', + path: 'libs/lib1/src/lib', + module: 'not-found', + }) + ).rejects.toThrow(); + }); + it('should throw an error when there are more than one candidate modules that the component can be added to', async () => { // ARRANGE const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); diff --git a/packages/angular/src/generators/component/lib/module.ts b/packages/angular/src/generators/component/lib/module.ts index a1e46bc18c31f..e6648666f531b 100644 --- a/packages/angular/src/generators/component/lib/module.ts +++ b/packages/angular/src/generators/component/lib/module.ts @@ -3,9 +3,8 @@ import { joinPathFragments, normalizePath } from '@nx/devkit'; import { basename, dirname } from 'path'; import type { NormalizedSchema } from '../schema'; -// Adapted from https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/utility/find-module.ts#L29 -// to match the logic in the component schematic. It doesn't throw if it can't -// find a module since the schematic would have thrown before getting here. +// Adapted from https://github.com/angular/angular-cli/blob/732aab5fa7e63618c89dfbbb6f78753f706d7014/packages/schematics/angular/utility/find-module.ts#L29 +// to match the logic from the Angular CLI component schematic. const moduleExt = '.module.ts'; const routingModuleExt = '-routing.module.ts'; @@ -13,7 +12,7 @@ export function findModuleFromOptions( tree: Tree, options: NormalizedSchema, projectRoot: string -): string | null { +): string { if (!options.module) { return normalizePath(findModule(tree, options.directory, projectRoot)); } else { @@ -49,7 +48,12 @@ export function findModuleFromOptions( } } - return null; + throw new Error( + `Specified module '${options.module}' does not exist.\n` + + `Looked in the following directories:\n ${candidatesDirs.join( + '\n ' + )}` + ); } }