fix(angular): correctly error application when standalone is used in Angular < 14.1.0 (#14338)

This commit is contained in:
Colum Ferry 2023-01-13 19:04:47 +00:00 committed by GitHub
parent 0db507394e
commit 3041b0a227
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 3 deletions

View File

@ -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
},

View File

@ -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(

View File

@ -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<Schema>
) {
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) {

View File

@ -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
},