Skip to content

Commit 3bc52df

Browse files
devversionmmalerba
authored andcommitted
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: angular/angular-cli@729d407
1 parent e0f3ae7 commit 3bc52df

File tree

7 files changed

+16
-105
lines changed

7 files changed

+16
-105
lines changed

src/cdk/schematics/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
export * from './ast';
1010
export * from './build-component';
1111
export * from './get-project';
12-
export * from './package-json';
1312
export * from './parse5-element';
1413
export * from './project-main-file';
1514
export * from './project-style-file';

src/lib/schematics/ng-add/index.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,6 @@ describe('ng-add schematic', () => {
4444
expect(runner.tasks.some(task => task.name === 'run-schematic')).toBe(true);
4545
});
4646

47-
it('should not set up dependencies if skipPackageJson is specified', () => {
48-
const tree = runner.runSchematic('ng-add', {skipPackageJson: true}, appTree);
49-
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
50-
const dependencies = packageJson.dependencies;
51-
52-
expect(dependencies['@angular/material']).toBeUndefined();
53-
expect(dependencies['@angular/cdk']).toBeUndefined();
54-
55-
expect(runner.tasks.some(task => task.name === 'run-schematic')).toBe(true);
56-
});
57-
5847
it('should add hammerjs import to project main file', () => {
5948
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
6049
const fileContent = getFileContent(tree, '/projects/material/src/main.ts');

src/lib/schematics/ng-add/index.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Rule, Tree, SchematicContext, TaskId} from '@angular-devkit/schematics';
9+
import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
1010
import {NodePackageInstallTask, RunSchematicTask} from '@angular-devkit/schematics/tasks';
11-
import {addPackageToPackageJson, getPackageVersionFromPackageJson} from './package-json';
11+
import {addPackageToPackageJson, getPackageVersionFromPackageJson} from './package-config';
1212
import {Schema} from './schema';
1313
import {hammerjsVersion, materialVersion, requiredAngularVersionRange} from './version-names';
1414

@@ -21,32 +21,24 @@ import {hammerjsVersion, materialVersion, requiredAngularVersionRange} from './v
2121
*/
2222
export default function(options: Schema): Rule {
2323
return (host: Tree, context: SchematicContext) => {
24-
// Since the Angular Material schematics depend on the schematic utility functions from the
25-
// CDK, we need to install the CDK before loading the schematic files that import from the CDK.
26-
let installTaskId: TaskId;
27-
28-
if (!options.skipPackageJson) {
29-
// Version tag of the `@angular/core` dependency that has been loaded from the `package.json`
30-
// of the CLI project. This tag should be preferred because all Angular dependencies should
31-
// have the same version tag if possible.
32-
const ngCoreVersionTag = getPackageVersionFromPackageJson(host, '@angular/core');
33-
34-
addPackageToPackageJson(host, '@angular/cdk', `^${materialVersion}`);
35-
addPackageToPackageJson(host, '@angular/material', `^${materialVersion}`);
36-
addPackageToPackageJson(host, '@angular/animations',
37-
ngCoreVersionTag || requiredAngularVersionRange);
24+
// Version tag of the `@angular/core` dependency that has been loaded from the `package.json`
25+
// of the CLI project. This tag should be preferred because all Angular dependencies should
26+
// have the same version tag if possible.
27+
const ngCoreVersionTag = getPackageVersionFromPackageJson(host, '@angular/core');
3828

39-
if (options.gestures) {
40-
addPackageToPackageJson(host, 'hammerjs', hammerjsVersion);
41-
}
29+
addPackageToPackageJson(host, '@angular/cdk', `^${materialVersion}`);
30+
addPackageToPackageJson(host, '@angular/material', `^${materialVersion}`);
31+
addPackageToPackageJson(host, '@angular/animations',
32+
ngCoreVersionTag || requiredAngularVersionRange);
4233

43-
installTaskId = context.addTask(new NodePackageInstallTask());
44-
} else {
45-
installTaskId = context.addTask(new NodePackageInstallTask({
46-
packageName: `@angular/cdk@^${materialVersion}`
47-
}));
34+
if (options.gestures) {
35+
addPackageToPackageJson(host, 'hammerjs', hammerjsVersion);
4836
}
4937

38+
// Since the Angular Material schematics depend on the schematic utility functions from the
39+
// CDK, we need to install the CDK before loading the schematic files that import from the CDK.
40+
const installTaskId = context.addTask(new NodePackageInstallTask());
41+
5042
context.addTask(new RunSchematicTask('ng-add-setup-project', options), [installTaskId]);
5143
};
5244
}

src/lib/schematics/ng-add/package-json.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/lib/schematics/ng-add/schema.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
"$source": "projectName"
1212
}
1313
},
14-
"skipPackageJson": {
15-
"type": "boolean",
16-
"default": false,
17-
"description": "Do not add materials dependencies to package.json (e.g., --skipPackageJson)"
18-
},
1914
"theme": {
2015
"enum": ["indigo-pink", "deeppurple-amber", "pink-bluegrey", "purple-green", "custom"],
2116
"default": "indigo-pink",

src/lib/schematics/ng-add/schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ export interface Schema {
1111
/** Name of the project to target. */
1212
project: string;
1313

14-
/** Whether to skip package.json install. */
15-
skipPackageJson: boolean;
16-
1714
/** Whether gesture support should be set up or not. */
1815
gestures: boolean;
1916

0 commit comments

Comments
 (0)