Skip to content

Commit 6b9d1c7

Browse files
committed
feat(angular): add new helper getAngularVersion
1 parent 5bdf568 commit 6b9d1c7

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

projects/docs/docs/apis/angular.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,23 @@ export default (options: any): Rule =>
474474

475475
## Helpers
476476

477+
### `getAngularVersion`
478+
479+
Gets the version of Angular currently used in the project.
480+
481+
```ts {6}
482+
import { getAngularVersion, schematic } from '@hug/ngx-schematics-utilities';
483+
import { Rule, Tree } from '@angular-devkit/schematics';
484+
485+
export default (options: any): Rule =>
486+
(tree: Tree): Rule => {
487+
const ngVersion = getAngularVersion();
488+
return schematic('my-schematic', [
489+
...
490+
]);
491+
};
492+
```
493+
477494
### `getProjectOutputPath`
478495

479496
Gets a project output path as defined in the `angular.json` file.

projects/lib/specs/angular.spec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { JSONFile } from '@schematics/angular/utility/json-file';
55
import { join } from 'path';
66

77
import {
8-
addAngularJsonAsset, addAngularJsonScript, addAngularJsonStyle, addDeclarationToNgModule, addExportToNgModule, addImportToNgModule,
9-
addProviderToBootstrapApplication, addProviderToNgModule, addRouteDeclarationToNgModule, ensureIsAngularApplication,
10-
ensureIsAngularLibrary, ensureIsAngularWorkspace, getProjectFromWorkspace, getProjectMainFilePath, getProjectOutputPath,
11-
isAngularVersion, isProjectStandalone, removeAngularJsonAsset, removeAngularJsonScript, removeAngularJsonStyle,
12-
removeDeclarationFromNgModule, removeExportFromNgModule, removeImportFromNgModule, removeProviderFromBootstrapApplication,
13-
removeProviderFromNgModule
8+
addAngularJsonAsset, addAngularJsonScript, addAngularJsonStyle, addDeclarationToNgModule, addExportToNgModule,
9+
addImportToNgModule, addProviderToBootstrapApplication, addProviderToNgModule, addRouteDeclarationToNgModule,
10+
ensureIsAngularApplication, ensureIsAngularLibrary, ensureIsAngularWorkspace, getAngularVersion, getProjectFromWorkspace,
11+
getProjectMainFilePath, getProjectOutputPath, isAngularVersion, isProjectStandalone, removeAngularJsonAsset,
12+
removeAngularJsonScript, removeAngularJsonStyle, removeDeclarationFromNgModule, removeExportFromNgModule,
13+
removeImportFromNgModule, removeProviderFromBootstrapApplication, removeProviderFromNgModule
1414
} from '../src';
1515
import { appTest1, appTest2, callRule, getCleanAppTree, libTest } from './common.spec';
1616
import { customMatchers } from './jasmine.matchers';
@@ -621,6 +621,10 @@ const expectAddToNgModule = async (
621621
});
622622
}
623623

624+
it('helper: getAngularVersion', () => {
625+
expect(getAngularVersion()).toBeDefined();
626+
});
627+
624628
it('helper: getProjectOutputPath', () => {
625629
expect(getProjectOutputPath(tree, appTest1.name)).toEqual(`dist/${appTest1.name}`);
626630
if (useWorkspace) {

projects/lib/src/angular.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { VERSION, Version } from '@angular/core';
12
import { JsonObject, JsonValue } from '@angular-devkit/core';
23
import { noop, Rule, SchematicsException, Tree } from '@angular-devkit/schematics';
34
import {
@@ -73,11 +74,9 @@ export const ensureIsAngularLibrary = (projectName: string): Rule =>
7374
* @returns {Rule}
7475
*/
7576
export const isAngularVersion = (range: string, rule: Rule): Rule =>
76-
async (): Promise<Rule> => {
77+
(): Rule => {
7778
try {
78-
const angularPkgJsonPath = require.resolve(join('@angular/core', 'package.json'), { paths: ['.'] });
79-
const ngVersion = (await import(angularPkgJsonPath) as { version: string }).version;
80-
return (satisfies(ngVersion, range)) ? rule : noop();
79+
return (satisfies(getAngularVersion().full, range)) ? rule : noop();
8180
} catch {
8281
return noop();
8382
}
@@ -343,6 +342,14 @@ export const removeProviderFromBootstrapApplication = (filePath: string, provide
343342
removeProviderFromStandaloneApplication(tree, filePath, providerName);
344343
};
345344

345+
// --- HELPER(s) ---
346+
347+
/**
348+
* Gets the version of Angular currently used in the project.
349+
* @returns {Version}
350+
*/
351+
export const getAngularVersion = (): Version => VERSION;
352+
346353
/**
347354
* Gets a project output path as defined in the `angular.json` file.
348355
* @param {Tree} tree The current schematic's project tree.
@@ -453,8 +460,6 @@ export const ensureProjectIsDefined = (projectName: string | undefined): void =>
453460
}
454461
};
455462

456-
// --- HELPER(s) ---
457-
458463
const customizeAngularJsonBuildAndTestSection = (action: 'add' | 'remove', option: string, tree: Tree, value: JsonValue, projectName: string): void => {
459464
const angularJson = new JSONFile(tree, 'angular.json');
460465
const architectPath = ['projects', projectName, 'architect'];

0 commit comments

Comments
 (0)