From 745770bc570679db484da32e69ad8ca42f782cf3 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Sun, 25 Jun 2023 09:22:38 +0200 Subject: [PATCH] fix(cdk/schematics): clean up deep imports of devkit APIs Removes deep imports for some devkit APIs from the CDK schematics code to avoid potential build issues. Fixes #27344. --- .../ng-update/devkit-migration-rule.ts | 4 ++-- .../schematics/ng-update/devkit-migration.ts | 6 +++--- src/cdk/schematics/utils/ast.ts | 4 ++-- src/cdk/schematics/utils/build-component.ts | 5 ++--- src/cdk/schematics/utils/get-project.ts | 6 +++--- .../schematics/utils/project-index-file.ts | 5 ++--- src/cdk/schematics/utils/project-main-file.ts | 5 ++--- .../schematics/utils/project-style-file.ts | 8 +++++--- src/cdk/schematics/utils/project-targets.ts | 9 ++++----- .../utils/project-tsconfig-paths.ts | 20 ++++++------------- src/cdk/schematics/utils/schematic-options.ts | 7 +++---- src/material/schematics/ng-add/index.spec.ts | 5 ++--- .../schematics/ng-add/theming/theming.ts | 5 ++--- 13 files changed, 38 insertions(+), 51 deletions(-) diff --git a/src/cdk/schematics/ng-update/devkit-migration-rule.ts b/src/cdk/schematics/ng-update/devkit-migration-rule.ts index e24b8fa2cafa..c3228d84b464 100644 --- a/src/cdk/schematics/ng-update/devkit-migration-rule.ts +++ b/src/cdk/schematics/ng-update/devkit-migration-rule.ts @@ -8,7 +8,7 @@ import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics'; import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks'; -import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; +import {workspaces} from '@angular-devkit/core'; import {UpdateProject} from '../update-tool'; import {WorkspacePath} from '../update-tool/file-system'; @@ -139,7 +139,7 @@ export function createMigrationSchematicRule( /** Runs the migrations for the specified workspace project. */ function runMigrations( - project: ProjectDefinition, + project: workspaces.ProjectDefinition, projectName: string, tsconfigPath: WorkspacePath, additionalStylesheetPaths: string[], diff --git a/src/cdk/schematics/ng-update/devkit-migration.ts b/src/cdk/schematics/ng-update/devkit-migration.ts index 138af851233d..4f93fa322942 100644 --- a/src/cdk/schematics/ng-update/devkit-migration.ts +++ b/src/cdk/schematics/ng-update/devkit-migration.ts @@ -7,7 +7,7 @@ */ import {SchematicContext, Tree} from '@angular-devkit/schematics'; -import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; +import {workspaces} from '@angular-devkit/core'; import {Constructor, Migration, PostMigrationAction} from '../update-tool/migration'; import {TargetVersion} from '../update-tool/target-version'; @@ -17,7 +17,7 @@ export type DevkitContext = { /** Name of the project the migrations run against. */ projectName: string; /** Workspace project the migrations run against. */ - project: ProjectDefinition; + project: workspaces.ProjectDefinition; /** Whether the migrations run for a test target. */ isTestTarget: boolean; }; @@ -43,5 +43,5 @@ export abstract class DevkitMigration extends Migration = Constructor> & { - [m in keyof typeof DevkitMigration]: typeof DevkitMigration[m]; + [m in keyof typeof DevkitMigration]: (typeof DevkitMigration)[m]; }; diff --git a/src/cdk/schematics/utils/ast.ts b/src/cdk/schematics/utils/ast.ts index ad71b00c067d..37633772e67a 100644 --- a/src/cdk/schematics/utils/ast.ts +++ b/src/cdk/schematics/utils/ast.ts @@ -13,7 +13,7 @@ import {getWorkspace} from '@schematics/angular/utility/workspace'; import {findModuleFromOptions as internalFindModule} from '@schematics/angular/utility/find-module'; import {addImportToModule} from '@schematics/angular/utility/ast-utils'; import {getAppModulePath} from '@schematics/angular/utility/ng-ast-utils'; -import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; +import {workspaces} from '@angular-devkit/core'; import * as ts from 'typescript'; import {getProjectMainFile} from './project-main-file'; @@ -31,7 +31,7 @@ export function addModuleImportToRootModule( host: Tree, moduleName: string, src: string, - project: ProjectDefinition, + project: workspaces.ProjectDefinition, ) { const modulePath = getAppModulePath(host, getProjectMainFile(project)); addModuleImportToModule(host, modulePath, moduleName, src); diff --git a/src/cdk/schematics/utils/build-component.ts b/src/cdk/schematics/utils/build-component.ts index d97b0c70c13e..a2aba22b2b7a 100644 --- a/src/cdk/schematics/utils/build-component.ts +++ b/src/cdk/schematics/utils/build-component.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {strings, template as interpolateTemplate} from '@angular-devkit/core'; +import {strings, template as interpolateTemplate, workspaces} from '@angular-devkit/core'; import { apply, applyTemplates, @@ -35,13 +35,12 @@ import {dirname, join, resolve} from 'path'; import * as ts from 'typescript'; import {getProjectFromWorkspace} from './get-project'; import {getDefaultComponentOptions, isStandaloneSchematic} from './schematic-options'; -import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; /** * Build a default project path for generating. * @param project The project to build the path for. */ -function buildDefaultPath(project: ProjectDefinition): string { +function buildDefaultPath(project: workspaces.ProjectDefinition): string { const root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`; const projectDirName = project.extensions.projectType === ProjectType.Application ? 'app' : 'lib'; diff --git a/src/cdk/schematics/utils/get-project.ts b/src/cdk/schematics/utils/get-project.ts index b1ee65b61551..c6c3727725fe 100644 --- a/src/cdk/schematics/utils/get-project.ts +++ b/src/cdk/schematics/utils/get-project.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {ProjectDefinition, WorkspaceDefinition} from '@angular-devkit/core/src/workspace'; +import {workspaces} from '@angular-devkit/core'; import {SchematicsException} from '@angular-devkit/schematics'; /** @@ -14,9 +14,9 @@ import {SchematicsException} from '@angular-devkit/schematics'; * couldn't be found. */ export function getProjectFromWorkspace( - workspace: WorkspaceDefinition, + workspace: workspaces.WorkspaceDefinition, projectName: string | undefined, -): ProjectDefinition { +): workspaces.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. diff --git a/src/cdk/schematics/utils/project-index-file.ts b/src/cdk/schematics/utils/project-index-file.ts index 4510afd7c94f..d7c287675168 100644 --- a/src/cdk/schematics/utils/project-index-file.ts +++ b/src/cdk/schematics/utils/project-index-file.ts @@ -6,12 +6,11 @@ * found in the LICENSE file at https://angular.io/license */ -import {Path} from '@angular-devkit/core'; -import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; +import {Path, workspaces} from '@angular-devkit/core'; import {defaultTargetBuilders, getTargetsByBuilderName} from './project-targets'; /** Gets the path of the index file in the given project. */ -export function getProjectIndexFiles(project: ProjectDefinition): Path[] { +export function getProjectIndexFiles(project: workspaces.ProjectDefinition): Path[] { const paths = getTargetsByBuilderName(project, defaultTargetBuilders.build) .filter(t => t.options?.index) .map(t => t.options!.index as Path); diff --git a/src/cdk/schematics/utils/project-main-file.ts b/src/cdk/schematics/utils/project-main-file.ts index f2c70b8916d7..30c4c3a6f233 100644 --- a/src/cdk/schematics/utils/project-main-file.ts +++ b/src/cdk/schematics/utils/project-main-file.ts @@ -6,13 +6,12 @@ * found in the LICENSE file at https://angular.io/license */ -import {Path} from '@angular-devkit/core'; -import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; +import {Path, workspaces} from '@angular-devkit/core'; import {SchematicsException} from '@angular-devkit/schematics'; import {getProjectTargetOptions} from './project-targets'; /** Looks for the main TypeScript file in the given project and returns its path. */ -export function getProjectMainFile(project: ProjectDefinition): Path { +export function getProjectMainFile(project: workspaces.ProjectDefinition): Path { const buildOptions = getProjectTargetOptions(project, 'build'); if (!buildOptions.main) { diff --git a/src/cdk/schematics/utils/project-style-file.ts b/src/cdk/schematics/utils/project-style-file.ts index 735991b5a102..8962d84f3487 100644 --- a/src/cdk/schematics/utils/project-style-file.ts +++ b/src/cdk/schematics/utils/project-style-file.ts @@ -6,8 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {isJsonArray, normalize} from '@angular-devkit/core'; -import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; +import {isJsonArray, normalize, workspaces} from '@angular-devkit/core'; import {getProjectTargetOptions} from './project-targets'; /** Regular expression that matches all possible Angular CLI default style files. */ @@ -20,7 +19,10 @@ const validStyleFileRegex = /\.(c|le|sc)ss/; * Gets a style file with the given extension in a project and returns its path. If no * extension is specified, any style file with a valid extension will be returned. */ -export function getProjectStyleFile(project: ProjectDefinition, extension?: string): string | null { +export function getProjectStyleFile( + project: workspaces.ProjectDefinition, + extension?: string, +): string | null { const buildOptions = getProjectTargetOptions(project, 'build'); if (buildOptions.styles && isJsonArray(buildOptions.styles) && buildOptions.styles.length) { const styles = buildOptions.styles.map(s => diff --git a/src/cdk/schematics/utils/project-targets.ts b/src/cdk/schematics/utils/project-targets.ts index 2146734a01b2..51a0947df782 100644 --- a/src/cdk/schematics/utils/project-targets.ts +++ b/src/cdk/schematics/utils/project-targets.ts @@ -6,8 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {ProjectDefinition, TargetDefinition} from '@angular-devkit/core/src/workspace'; -import {JsonValue} from '@angular-devkit/core'; +import {JsonValue, workspaces} from '@angular-devkit/core'; import {SchematicsException} from '@angular-devkit/schematics'; /** Object that maps a CLI target to its default builder name. */ @@ -18,7 +17,7 @@ export const defaultTargetBuilders = { /** Resolves the architect options for the build target of the given project. */ export function getProjectTargetOptions( - project: ProjectDefinition, + project: workspaces.ProjectDefinition, buildTarget: string, ): Record { const options = project.targets?.get(buildTarget)?.options; @@ -34,9 +33,9 @@ export function getProjectTargetOptions( /** Gets all targets from the given project that match the specified builder name. */ export function getTargetsByBuilderName( - project: ProjectDefinition, + project: workspaces.ProjectDefinition, builderName: string, -): TargetDefinition[] { +): workspaces.TargetDefinition[] { return Array.from(project.targets.keys()) .filter(name => project.targets.get(name)?.builder === builderName) .map(name => project.targets.get(name)!); diff --git a/src/cdk/schematics/utils/project-tsconfig-paths.ts b/src/cdk/schematics/utils/project-tsconfig-paths.ts index cbcb5daad7f6..6138fa258c82 100644 --- a/src/cdk/schematics/utils/project-tsconfig-paths.ts +++ b/src/cdk/schematics/utils/project-tsconfig-paths.ts @@ -6,14 +6,9 @@ * found in the LICENSE file at https://angular.io/license */ -import {normalize} from '@angular-devkit/core'; -import { - ProjectDefinition, - WorkspaceDefinition, - WorkspaceHost, -} from '@angular-devkit/core/src/workspace'; -import {readJsonWorkspace} from '@angular-devkit/core/src/workspace/json/reader'; +import {normalize, workspaces} from '@angular-devkit/core'; import {Tree} from '@angular-devkit/schematics'; +import {getWorkspace} from '@schematics/angular/utility/workspace'; import {WorkspacePath} from '../update-tool/file-system'; /** Name of the default Angular CLI workspace configuration files. */ @@ -21,7 +16,7 @@ const defaultWorkspaceConfigPaths = ['/angular.json', '/.angular.json']; /** Gets the tsconfig path from the given target within the specified project. */ export function getTargetTsconfigPath( - project: ProjectDefinition, + project: workspaces.ProjectDefinition, targetName: string, ): WorkspacePath | null { const tsconfig = project.targets?.get(targetName)?.options?.tsConfig; @@ -31,18 +26,15 @@ export function getTargetTsconfigPath( /** Resolve the workspace configuration of the specified tree gracefully. */ export async function getWorkspaceConfigGracefully( tree: Tree, -): Promise { +): Promise { const path = defaultWorkspaceConfigPaths.find(filePath => tree.exists(filePath)); - const configBuffer = tree.read(path!); - if (!path || !configBuffer) { + if (!path) { return null; } try { - return await readJsonWorkspace(path, { - readFile: async filePath => tree.read(filePath)!.toString(), - } as WorkspaceHost); + return getWorkspace(tree, path); } catch { return null; } diff --git a/src/cdk/schematics/utils/schematic-options.ts b/src/cdk/schematics/utils/schematic-options.ts index f0a9f81485f4..4de7e3ab17c6 100644 --- a/src/cdk/schematics/utils/schematic-options.ts +++ b/src/cdk/schematics/utils/schematic-options.ts @@ -6,8 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; -import {isJsonObject, JsonObject} from '@angular-devkit/core'; +import {isJsonObject, JsonObject, workspaces} from '@angular-devkit/core'; import {Schema, Style} from '@schematics/angular/component/schema'; import {isStandaloneApp} from '@schematics/angular/utility/ng-ast-utils'; import {getProjectMainFile} from './project-main-file'; @@ -22,7 +21,7 @@ import {Tree} from '@angular-devkit/schematics'; * This is necessary because the Angular CLI only exposes the default values for the "--style", * "--inlineStyle", "--skipTests" and "--inlineTemplate" options to the "component" schematic. */ -export function getDefaultComponentOptions(project: ProjectDefinition): Partial { +export function getDefaultComponentOptions(project: workspaces.ProjectDefinition): Partial { // Note: Not all options which are available when running "ng new" will be stored in the // workspace config. List of options which will be available in the configuration: // angular/angular-cli/blob/main/packages/schematics/angular/application/index.ts#L109-L131 @@ -68,7 +67,7 @@ export async function isStandaloneSchematic(host: Tree, options: Schema): Promis * CLI workspace configuration. */ function getDefaultComponentOption( - project: ProjectDefinition, + project: workspaces.ProjectDefinition, optionNames: string[], fallbackValue: T, ): T { diff --git a/src/material/schematics/ng-add/index.spec.ts b/src/material/schematics/ng-add/index.spec.ts index 8b902cafbe83..9ba0f5a7880f 100644 --- a/src/material/schematics/ng-add/index.spec.ts +++ b/src/material/schematics/ng-add/index.spec.ts @@ -1,5 +1,4 @@ -import {normalize} from '@angular-devkit/core'; -import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; +import {normalize, workspaces} from '@angular-devkit/core'; import {Tree} from '@angular-devkit/schematics'; import {SchematicTestRunner} from '@angular-devkit/schematics/testing'; import { @@ -41,7 +40,7 @@ describe('ng-add schematic', () => { }); /** Expects the given file to be in the styles of the specified workspace project. */ - function expectProjectStyleFile(project: ProjectDefinition, filePath: string) { + function expectProjectStyleFile(project: workspaces.ProjectDefinition, filePath: string) { expect(getProjectTargetOptions(project, 'build').styles) .withContext(`Expected "${filePath}" to be added to the project styles in the workspace.`) .toContain(filePath); diff --git a/src/material/schematics/ng-add/theming/theming.ts b/src/material/schematics/ng-add/theming/theming.ts index e7f1534cbb52..2b33372d0f29 100644 --- a/src/material/schematics/ng-add/theming/theming.ts +++ b/src/material/schematics/ng-add/theming/theming.ts @@ -6,8 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {normalize, logging} from '@angular-devkit/core'; -import {ProjectDefinition} from '@angular-devkit/core/src/workspace'; +import {normalize, logging, workspaces} from '@angular-devkit/core'; import { chain, noop, @@ -174,7 +173,7 @@ function addThemeStyleToTarget( * this function can either throw or just show a warning. */ function validateDefaultTargetBuilder( - project: ProjectDefinition, + project: workspaces.ProjectDefinition, targetName: 'build' | 'test', logger: logging.LoggerApi, ) {