Skip to content

Commit

Permalink
feat(@angular/cli): remove deprecated defaultCollection from worksp…
Browse files Browse the repository at this point in the history
…ace 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"]
```
  • Loading branch information
alan-agius4 authored and angular-robot[bot] committed Feb 17, 2023
1 parent d58428d commit 6802423
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 19 deletions.
15 changes: 0 additions & 15 deletions packages/angular/cli/lib/config/workspace-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
"cliOptions": {
"type": "object",
"properties": {
"defaultCollection": {
"description": "The default schematics collection to use.",
"type": "string",
"x-deprecated": "Use 'schematicCollections' instead."
},
"schematicCollections": {
"type": "array",
"description": "The list of schematic collections to use.",
Expand Down Expand Up @@ -95,11 +90,6 @@
"cliGlobalOptions": {
"type": "object",
"properties": {
"defaultCollection": {
"description": "The default schematics collection to use.",
"type": "string",
"x-deprecated": "Use 'schematicCollections' instead."
},
"schematicCollections": {
"type": "array",
"description": "The list of schematic collections to use.",
Expand Down Expand Up @@ -199,11 +189,6 @@
"type": "object",
"properties": {
"cli": {
"defaultCollection": {
"description": "The default schematics collection to use.",
"type": "string",
"x-deprecated": "Use 'schematicCollections' instead."
},
"schematicCollections": {
"type": "array",
"description": "The list of schematic collections to use.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,9 @@ export abstract class SchematicsCommandModule
return undefined;
}

const { schematicCollections, defaultCollection } = configSection;
const { schematicCollections } = configSection;
if (Array.isArray(schematicCollections)) {
return new Set(schematicCollections.map((c) => resolveRelativeCollection(c)));
} else if (typeof defaultCollection === 'string') {
return new Set([resolveRelativeCollection(defaultCollection)]);
}

return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"version": "16.0.0",
"factory": "./update-16/remove-default-project-option",
"description": "Remove 'defaultProject' option from workspace configuration. The project to use will be determined from the current working directory."
},
"replace-default-collection-option": {
"version": "16.0.0",
"factory": "./update-16/replace-default-collection-option",
"description": "Replace removed 'defaultCollection' option in workspace configuration with 'schematicCollections'."
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @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 { JsonValue, isJsonObject } from '@angular-devkit/core';
import { Rule } from '@angular-devkit/schematics';
import { updateWorkspace } from '../../utility/workspace';

/** Migration to replace 'defaultCollection' option in angular.json. */
export default function (): Rule {
return updateWorkspace((workspace) => {
// workspace level
replaceDefaultCollection(workspace.extensions['cli']);

// Project level
for (const project of workspace.projects.values()) {
replaceDefaultCollection(project.extensions['cli']);
}
});
}

function replaceDefaultCollection(cliExtension: JsonValue | undefined): void {
if (cliExtension && isJsonObject(cliExtension) && cliExtension['defaultCollection']) {
// If `schematicsCollection` defined `defaultCollection` is ignored hence no need to warn.
if (!cliExtension['schematicCollections']) {
cliExtension['schematicCollections'] = [cliExtension['defaultCollection']];
}

delete cliExtension['defaultCollection'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @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 { EmptyTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { ProjectType, WorkspaceSchema } from '../../utility/workspace-models';

describe(`Migration to replace 'defaultCollection' option.`, () => {
const schematicName = 'replace-default-collection-option';
const schematicRunner = new SchematicTestRunner(
'migrations',
require.resolve('../migration-collection.json'),
);

let tree: UnitTestTree;
beforeEach(() => {
tree = new UnitTestTree(new EmptyTree());
});

it(`should replace 'defaultCollection' with 'schematicCollections' at the root level`, async () => {
const angularConfig = {
version: 1,
projects: {},
cli: {
defaultCollection: 'foo',
},
};

tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const { cli } = JSON.parse(newTree.readContent('/angular.json'));

expect(cli.defaultCollection).toBeUndefined();
expect(cli.schematicCollections).toEqual(['foo']);
});

it(`should not error when 'cli' is not defined`, async () => {
const angularConfig: WorkspaceSchema = {
version: 1,
projects: {},
};

tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const { cli } = JSON.parse(newTree.readContent('/angular.json'));

expect(cli).toBeUndefined();
});

it(`should replace 'defaultCollection' with 'schematicCollections' at the project level`, async () => {
const angularConfig = {
version: 1,
cli: {
defaultCollection: 'foo',
},
projects: {
test: {
sourceRoot: '',
root: '',
prefix: '',
projectType: ProjectType.Application,
cli: {
defaultCollection: 'bar',
},
},
},
};

tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const {
projects: { test },
} = JSON.parse(newTree.readContent('/angular.json'));

expect(test.cli.defaultCollection).toBeUndefined();
expect(test.cli.schematicCollections).toEqual(['bar']);
});

it(`should not replace 'defaultCollection' with 'schematicCollections', when it is already defined`, async () => {
const angularConfig = {
version: 1,
projects: {},
cli: {
defaultCollection: 'foo',
schematicCollections: ['bar'],
},
};

tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const { cli } = JSON.parse(newTree.readContent('/angular.json'));

expect(cli.defaultCollection).toBeUndefined();
expect(cli.schematicCollections).toEqual(['bar']);
});
});
1 change: 0 additions & 1 deletion packages/schematics/angular/utility/workspace-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ export type E2EBuilderTarget = BuilderTarget<Builders.Protractor, E2EOptions>;
interface WorkspaceCLISchema {
warnings?: Record<string, boolean>;
schematicCollections?: string[];
defaultCollection?: string;
}
export interface WorkspaceSchema {
version: 1;
Expand Down

0 comments on commit 6802423

Please sign in to comment.