Skip to content

Commit

Permalink
fix(angular): correctly error application when standalone is used in …
Browse files Browse the repository at this point in the history
…Angular < 14.1.0 (#14338)
  • Loading branch information
Coly010 authored Jan 13, 2023
1 parent 0db5073 commit 3041b0a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
21 changes: 21 additions & 0 deletions packages/angular/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
readJson,
readNxJson,
readProjectConfiguration,
stripIndents,
updateJson,
} from '@nrwl/devkit';
import {
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 13 additions & 1 deletion packages/angular/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
installPackagesTask,
moveFilesToNewDirectory,
readNxJson,
stripIndents,
Tree,
updateNxJson,
} from '@nrwl/devkit';
Expand All @@ -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,
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down

1 comment on commit 3041b0a

@vercel
Copy link

@vercel vercel bot commented on 3041b0a Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx.dev
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app

Please sign in to comment.