diff --git a/packages/angular/src/utils/get-generator-directory-for-ng-version.spec.ts b/packages/angular/src/utils/get-generator-directory-for-ng-version.spec.ts new file mode 100644 index 0000000000000..aef2948a084a7 --- /dev/null +++ b/packages/angular/src/utils/get-generator-directory-for-ng-version.spec.ts @@ -0,0 +1,47 @@ +import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; +import { updateJson } from '@nrwl/devkit'; +import { getGeneratorDirectoryForInstalledAngularVersion } from './get-generator-directory-for-ng-version'; + +describe('getGeneratorDirectoryForAngularVersion', () => { + test.each(['14.0.0', '~14.1.0', '^14.2.0', '~14.3.0-beta.0'])( + 'should return correct directory name for v14', + (ngVersion) => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace(); + updateJson(tree, 'package.json', (json) => ({ + ...json, + dependencies: { + '@angular/core': ngVersion, + }, + })); + + // ACT + const directoryName = + getGeneratorDirectoryForInstalledAngularVersion(tree); + + // ASSERT + expect(directoryName).toEqual('angular-v14'); + } + ); + + test.each(['15.0.0', '~15.1.0', '^13.2.0', '~15.3.0-beta.0'])( + 'should return null for anything other than v14', + (ngVersion) => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace(); + updateJson(tree, 'package.json', (json) => ({ + ...json, + dependencies: { + '@angular/core': ngVersion, + }, + })); + + // ACT + const directoryName = + getGeneratorDirectoryForInstalledAngularVersion(tree); + + // ASSERT + expect(directoryName).toBe(null); + } + ); +}); diff --git a/packages/angular/src/utils/get-generator-directory-for-ng-version.ts b/packages/angular/src/utils/get-generator-directory-for-ng-version.ts new file mode 100644 index 0000000000000..86ea8a2af8cd0 --- /dev/null +++ b/packages/angular/src/utils/get-generator-directory-for-ng-version.ts @@ -0,0 +1,16 @@ +import type { Tree } from '@nrwl/devkit'; +import { readJson } from '@nrwl/devkit'; +import { coerce, major } from 'semver'; + +export function getGeneratorDirectoryForInstalledAngularVersion(tree: Tree) { + const pkgJson = readJson(tree, 'package.json'); + const angularVersion = pkgJson.dependencies['@angular/core']; + + const majorAngularVersion = major(coerce(angularVersion)); + + const directoryDictionary = { + 14: 'angular-v14', + }; + + return directoryDictionary[majorAngularVersion] ?? null; +}