refactor(schematics): remove ng-add skipPackageJson option (#13233)

* 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
This commit is contained in:
Paul Gschwendtner
2018-09-21 19:21:17 +02:00
committed by mmalerba
parent e0f3ae76a7
commit 3bc52df940
7 changed files with 17 additions and 106 deletions
-1
View File
@@ -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';
-11
View File
@@ -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');
+17 -25
View File
@@ -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,31 +21,23 @@ import {hammerjsVersion, materialVersion, requiredAngularVersionRange} from './v
*/
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');
addPackageToPackageJson(host, '@angular/cdk', `^${materialVersion}`);
addPackageToPackageJson(host, '@angular/material', `^${materialVersion}`);
addPackageToPackageJson(host, '@angular/animations',
ngCoreVersionTag || requiredAngularVersionRange);
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.
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);
if (options.gestures) {
addPackageToPackageJson(host, 'hammerjs', hammerjsVersion);
}
installTaskId = context.addTask(new NodePackageInstallTask());
} else {
installTaskId = context.addTask(new NodePackageInstallTask({
packageName: `@angular/cdk@^${materialVersion}`
}));
}
const installTaskId = context.addTask(new NodePackageInstallTask());
context.addTask(new RunSchematicTask('ng-add-setup-project', options), [installTaskId]);
};
-61
View File
@@ -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;
}
-5
View File
@@ -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",
-3
View File
@@ -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;