From 3041b0a227e975f7890b2192bc156beecc9ecbdd Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Fri, 13 Jan 2023 19:04:47 +0000 Subject: [PATCH] fix(angular): correctly error application when standalone is used in Angular < 14.1.0 (#14338) --- .../angular/generators/application.json | 2 +- .../application/application.spec.ts | 21 +++++++++++++++++++ .../src/generators/application/application.ts | 14 ++++++++++++- .../src/generators/application/schema.json | 2 +- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/docs/generated/packages/angular/generators/application.json b/docs/generated/packages/angular/generators/application.json index 2c22aa6bc7fc6..5177cc464fcf2 100644 --- a/docs/generated/packages/angular/generators/application.json +++ b/docs/generated/packages/angular/generators/application.json @@ -140,7 +140,7 @@ "default": false }, "standalone": { - "description": "Generate an application that is setup to use standalone components.", + "description": "Generate an application that is setup to use standalone components. _Note: This is only supported in Angular versions >= 14.1.0_", "type": "boolean", "default": false }, diff --git a/packages/angular/src/generators/application/application.spec.ts b/packages/angular/src/generators/application/application.spec.ts index 5c742049778e0..f6c2d68c4d990 100644 --- a/packages/angular/src/generators/application/application.spec.ts +++ b/packages/angular/src/generators/application/application.spec.ts @@ -7,6 +7,7 @@ import { readJson, readNxJson, readProjectConfiguration, + stripIndents, updateJson, } from '@nrwl/devkit'; import { @@ -1069,6 +1070,26 @@ describe('app', () => { expect(project.targets.build.options['outputPath']).toBe('dist/my-app'); }); }); + + 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', + }, + })); + + // ACT & ASSERT + await expect( + generateApp(tree, 'my-app', { + 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.`); + }); }); async function generateApp( diff --git a/packages/angular/src/generators/application/application.ts b/packages/angular/src/generators/application/application.ts index bf2c035770664..01a755299734c 100644 --- a/packages/angular/src/generators/application/application.ts +++ b/packages/angular/src/generators/application/application.ts @@ -3,6 +3,7 @@ import { installPackagesTask, moveFilesToNewDirectory, readNxJson, + stripIndents, Tree, updateNxJson, } from '@nrwl/devkit'; @@ -12,7 +13,10 @@ import { join } from 'path'; import { UnitTestRunner } from '../../utils/test-runners'; import { angularInitGenerator } from '../init/init'; import { setupTailwindGenerator } from '../setup-tailwind/setup-tailwind'; -import { getGeneratorDirectoryForInstalledAngularVersion } from '../utils/angular-version-utils'; +import { + getGeneratorDirectoryForInstalledAngularVersion, + getInstalledAngularVersionInfo, +} from '../utils/angular-version-utils'; import { addE2e, addLinting, @@ -31,11 +35,19 @@ import { updateNxComponentTemplate, } from './lib'; import type { Schema } from './schema'; +import { lt } from 'semver'; export async function applicationGenerator( tree: Tree, schema: Partial ) { + const installedAngularVersionInfo = getInstalledAngularVersionInfo(tree); + + if (lt(installedAngularVersionInfo.version, '14.1.0') && schema.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 generatorDirectory = getGeneratorDirectoryForInstalledAngularVersion(tree); if (generatorDirectory) { diff --git a/packages/angular/src/generators/application/schema.json b/packages/angular/src/generators/application/schema.json index 70c7476ea63f0..ecf29aef3cb07 100644 --- a/packages/angular/src/generators/application/schema.json +++ b/packages/angular/src/generators/application/schema.json @@ -143,7 +143,7 @@ "default": false }, "standalone": { - "description": "Generate an application that is setup to use standalone components.", + "description": "Generate an application that is setup to use standalone components. _Note: This is only supported in Angular versions >= 14.1.0_", "type": "boolean", "default": false },