fix(angular): correctly error scam-to-standalone when used in Angular < 14.1.0 (#14341)

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

View File

@ -6,7 +6,7 @@
"$id": "GeneratorAngularScamToStandalone",
"cli": "nx",
"title": "Convert an Inline SCAM to Standalone Component",
"description": "Convert an Inline SCAM to a Standalone Component.",
"description": "Convert an Inline SCAM to a Standalone Component. _Note: This generator is only supported with Angular versions >= 14.1.0_.",
"type": "object",
"properties": {
"component": {

View File

@ -2,6 +2,7 @@ import { scamToStandalone } from './scam-to-standalone';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import applicationGenerator from '../application/application';
import scamGenerator from '../scam/scam';
import { stripIndents, updateJson } from '@nrwl/devkit';
describe('scam-to-standalone', () => {
it('should convert an inline scam to standalone', async () => {
@ -80,4 +81,25 @@ describe('scam-to-standalone', () => {
"
`);
});
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(
scamToStandalone(tree, {
component: 'src/app/bar/bar.component.ts',
project: 'foo',
})
).rejects
.toThrow(stripIndents`This generator is only supported with Angular >= 14.1.0. You are currently using 14.0.0.
You can resolve this error by migrating to Angular 14.1.0.`);
});
});

View File

@ -1,5 +1,10 @@
import type { Tree } from '@nrwl/devkit';
import { formatFiles, getProjects, joinPathFragments } from '@nrwl/devkit';
import {
formatFiles,
getProjects,
joinPathFragments,
stripIndents,
} from '@nrwl/devkit';
import type { Schema } from './schema';
import {
convertScamToStandalone,
@ -10,11 +15,20 @@ import {
verifyIsInlineScam,
verifyModuleIsScam,
} from './lib';
import { getInstalledAngularVersionInfo } from '../utils/angular-version-utils';
import { lt } from 'semver';
export async function scamToStandalone(
tree: Tree,
{ component, project: projectName, skipFormat }: Schema
) {
const installedAngularVersionInfo = getInstalledAngularVersionInfo(tree);
if (lt(installedAngularVersionInfo.version, '14.1.0')) {
throw new Error(stripIndents`This generator is only supported with Angular >= 14.1.0. You are currently using ${installedAngularVersionInfo.version}.
You can resolve this error by migrating to Angular 14.1.0.`);
}
const projects = getProjects(tree);
let project = getTargetProject(projectName, projects);

View File

@ -3,7 +3,7 @@
"$id": "GeneratorAngularScamToStandalone",
"cli": "nx",
"title": "Convert an Inline SCAM to Standalone Component",
"description": "Convert an Inline SCAM to a Standalone Component.",
"description": "Convert an Inline SCAM to a Standalone Component. _Note: This generator is only supported with Angular versions >= 14.1.0_.",
"type": "object",
"properties": {
"component": {