Skip to content

Commit

Permalink
fix(angular): correctly error scam-to-standalone when used in Angular…
Browse files Browse the repository at this point in the history
… < 14.1.0 (#14341)
  • Loading branch information
Coly010 authored Jan 13, 2023
1 parent 3041b0a commit 81211bf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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.`);
});
});
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 81211bf

Please sign in to comment.