Skip to content

Commit 6802423

Browse files
alan-agius4angular-robot[bot]
authored andcommitted
feat(@angular/cli): remove deprecated defaultCollection from workspace configuration
The deprecated 'defaultCollection' workspace option has been removed BREAKING CHANGE: The deprecated `defaultCollection` workspace option has been removed. Use `schematicCollections` instead. Before ```json "defaultCollection": "@angular/material" ``` After ```json "schematicCollections": ["@angular/material"] ```
1 parent d58428d commit 6802423

File tree

6 files changed

+142
-19
lines changed

6 files changed

+142
-19
lines changed

packages/angular/cli/lib/config/workspace-schema.json

-15
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
"cliOptions": {
3737
"type": "object",
3838
"properties": {
39-
"defaultCollection": {
40-
"description": "The default schematics collection to use.",
41-
"type": "string",
42-
"x-deprecated": "Use 'schematicCollections' instead."
43-
},
4439
"schematicCollections": {
4540
"type": "array",
4641
"description": "The list of schematic collections to use.",
@@ -95,11 +90,6 @@
9590
"cliGlobalOptions": {
9691
"type": "object",
9792
"properties": {
98-
"defaultCollection": {
99-
"description": "The default schematics collection to use.",
100-
"type": "string",
101-
"x-deprecated": "Use 'schematicCollections' instead."
102-
},
10393
"schematicCollections": {
10494
"type": "array",
10595
"description": "The list of schematic collections to use.",
@@ -199,11 +189,6 @@
199189
"type": "object",
200190
"properties": {
201191
"cli": {
202-
"defaultCollection": {
203-
"description": "The default schematics collection to use.",
204-
"type": "string",
205-
"x-deprecated": "Use 'schematicCollections' instead."
206-
},
207192
"schematicCollections": {
208193
"type": "array",
209194
"description": "The list of schematic collections to use.",

packages/angular/cli/src/command-builder/schematics-command-module.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,9 @@ export abstract class SchematicsCommandModule
261261
return undefined;
262262
}
263263

264-
const { schematicCollections, defaultCollection } = configSection;
264+
const { schematicCollections } = configSection;
265265
if (Array.isArray(schematicCollections)) {
266266
return new Set(schematicCollections.map((c) => resolveRelativeCollection(c)));
267-
} else if (typeof defaultCollection === 'string') {
268-
return new Set([resolveRelativeCollection(defaultCollection)]);
269267
}
270268

271269
return undefined;

packages/schematics/angular/migrations/migration-collection.json

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"version": "16.0.0",
55
"factory": "./update-16/remove-default-project-option",
66
"description": "Remove 'defaultProject' option from workspace configuration. The project to use will be determined from the current working directory."
7+
},
8+
"replace-default-collection-option": {
9+
"version": "16.0.0",
10+
"factory": "./update-16/replace-default-collection-option",
11+
"description": "Replace removed 'defaultCollection' option in workspace configuration with 'schematicCollections'."
712
}
813
}
914
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { JsonValue, isJsonObject } from '@angular-devkit/core';
10+
import { Rule } from '@angular-devkit/schematics';
11+
import { updateWorkspace } from '../../utility/workspace';
12+
13+
/** Migration to replace 'defaultCollection' option in angular.json. */
14+
export default function (): Rule {
15+
return updateWorkspace((workspace) => {
16+
// workspace level
17+
replaceDefaultCollection(workspace.extensions['cli']);
18+
19+
// Project level
20+
for (const project of workspace.projects.values()) {
21+
replaceDefaultCollection(project.extensions['cli']);
22+
}
23+
});
24+
}
25+
26+
function replaceDefaultCollection(cliExtension: JsonValue | undefined): void {
27+
if (cliExtension && isJsonObject(cliExtension) && cliExtension['defaultCollection']) {
28+
// If `schematicsCollection` defined `defaultCollection` is ignored hence no need to warn.
29+
if (!cliExtension['schematicCollections']) {
30+
cliExtension['schematicCollections'] = [cliExtension['defaultCollection']];
31+
}
32+
33+
delete cliExtension['defaultCollection'];
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { EmptyTree } from '@angular-devkit/schematics';
10+
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
11+
import { ProjectType, WorkspaceSchema } from '../../utility/workspace-models';
12+
13+
describe(`Migration to replace 'defaultCollection' option.`, () => {
14+
const schematicName = 'replace-default-collection-option';
15+
const schematicRunner = new SchematicTestRunner(
16+
'migrations',
17+
require.resolve('../migration-collection.json'),
18+
);
19+
20+
let tree: UnitTestTree;
21+
beforeEach(() => {
22+
tree = new UnitTestTree(new EmptyTree());
23+
});
24+
25+
it(`should replace 'defaultCollection' with 'schematicCollections' at the root level`, async () => {
26+
const angularConfig = {
27+
version: 1,
28+
projects: {},
29+
cli: {
30+
defaultCollection: 'foo',
31+
},
32+
};
33+
34+
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
35+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
36+
const { cli } = JSON.parse(newTree.readContent('/angular.json'));
37+
38+
expect(cli.defaultCollection).toBeUndefined();
39+
expect(cli.schematicCollections).toEqual(['foo']);
40+
});
41+
42+
it(`should not error when 'cli' is not defined`, async () => {
43+
const angularConfig: WorkspaceSchema = {
44+
version: 1,
45+
projects: {},
46+
};
47+
48+
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
49+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
50+
const { cli } = JSON.parse(newTree.readContent('/angular.json'));
51+
52+
expect(cli).toBeUndefined();
53+
});
54+
55+
it(`should replace 'defaultCollection' with 'schematicCollections' at the project level`, async () => {
56+
const angularConfig = {
57+
version: 1,
58+
cli: {
59+
defaultCollection: 'foo',
60+
},
61+
projects: {
62+
test: {
63+
sourceRoot: '',
64+
root: '',
65+
prefix: '',
66+
projectType: ProjectType.Application,
67+
cli: {
68+
defaultCollection: 'bar',
69+
},
70+
},
71+
},
72+
};
73+
74+
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
75+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
76+
const {
77+
projects: { test },
78+
} = JSON.parse(newTree.readContent('/angular.json'));
79+
80+
expect(test.cli.defaultCollection).toBeUndefined();
81+
expect(test.cli.schematicCollections).toEqual(['bar']);
82+
});
83+
84+
it(`should not replace 'defaultCollection' with 'schematicCollections', when it is already defined`, async () => {
85+
const angularConfig = {
86+
version: 1,
87+
projects: {},
88+
cli: {
89+
defaultCollection: 'foo',
90+
schematicCollections: ['bar'],
91+
},
92+
};
93+
94+
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
95+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
96+
const { cli } = JSON.parse(newTree.readContent('/angular.json'));
97+
98+
expect(cli.defaultCollection).toBeUndefined();
99+
expect(cli.schematicCollections).toEqual(['bar']);
100+
});
101+
});

packages/schematics/angular/utility/workspace-models.ts

-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ export type E2EBuilderTarget = BuilderTarget<Builders.Protractor, E2EOptions>;
138138
interface WorkspaceCLISchema {
139139
warnings?: Record<string, boolean>;
140140
schematicCollections?: string[];
141-
defaultCollection?: string;
142141
}
143142
export interface WorkspaceSchema {
144143
version: 1;

0 commit comments

Comments
 (0)