diff --git a/integration/harness-e2e-cli/angular.json b/integration/harness-e2e-cli/angular.json index d0e36b1cfac3..40dd320e0b29 100644 --- a/integration/harness-e2e-cli/angular.json +++ b/integration/harness-e2e-cli/angular.json @@ -96,6 +96,5 @@ } } } - }, - "defaultProject": "harness-e2e-cli" + } } diff --git a/integration/ng-add/angular.json b/integration/ng-add/angular.json index 274bde61e36b..b239289de387 100644 --- a/integration/ng-add/angular.json +++ b/integration/ng-add/angular.json @@ -97,6 +97,5 @@ } } } - }, - "defaultProject": "ng-add" + } } diff --git a/integration/ng-update-v13/angular.json b/integration/ng-update-v13/angular.json index be048fa46824..ead00b38dd98 100644 --- a/integration/ng-update-v13/angular.json +++ b/integration/ng-update-v13/angular.json @@ -96,6 +96,5 @@ } } } - }, - "defaultProject": "ng-update" + } } diff --git a/src/cdk/schematics/ng-generate/drag-drop/index.spec.ts b/src/cdk/schematics/ng-generate/drag-drop/index.spec.ts index eaae82a7bfb4..8e6d76ddd751 100644 --- a/src/cdk/schematics/ng-generate/drag-drop/index.spec.ts +++ b/src/cdk/schematics/ng-generate/drag-drop/index.spec.ts @@ -60,7 +60,6 @@ describe('CDK drag-drop schematic', () => { 'angular.json', JSON.stringify({ version: 1, - defaultProject: 'material', projects: { material: { projectType: 'application', @@ -173,7 +172,6 @@ describe('CDK drag-drop schematic', () => { 'angular.json', JSON.stringify({ version: 1, - defaultProject: 'material', projects: { material: { projectType: 'application', diff --git a/src/cdk/schematics/utils/get-project.ts b/src/cdk/schematics/utils/get-project.ts index 3178dcf70ba8..b1ee65b61551 100644 --- a/src/cdk/schematics/utils/get-project.ts +++ b/src/cdk/schematics/utils/get-project.ts @@ -15,8 +15,14 @@ import {SchematicsException} from '@angular-devkit/schematics'; */ export function getProjectFromWorkspace( workspace: WorkspaceDefinition, - projectName = workspace.extensions.defaultProject as string, + projectName: string | undefined, ): ProjectDefinition { + if (!projectName) { + // TODO(crisbeto): some schematics APIs have the project name as optional so for now it's + // simpler to allow undefined and checking it at runtime. Eventually we should clean this up. + throw new SchematicsException('Project name is required.'); + } + const project = workspace.projects.get(projectName); if (!project) { diff --git a/src/material/schematics/ng-add/index.spec.ts b/src/material/schematics/ng-add/index.spec.ts index 858f73a14ea8..d47589ea4740 100644 --- a/src/material/schematics/ng-add/index.spec.ts +++ b/src/material/schematics/ng-add/index.spec.ts @@ -19,6 +19,7 @@ interface PackageJson { } describe('ng-add schematic', () => { + const baseOptions = {project: 'material'}; let runner: SchematicTestRunner; let appTree: Tree; let errorOutput: string[]; @@ -59,7 +60,7 @@ describe('ng-add schematic', () => { // animations installed already, we remove the animations dependency explicitly. removePackageJsonDependency(appTree, '@angular/animations'); - const tree = await runner.runSchematicAsync('ng-add', {}, appTree).toPromise(); + const tree = await runner.runSchematicAsync('ng-add', baseOptions, appTree).toPromise(); const packageJson = JSON.parse(getFileContent(tree, '/package.json')) as PackageJson; const dependencies = packageJson.dependencies; const angularCoreVersion = dependencies['@angular/core']; @@ -92,7 +93,7 @@ describe('ng-add schematic', () => { // requested package version into the `package.json` before the actual schematic runs. addPackageToPackageJson(appTree, '@angular/material', '^9.0.0'); - const tree = await runner.runSchematicAsync('ng-add', {}, appTree).toPromise(); + const tree = await runner.runSchematicAsync('ng-add', baseOptions, appTree).toPromise(); const packageJson = JSON.parse(getFileContent(tree, '/package.json')) as PackageJson; const dependencies = packageJson.dependencies; @@ -101,10 +102,12 @@ describe('ng-add schematic', () => { }); it('should add default theme', async () => { - const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise(); + const tree = await runner + .runSchematicAsync('ng-add-setup-project', baseOptions, appTree) + .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); expectProjectStyleFile( project, @@ -117,18 +120,18 @@ describe('ng-add schematic', () => { appTree = await createTestApp(runner, {style: 'scss'}); const tree = await runner - .runSchematicAsync('ng-add-setup-project', {theme: 'custom'}, appTree) + .runSchematicAsync('ng-add-setup-project', {...baseOptions, theme: 'custom'}, appTree) .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const expectedStylesPath = normalize(`/${project.root}/src/styles.scss`); const buffer = tree.read(expectedStylesPath); const themeContent = buffer!.toString(); expect(themeContent).toContain(`@use '@angular/material' as mat;`); - expect(themeContent).toContain(`$app-primary: mat.define-palette(`); + expect(themeContent).toContain(`$material-primary: mat.define-palette(`); }); it('should create a custom theme file if no SCSS file could be found', async () => { @@ -136,10 +139,10 @@ describe('ng-add schematic', () => { appTree = await createTestApp(runner, {style: 'css'}); const tree = await runner - .runSchematicAsync('ng-add-setup-project', {theme: 'custom'}, appTree) + .runSchematicAsync('ng-add-setup-project', {...baseOptions, theme: 'custom'}, appTree) .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const expectedStylesPath = normalize(`/${project.root}/src/custom-theme.scss`); expect(tree.files) @@ -149,9 +152,11 @@ describe('ng-add schematic', () => { }); it('should add font links', async () => { - const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise(); + const tree = await runner + .runSchematicAsync('ng-add-setup-project', baseOptions, appTree) + .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const indexFiles = getProjectIndexFiles(project); expect(indexFiles.length).toBe(1); @@ -175,9 +180,11 @@ describe('ng-add schematic', () => { }); it('should add material app styles', async () => { - const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise(); + const tree = await runner + .runSchematicAsync('ng-add-setup-project', baseOptions, appTree) + .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const defaultStylesPath = getProjectStyleFile(project)!; const htmlContent = tree.read(defaultStylesPath)!.toString(); @@ -190,7 +197,9 @@ describe('ng-add schematic', () => { describe('animations enabled', () => { it('should add the BrowserAnimationsModule to the project module', async () => { - const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise(); + const tree = await runner + .runSchematicAsync('ng-add-setup-project', baseOptions, appTree) + .toPromise(); const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts'); expect(fileContent) @@ -200,7 +209,7 @@ describe('ng-add schematic', () => { it('should not add BrowserAnimationsModule if NoopAnimationsModule is set up', async () => { const workspace = await getWorkspace(appTree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); // Simulate the case where a developer uses `ng-add` on an Angular CLI project which already // explicitly uses the `NoopAnimationsModule`. It would be wrong to forcibly enable browser @@ -213,7 +222,7 @@ describe('ng-add schematic', () => { project, ); - await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise(); + await runner.runSchematicAsync('ng-add-setup-project', baseOptions, appTree).toPromise(); expect(errorOutput.length).toBe(1); expect(errorOutput[0]).toMatch(/Could not set up "BrowserAnimationsModule"/); @@ -223,7 +232,11 @@ describe('ng-add schematic', () => { describe('animations disabled', () => { it('should add the NoopAnimationsModule to the project module', async () => { const tree = await runner - .runSchematicAsync('ng-add-setup-project', {animations: 'disabled'}, appTree) + .runSchematicAsync( + 'ng-add-setup-project', + {...baseOptions, animations: 'disabled'}, + appTree, + ) .toPromise(); const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts'); @@ -234,7 +247,7 @@ describe('ng-add schematic', () => { it('should not add NoopAnimationsModule if BrowserAnimationsModule is set up', async () => { const workspace = await getWorkspace(appTree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); // Simulate the case where a developer uses `ng-add` on an Angular CLI project which already // explicitly uses the `BrowserAnimationsModule`. It would be wrong to forcibly change @@ -248,17 +261,22 @@ describe('ng-add schematic', () => { const fileContent = getFileContent(appTree, '/projects/material/src/app/app.module.ts'); - expect(fileContent).not.toContain( - 'NoopAnimationsModule', - 'Expected the project app module to not import the "NoopAnimationsModule".', - ); + expect(fileContent) + .not.withContext( + 'Expected the project app module to not import the "NoopAnimationsModule".', + ) + .toContain('NoopAnimationsModule'); }); }); describe('animations excluded', () => { it('should not add any animations code if animations are excluded', async () => { const tree = await runner - .runSchematicAsync('ng-add-setup-project', {animations: 'excluded'}, appTree) + .runSchematicAsync( + 'ng-add-setup-project', + {...baseOptions, animations: 'excluded'}, + appTree, + ) .toPromise(); const fileContent = getFileContent(tree, '/projects/material/src/app/app.module.ts'); @@ -274,7 +292,6 @@ describe('ng-add schematic', () => { function overwriteTargetBuilder(tree: Tree, targetName: 'build' | 'test', newBuilder: string) { const config = { version: 1, - defaultProject: 'material', projects: { material: { projectType: 'application', @@ -312,13 +329,13 @@ describe('ng-add schematic', () => { it('should throw an error if the "build" target has been changed', async () => { overwriteTargetBuilder(appTree, 'build', 'thirdparty-builder'); await expectAsync( - runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise(), + runner.runSchematicAsync('ng-add-setup-project', baseOptions, appTree).toPromise(), ).toBeRejectedWithError(/not using the default builders.*build/); }); it('should warn if the "test" target has been changed', async () => { overwriteTargetBuilder(appTree, 'test', 'thirdparty-test-builder'); - await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise(); + await runner.runSchematicAsync('ng-add-setup-project', baseOptions, appTree).toPromise(); expect(errorOutput.length).toBe(0); expect(warnOutput.length).toBe(1); @@ -340,7 +357,6 @@ describe('ng-add schematic', () => { JSON.stringify( { version: 1, - defaultProject: 'material', projects: { material: { projectType: 'application', @@ -371,9 +387,11 @@ describe('ng-add schematic', () => { const existingThemePath = './node_modules/@angular/material/prebuilt-themes/purple-green.css'; writeStyleFileToWorkspace(appTree, existingThemePath); - const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise(); + const tree = await runner + .runSchematicAsync('ng-add-setup-project', baseOptions, appTree) + .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const styles = getProjectTargetOptions(project, 'build').styles; expect(styles) @@ -387,15 +405,16 @@ describe('ng-add schematic', () => { it('should not replace existing custom theme files', async () => { writeStyleFileToWorkspace(appTree, './projects/material/custom-theme.scss'); - const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise(); + const tree = await runner + .runSchematicAsync('ng-add-setup-project', baseOptions, appTree) + .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const styles = getProjectTargetOptions(project, 'build').styles; - expect(styles).not.toContain( - defaultPrebuiltThemePath, - 'Expected the default prebuilt theme to be not configured.', - ); + expect(styles) + .not.withContext('Expected the default prebuilt theme to be not configured.') + .toContain(defaultPrebuiltThemePath); expect(errorOutput.length).toBe(1); expect(errorOutput[0]).toMatch(/Could not add the selected theme/); }); @@ -403,9 +422,11 @@ describe('ng-add schematic', () => { it('should not add a theme file multiple times', async () => { writeStyleFileToWorkspace(appTree, defaultPrebuiltThemePath); - const tree = await runner.runSchematicAsync('ng-add-setup-project', {}, appTree).toPromise(); + const tree = await runner + .runSchematicAsync('ng-add-setup-project', baseOptions, appTree) + .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const styles = getProjectTargetOptions(project, 'build').styles; expect(styles) @@ -418,7 +439,7 @@ describe('ng-add schematic', () => { it('should not overwrite existing custom theme files', async () => { appTree.create('/projects/material/custom-theme.scss', 'custom-theme'); const tree = await runner - .runSchematicAsync('ng-add-setup-project', {theme: 'custom'}, appTree) + .runSchematicAsync('ng-add-setup-project', {...baseOptions, theme: 'custom'}, appTree) .toPromise(); expect(tree.readContent('/projects/material/custom-theme.scss')) @@ -432,13 +453,14 @@ describe('ng-add schematic', () => { .runSchematicAsync( 'ng-add-setup-project', { + ...baseOptions, typography: true, }, appTree, ) .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const indexFiles = getProjectIndexFiles(project); expect(indexFiles.length).toBe(1); @@ -464,6 +486,7 @@ describe('ng-add schematic', () => { .runSchematicAsync( 'ng-add-setup-project', { + ...baseOptions, typography: true, }, appTree, @@ -471,7 +494,7 @@ describe('ng-add schematic', () => { .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const indexFiles = getProjectIndexFiles(project); expect(indexFiles.length).toBe(1); @@ -496,6 +519,7 @@ describe('ng-add schematic', () => { .runSchematicAsync( 'ng-add-setup-project', { + ...baseOptions, typography: true, }, appTree, @@ -503,7 +527,7 @@ describe('ng-add schematic', () => { .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const indexFiles = getProjectIndexFiles(project); expect(indexFiles.length).toBe(1); @@ -528,6 +552,7 @@ describe('ng-add schematic', () => { .runSchematicAsync( 'ng-add-setup-project', { + ...baseOptions, typography: false, }, appTree, @@ -535,7 +560,7 @@ describe('ng-add schematic', () => { .toPromise(); const workspace = await getWorkspace(tree); - const project = getProjectFromWorkspace(workspace); + const project = getProjectFromWorkspace(workspace, baseOptions.project); const indexFiles = getProjectIndexFiles(project); expect(indexFiles.length).toBe(1); @@ -568,7 +593,9 @@ describe('ng-add schematic - library project', () => { }); it('should warn if a library project is targeted', async () => { - await runner.runSchematicAsync('ng-add-setup-project', {}, libraryTree).toPromise(); + await runner + .runSchematicAsync('ng-add-setup-project', {project: 'material'}, libraryTree) + .toPromise(); expect(errorOutput.length).toBe(0); expect(warnOutput.length).toBe(1);