From 68024234edcb942d5a177d6bd7567e77d7e40245 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 17 Feb 2023 13:32:33 +0000 Subject: [PATCH] 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"] ``` --- .../cli/lib/config/workspace-schema.json | 15 --- .../schematics-command-module.ts | 4 +- .../migrations/migration-collection.json | 5 + .../replace-default-collection-option.ts | 35 ++++++ .../replace-default-collection-option_spec.ts | 101 ++++++++++++++++++ .../angular/utility/workspace-models.ts | 1 - 6 files changed, 142 insertions(+), 19 deletions(-) create mode 100644 packages/schematics/angular/migrations/update-16/replace-default-collection-option.ts create mode 100644 packages/schematics/angular/migrations/update-16/replace-default-collection-option_spec.ts diff --git a/packages/angular/cli/lib/config/workspace-schema.json b/packages/angular/cli/lib/config/workspace-schema.json index 8e74184ade47..44e0339f43b8 100644 --- a/packages/angular/cli/lib/config/workspace-schema.json +++ b/packages/angular/cli/lib/config/workspace-schema.json @@ -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.", @@ -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.", @@ -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.", diff --git a/packages/angular/cli/src/command-builder/schematics-command-module.ts b/packages/angular/cli/src/command-builder/schematics-command-module.ts index 7368da0759cc..44631afa7988 100644 --- a/packages/angular/cli/src/command-builder/schematics-command-module.ts +++ b/packages/angular/cli/src/command-builder/schematics-command-module.ts @@ -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; diff --git a/packages/schematics/angular/migrations/migration-collection.json b/packages/schematics/angular/migrations/migration-collection.json index acee4359cfda..cb265c98c51b 100644 --- a/packages/schematics/angular/migrations/migration-collection.json +++ b/packages/schematics/angular/migrations/migration-collection.json @@ -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'." } } } diff --git a/packages/schematics/angular/migrations/update-16/replace-default-collection-option.ts b/packages/schematics/angular/migrations/update-16/replace-default-collection-option.ts new file mode 100644 index 000000000000..0bb7d2f16d3b --- /dev/null +++ b/packages/schematics/angular/migrations/update-16/replace-default-collection-option.ts @@ -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']; + } +} diff --git a/packages/schematics/angular/migrations/update-16/replace-default-collection-option_spec.ts b/packages/schematics/angular/migrations/update-16/replace-default-collection-option_spec.ts new file mode 100644 index 000000000000..ecaddb34bfd1 --- /dev/null +++ b/packages/schematics/angular/migrations/update-16/replace-default-collection-option_spec.ts @@ -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']); + }); +}); diff --git a/packages/schematics/angular/utility/workspace-models.ts b/packages/schematics/angular/utility/workspace-models.ts index fbb0ecc48abb..f64e0de4f922 100644 --- a/packages/schematics/angular/utility/workspace-models.ts +++ b/packages/schematics/angular/utility/workspace-models.ts @@ -138,7 +138,6 @@ export type E2EBuilderTarget = BuilderTarget; interface WorkspaceCLISchema { warnings?: Record; schematicCollections?: string[]; - defaultCollection?: string; } export interface WorkspaceSchema { version: 1;