-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup(angular): add util for finding the directory name based on an…
…gular version (#13539)
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
packages/angular/src/utils/get-generator-directory-for-ng-version.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/angular/src/utils/get-generator-directory-for-ng-version.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |