From 36d729b31cd674dc633c0d85e994fc488c9b29e9 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Fri, 21 Sep 2018 08:50:43 +0200 Subject: [PATCH] refactor(schematics): remove ng-add skipPackageJson option * Removes the `skipPackageJson` option from the `ng-add` schematic. The option does not work properly because `ng add` by default saves the dependency to the `package.json` file. Also we have no way to disable saving when installing the CDK through the `NodePackageInstallTask`. See for reference: https://github.com/angular/angular-cli/commit/729d407fa1877d60d1e44efa3d657dc3ab4279e1 --- src/cdk/schematics/utils/index.ts | 1 - src/lib/schematics/ng-add/index.spec.ts | 11 ---- src/lib/schematics/ng-add/index.ts | 40 +++++------- .../schematics/ng-add/package-config.ts} | 0 src/lib/schematics/ng-add/package-json.ts | 61 ------------------- src/lib/schematics/ng-add/schema.json | 5 -- src/lib/schematics/ng-add/schema.ts | 3 - 7 files changed, 16 insertions(+), 105 deletions(-) rename src/{cdk/schematics/utils/package-json.ts => lib/schematics/ng-add/package-config.ts} (100%) delete mode 100644 src/lib/schematics/ng-add/package-json.ts diff --git a/src/cdk/schematics/utils/index.ts b/src/cdk/schematics/utils/index.ts index 3924774a2c25..2313a73416d0 100644 --- a/src/cdk/schematics/utils/index.ts +++ b/src/cdk/schematics/utils/index.ts @@ -9,7 +9,6 @@ export * from './ast'; export * from './build-component'; export * from './get-project'; -export * from './package-json'; export * from './parse5-element'; export * from './project-main-file'; export * from './project-style-file'; diff --git a/src/lib/schematics/ng-add/index.spec.ts b/src/lib/schematics/ng-add/index.spec.ts index 0242b53b7cd0..26fce26c166a 100644 --- a/src/lib/schematics/ng-add/index.spec.ts +++ b/src/lib/schematics/ng-add/index.spec.ts @@ -44,17 +44,6 @@ describe('ng-add schematic', () => { expect(runner.tasks.some(task => task.name === 'run-schematic')).toBe(true); }); - it('should not set up dependencies if skipPackageJson is specified', () => { - const tree = runner.runSchematic('ng-add', {skipPackageJson: true}, appTree); - const packageJson = JSON.parse(getFileContent(tree, '/package.json')); - const dependencies = packageJson.dependencies; - - expect(dependencies['@angular/material']).toBeUndefined(); - expect(dependencies['@angular/cdk']).toBeUndefined(); - - expect(runner.tasks.some(task => task.name === 'run-schematic')).toBe(true); - }); - it('should add hammerjs import to project main file', () => { const tree = runner.runSchematic('ng-add-setup-project', {}, appTree); const fileContent = getFileContent(tree, '/projects/material/src/main.ts'); diff --git a/src/lib/schematics/ng-add/index.ts b/src/lib/schematics/ng-add/index.ts index ce56ff995118..69e3deb55321 100644 --- a/src/lib/schematics/ng-add/index.ts +++ b/src/lib/schematics/ng-add/index.ts @@ -6,9 +6,9 @@ * found in the LICENSE file at https://angular.io/license */ -import {Rule, Tree, SchematicContext, TaskId} from '@angular-devkit/schematics'; +import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics'; import {NodePackageInstallTask, RunSchematicTask} from '@angular-devkit/schematics/tasks'; -import {addPackageToPackageJson, getPackageVersionFromPackageJson} from './package-json'; +import {addPackageToPackageJson, getPackageVersionFromPackageJson} from './package-config'; import {Schema} from './schema'; import {hammerjsVersion, materialVersion, requiredAngularVersionRange} from './version-names'; @@ -21,32 +21,24 @@ import {hammerjsVersion, materialVersion, requiredAngularVersionRange} from './v */ export default function(options: Schema): Rule { return (host: Tree, context: SchematicContext) => { - // 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. - let installTaskId: TaskId; - - if (!options.skipPackageJson) { - // 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'); - - addPackageToPackageJson(host, '@angular/cdk', `^${materialVersion}`); - addPackageToPackageJson(host, '@angular/material', `^${materialVersion}`); - addPackageToPackageJson(host, '@angular/animations', - ngCoreVersionTag || requiredAngularVersionRange); + // 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'); - if (options.gestures) { - addPackageToPackageJson(host, 'hammerjs', hammerjsVersion); - } + addPackageToPackageJson(host, '@angular/cdk', `^${materialVersion}`); + addPackageToPackageJson(host, '@angular/material', `^${materialVersion}`); + addPackageToPackageJson(host, '@angular/animations', + ngCoreVersionTag || requiredAngularVersionRange); - installTaskId = context.addTask(new NodePackageInstallTask()); - } else { - installTaskId = context.addTask(new NodePackageInstallTask({ - packageName: `@angular/cdk@^${materialVersion}` - })); + 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]); }; } diff --git a/src/cdk/schematics/utils/package-json.ts b/src/lib/schematics/ng-add/package-config.ts similarity index 100% rename from src/cdk/schematics/utils/package-json.ts rename to src/lib/schematics/ng-add/package-config.ts diff --git a/src/lib/schematics/ng-add/package-json.ts b/src/lib/schematics/ng-add/package-json.ts deleted file mode 100644 index a8cf8d338177..000000000000 --- a/src/lib/schematics/ng-add/package-json.ts +++ /dev/null @@ -1,61 +0,0 @@ -/** - * @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 {Tree} from '@angular-devkit/schematics'; - -/* - * This file is temporary and a duplicate version has been moved to `@angular/cdk/schematics`. - * - * We still need this file inside of the Material schematics because if people run `ng add` for - * Angular Material, the CDK is not present, and we cannot use the utility functions from there. - */ - -/** - * Sorts the keys of the given object. - * @returns A new object instance with sorted keys - */ -function sortObjectByKeys(obj: object) { - return Object.keys(obj).sort().reduce((result, key) => (result[key] = obj[key]) && result, {}); -} - -/** Adds a package to the package.json in the given host tree. */ -export function addPackageToPackageJson(host: Tree, pkg: string, version: string): Tree { - - if (host.exists('package.json')) { - const sourceText = host.read('package.json')!.toString('utf-8'); - const json = JSON.parse(sourceText); - - if (!json.dependencies) { - json.dependencies = {}; - } - - if (!json.dependencies[pkg]) { - json.dependencies[pkg] = version; - json.dependencies = sortObjectByKeys(json.dependencies); - } - - host.overwrite('package.json', JSON.stringify(json, null, 2)); - } - - return host; -} - -/** Gets the version of the specified package by looking at the package.json in the given tree. */ -export function getPackageVersionFromPackageJson(tree: Tree, name: string): string | null { - if (!tree.exists('package.json')) { - return null; - } - - const packageJson = JSON.parse(tree.read('package.json')!.toString('utf8')); - - if (packageJson.dependencies && packageJson.dependencies[name]) { - return packageJson.dependencies[name]; - } - - return null; -} diff --git a/src/lib/schematics/ng-add/schema.json b/src/lib/schematics/ng-add/schema.json index 5a3dcbd66529..d9e1cdef4f27 100644 --- a/src/lib/schematics/ng-add/schema.json +++ b/src/lib/schematics/ng-add/schema.json @@ -11,11 +11,6 @@ "$source": "projectName" } }, - "skipPackageJson": { - "type": "boolean", - "default": false, - "description": "Do not add materials dependencies to package.json (e.g., --skipPackageJson)" - }, "theme": { "enum": ["indigo-pink", "deeppurple-amber", "pink-bluegrey", "purple-green", "custom"], "default": "indigo-pink", diff --git a/src/lib/schematics/ng-add/schema.ts b/src/lib/schematics/ng-add/schema.ts index d1929dfbd534..01e537538b4c 100644 --- a/src/lib/schematics/ng-add/schema.ts +++ b/src/lib/schematics/ng-add/schema.ts @@ -11,9 +11,6 @@ export interface Schema { /** Name of the project to target. */ project: string; - /** Whether to skip package.json install. */ - skipPackageJson: boolean; - /** Whether gesture support should be set up or not. */ gestures: boolean;