/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics'; import {NodePackageInstallTask, RunSchematicTask} from '@angular-devkit/schematics/tasks'; import {addPackageToPackageJson, getPackageVersionFromPackageJson} from './package-config'; import {Schema} from './schema'; import {hammerjsVersion, materialVersion, requiredAngularVersionRange} from './version-names'; /** * Schematic factory entry-point for the `ng-add` schematic. The ng-add schematic will be * automatically executed if developers run `ng add @angular/material`. * * Since the Angular Material schematics depend on the schematic utility functions from the CDK, * we need to install the CDK before loading the schematic files that import from the CDK. */ export default function(options: Schema): Rule { return (host: Tree, context: SchematicContext) => { // Version tag of the `@angular/core` dependency that has been loaded from the `package.json` // of the CLI project. This tag should be preferred because all Angular dependencies should // have the same version tag if possible. const ngCoreVersionTag = getPackageVersionFromPackageJson(host, '@angular/core'); const angularDependencyVersion = ngCoreVersionTag || requiredAngularVersionRange; // In order to align the Material and CDK version with the other Angular dependencies, // we use tilde instead of caret. This is default for Angular dependencies in new CLI projects. addPackageToPackageJson(host, '@angular/cdk', `~${materialVersion}`); addPackageToPackageJson(host, '@angular/material', `~${materialVersion}`); addPackageToPackageJson(host, '@angular/forms', angularDependencyVersion); addPackageToPackageJson(host, '@angular/animations', angularDependencyVersion); if (options.gestures) { addPackageToPackageJson(host, 'hammerjs', hammerjsVersion); } // Since the Angular Material schematics depend on the schematic utility functions from the // CDK, we need to install the CDK before loading the schematic files that import from the CDK. const installTaskId = context.addTask(new NodePackageInstallTask()); context.addTask(new RunSchematicTask('ng-add-setup-project', options), [installTaskId]); }; }