From 46b10c2e2050ebc6470af525b2ade3675d17f1ef Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Fri, 13 Jan 2023 12:57:19 +0000 Subject: [PATCH] fix(angular): correctly error component when standalone is used in Angular < 14.1.0 (#14337) --- .../angular/generators/component.json | 2 +- .../generators/component/component.spec.ts | 35 ++++++++++++++++++- .../src/generators/component/component.ts | 13 +++++++ .../src/generators/component/schema.json | 2 +- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/docs/generated/packages/angular/generators/component.json b/docs/generated/packages/angular/generators/component.json index c6ac4b0b15c79..d38800397a50b 100644 --- a/docs/generated/packages/angular/generators/component.json +++ b/docs/generated/packages/angular/generators/component.json @@ -46,7 +46,7 @@ "alias": "t" }, "standalone": { - "description": "Whether the generated component is standalone.", + "description": "Whether the generated component is standalone. _Note: This is only supported in Angular versions >= 14.1.0_", "type": "boolean", "default": false }, diff --git a/packages/angular/src/generators/component/component.spec.ts b/packages/angular/src/generators/component/component.spec.ts index eb6122e36e9e9..9f3b0875fd00e 100644 --- a/packages/angular/src/generators/component/component.spec.ts +++ b/packages/angular/src/generators/component/component.spec.ts @@ -1,5 +1,10 @@ import type { ProjectGraph } from '@nrwl/devkit'; -import { addProjectConfiguration, writeJson } from '@nrwl/devkit'; +import { + addProjectConfiguration, + stripIndents, + updateJson, + writeJson, +} from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import componentGenerator from './component'; @@ -737,4 +742,32 @@ describe('component Generator', () => { expect(indexSource).toBe(''); }); }); + + it('should error correctly when Angular version does not support standalone', async () => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); + updateJson(tree, 'package.json', (json) => ({ + ...json, + dependencies: { + '@angular/core': '14.0.0', + }, + })); + + addProjectConfiguration(tree, 'lib1', { + projectType: 'library', + sourceRoot: 'libs/lib1/src', + root: 'libs/lib1', + }); + + // ACT & ASSERT + await expect( + componentGenerator(tree, { + name: 'example', + project: 'lib1', + standalone: true, + }) + ).rejects + .toThrow(stripIndents`The "standalone" option is only supported in Angular >= 14.1.0. You are currently using 14.0.0. + You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`); + }); }); diff --git a/packages/angular/src/generators/component/component.ts b/packages/angular/src/generators/component/component.ts index b1b59c82da59c..514f9e26568d1 100644 --- a/packages/angular/src/generators/component/component.ts +++ b/packages/angular/src/generators/component/component.ts @@ -4,14 +4,27 @@ import { normalizePath, readNxJson, readProjectConfiguration, + stripIndents, } from '@nrwl/devkit'; import { wrapAngularDevkitSchematic } from '@nrwl/devkit/ngcli-adapter'; import { pathStartsWith } from '../utils/path'; import { exportComponentInEntryPoint } from './lib/component'; import { normalizeOptions } from './lib/normalize-options'; import type { NormalizedSchema, Schema } from './schema'; +import { getInstalledAngularVersionInfo } from '../utils/angular-version-utils'; +import { lt } from 'semver'; export async function componentGenerator(tree: Tree, rawOptions: Schema) { + const installedAngularVersionInfo = getInstalledAngularVersionInfo(tree); + + if ( + lt(installedAngularVersionInfo.version, '14.1.0') && + rawOptions.standalone + ) { + throw new Error(stripIndents`The "standalone" option is only supported in Angular >= 14.1.0. You are currently using ${installedAngularVersionInfo.version}. + You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`); + } + const options = await normalizeOptions(tree, rawOptions); const { projectSourceRoot, ...schematicOptions } = options; diff --git a/packages/angular/src/generators/component/schema.json b/packages/angular/src/generators/component/schema.json index 05f5e87b9cf4e..1840950a7680a 100644 --- a/packages/angular/src/generators/component/schema.json +++ b/packages/angular/src/generators/component/schema.json @@ -46,7 +46,7 @@ "alias": "t" }, "standalone": { - "description": "Whether the generated component is standalone.", + "description": "Whether the generated component is standalone. _Note: This is only supported in Angular versions >= 14.1.0_", "type": "boolean", "default": false },