From b36c0c96ec55aa93764ae5b46e932267495d9a8b Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 1 Jun 2021 10:24:35 +0200 Subject: [PATCH] fix(@schematics/angular): show better error when non existing project is passed to the component schematic Closes: #21003 (cherry picked from commit 7cd801eb066543fe751ac6b7840c279e37b6e74a) --- packages/schematics/angular/component/index.ts | 6 +++++- packages/schematics/angular/directive/index.ts | 2 +- packages/schematics/angular/library/index.ts | 6 +++--- packages/schematics/angular/utility/workspace.ts | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/schematics/angular/component/index.ts b/packages/schematics/angular/component/index.ts index 9737310d659d..fa8315bce3f1 100644 --- a/packages/schematics/angular/component/index.ts +++ b/packages/schematics/angular/component/index.ts @@ -116,7 +116,11 @@ export default function (options: ComponentOptions): Rule { const workspace = await getWorkspace(host); const project = workspace.projects.get(options.project as string); - if (options.path === undefined && project) { + if (!project) { + throw new SchematicsException(`Project "${options.project}" does not exist.`); + } + + if (options.path === undefined) { options.path = buildDefaultPath(project); } diff --git a/packages/schematics/angular/directive/index.ts b/packages/schematics/angular/directive/index.ts index 9b06fab3d5f3..037c003de32a 100644 --- a/packages/schematics/angular/directive/index.ts +++ b/packages/schematics/angular/directive/index.ts @@ -110,7 +110,7 @@ export default function (options: DirectiveOptions): Rule { const workspace = await getWorkspace(host); const project = workspace.projects.get(options.project as string); if (!project) { - throw new SchematicsException(`Invalid project name (${options.project})`); + throw new SchematicsException(`Project "${options.project}" does not exist.`); } if (options.path === undefined) { diff --git a/packages/schematics/angular/library/index.ts b/packages/schematics/angular/library/index.ts index b2a89f929547..40ff8b1a4583 100644 --- a/packages/schematics/angular/library/index.ts +++ b/packages/schematics/angular/library/index.ts @@ -179,7 +179,7 @@ export default function (options: LibraryOptions): Rule { commonModule: false, flat: true, path: sourceDir, - project: options.name, + project: projectName, }), schematic('component', { name: options.name, @@ -189,13 +189,13 @@ export default function (options: LibraryOptions): Rule { flat: true, path: sourceDir, export: true, - project: options.name, + project: projectName, }), schematic('service', { name: options.name, flat: true, path: sourceDir, - project: options.name, + project: projectName, }), options.lintFix ? applyLintFix(sourceDir) : noop(), (_tree: Tree, context: SchematicContext) => { diff --git a/packages/schematics/angular/utility/workspace.ts b/packages/schematics/angular/utility/workspace.ts index c5fae05d28a0..07bf2c9ffc64 100644 --- a/packages/schematics/angular/utility/workspace.ts +++ b/packages/schematics/angular/utility/workspace.ts @@ -85,7 +85,7 @@ export async function createDefaultPath(tree: Tree, projectName: string): Promis const workspace = await getWorkspace(tree); const project = workspace.projects.get(projectName); if (!project) { - throw new Error('Specified project does not exist.'); + throw new Error(`Project "${projectName}" does not exist.`); } return buildDefaultPath(project);