Skip to content

Commit 0d499a3

Browse files
authored
fix(cdk/schematics): clean up deep imports of devkit APIs (#27363)
Removes deep imports for some devkit APIs from the CDK schematics code to avoid potential build issues. Fixes #27344.
1 parent 425c469 commit 0d499a3

File tree

13 files changed

+38
-51
lines changed

13 files changed

+38
-51
lines changed

src/cdk/schematics/ng-update/devkit-migration-rule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
1010
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
11-
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
11+
import {workspaces} from '@angular-devkit/core';
1212

1313
import {UpdateProject} from '../update-tool';
1414
import {WorkspacePath} from '../update-tool/file-system';
@@ -139,7 +139,7 @@ export function createMigrationSchematicRule(
139139

140140
/** Runs the migrations for the specified workspace project. */
141141
function runMigrations(
142-
project: ProjectDefinition,
142+
project: workspaces.ProjectDefinition,
143143
projectName: string,
144144
tsconfigPath: WorkspacePath,
145145
additionalStylesheetPaths: string[],

src/cdk/schematics/ng-update/devkit-migration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {SchematicContext, Tree} from '@angular-devkit/schematics';
10-
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
10+
import {workspaces} from '@angular-devkit/core';
1111
import {Constructor, Migration, PostMigrationAction} from '../update-tool/migration';
1212
import {TargetVersion} from '../update-tool/target-version';
1313

@@ -17,7 +17,7 @@ export type DevkitContext = {
1717
/** Name of the project the migrations run against. */
1818
projectName: string;
1919
/** Workspace project the migrations run against. */
20-
project: ProjectDefinition;
20+
project: workspaces.ProjectDefinition;
2121
/** Whether the migrations run for a test target. */
2222
isTestTarget: boolean;
2323
};
@@ -43,5 +43,5 @@ export abstract class DevkitMigration<Data> extends Migration<Data, DevkitContex
4343
}
4444

4545
export type DevkitMigrationCtor<Data> = Constructor<DevkitMigration<Data>> & {
46-
[m in keyof typeof DevkitMigration]: typeof DevkitMigration[m];
46+
[m in keyof typeof DevkitMigration]: (typeof DevkitMigration)[m];
4747
};

src/cdk/schematics/utils/ast.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {getWorkspace} from '@schematics/angular/utility/workspace';
1313
import {findModuleFromOptions as internalFindModule} from '@schematics/angular/utility/find-module';
1414
import {addImportToModule} from '@schematics/angular/utility/ast-utils';
1515
import {getAppModulePath} from '@schematics/angular/utility/ng-ast-utils';
16-
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
16+
import {workspaces} from '@angular-devkit/core';
1717
import * as ts from 'typescript';
1818
import {getProjectMainFile} from './project-main-file';
1919

@@ -31,7 +31,7 @@ export function addModuleImportToRootModule(
3131
host: Tree,
3232
moduleName: string,
3333
src: string,
34-
project: ProjectDefinition,
34+
project: workspaces.ProjectDefinition,
3535
) {
3636
const modulePath = getAppModulePath(host, getProjectMainFile(project));
3737
addModuleImportToModule(host, modulePath, moduleName, src);

src/cdk/schematics/utils/build-component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {strings, template as interpolateTemplate} from '@angular-devkit/core';
9+
import {strings, template as interpolateTemplate, workspaces} from '@angular-devkit/core';
1010
import {
1111
apply,
1212
applyTemplates,
@@ -35,13 +35,12 @@ import {dirname, join, resolve} from 'path';
3535
import * as ts from 'typescript';
3636
import {getProjectFromWorkspace} from './get-project';
3737
import {getDefaultComponentOptions, isStandaloneSchematic} from './schematic-options';
38-
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
3938

4039
/**
4140
* Build a default project path for generating.
4241
* @param project The project to build the path for.
4342
*/
44-
function buildDefaultPath(project: ProjectDefinition): string {
43+
function buildDefaultPath(project: workspaces.ProjectDefinition): string {
4544
const root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
4645

4746
const projectDirName = project.extensions.projectType === ProjectType.Application ? 'app' : 'lib';

src/cdk/schematics/utils/get-project.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ProjectDefinition, WorkspaceDefinition} from '@angular-devkit/core/src/workspace';
9+
import {workspaces} from '@angular-devkit/core';
1010
import {SchematicsException} from '@angular-devkit/schematics';
1111

1212
/**
1313
* Finds the specified project configuration in the workspace. Throws an error if the project
1414
* couldn't be found.
1515
*/
1616
export function getProjectFromWorkspace(
17-
workspace: WorkspaceDefinition,
17+
workspace: workspaces.WorkspaceDefinition,
1818
projectName: string | undefined,
19-
): ProjectDefinition {
19+
): workspaces.ProjectDefinition {
2020
if (!projectName) {
2121
// TODO(crisbeto): some schematics APIs have the project name as optional so for now it's
2222
// simpler to allow undefined and checking it at runtime. Eventually we should clean this up.

src/cdk/schematics/utils/project-index-file.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Path} from '@angular-devkit/core';
10-
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
9+
import {Path, workspaces} from '@angular-devkit/core';
1110
import {defaultTargetBuilders, getTargetsByBuilderName} from './project-targets';
1211

1312
/** Gets the path of the index file in the given project. */
14-
export function getProjectIndexFiles(project: ProjectDefinition): Path[] {
13+
export function getProjectIndexFiles(project: workspaces.ProjectDefinition): Path[] {
1514
const paths = getTargetsByBuilderName(project, defaultTargetBuilders.build)
1615
.filter(t => t.options?.index)
1716
.map(t => t.options!.index as Path);

src/cdk/schematics/utils/project-main-file.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Path} from '@angular-devkit/core';
10-
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
9+
import {Path, workspaces} from '@angular-devkit/core';
1110
import {SchematicsException} from '@angular-devkit/schematics';
1211
import {getProjectTargetOptions} from './project-targets';
1312

1413
/** Looks for the main TypeScript file in the given project and returns its path. */
15-
export function getProjectMainFile(project: ProjectDefinition): Path {
14+
export function getProjectMainFile(project: workspaces.ProjectDefinition): Path {
1615
const buildOptions = getProjectTargetOptions(project, 'build');
1716

1817
if (!buildOptions.main) {

src/cdk/schematics/utils/project-style-file.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {isJsonArray, normalize} from '@angular-devkit/core';
10-
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
9+
import {isJsonArray, normalize, workspaces} from '@angular-devkit/core';
1110
import {getProjectTargetOptions} from './project-targets';
1211

1312
/** Regular expression that matches all possible Angular CLI default style files. */
@@ -20,7 +19,10 @@ const validStyleFileRegex = /\.(c|le|sc)ss/;
2019
* Gets a style file with the given extension in a project and returns its path. If no
2120
* extension is specified, any style file with a valid extension will be returned.
2221
*/
23-
export function getProjectStyleFile(project: ProjectDefinition, extension?: string): string | null {
22+
export function getProjectStyleFile(
23+
project: workspaces.ProjectDefinition,
24+
extension?: string,
25+
): string | null {
2426
const buildOptions = getProjectTargetOptions(project, 'build');
2527
if (buildOptions.styles && isJsonArray(buildOptions.styles) && buildOptions.styles.length) {
2628
const styles = buildOptions.styles.map(s =>

src/cdk/schematics/utils/project-targets.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ProjectDefinition, TargetDefinition} from '@angular-devkit/core/src/workspace';
10-
import {JsonValue} from '@angular-devkit/core';
9+
import {JsonValue, workspaces} from '@angular-devkit/core';
1110
import {SchematicsException} from '@angular-devkit/schematics';
1211

1312
/** Object that maps a CLI target to its default builder name. */
@@ -18,7 +17,7 @@ export const defaultTargetBuilders = {
1817

1918
/** Resolves the architect options for the build target of the given project. */
2019
export function getProjectTargetOptions(
21-
project: ProjectDefinition,
20+
project: workspaces.ProjectDefinition,
2221
buildTarget: string,
2322
): Record<string, JsonValue | undefined> {
2423
const options = project.targets?.get(buildTarget)?.options;
@@ -34,9 +33,9 @@ export function getProjectTargetOptions(
3433

3534
/** Gets all targets from the given project that match the specified builder name. */
3635
export function getTargetsByBuilderName(
37-
project: ProjectDefinition,
36+
project: workspaces.ProjectDefinition,
3837
builderName: string,
39-
): TargetDefinition[] {
38+
): workspaces.TargetDefinition[] {
4039
return Array.from(project.targets.keys())
4140
.filter(name => project.targets.get(name)?.builder === builderName)
4241
.map(name => project.targets.get(name)!);

src/cdk/schematics/utils/project-tsconfig-paths.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,17 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {normalize} from '@angular-devkit/core';
10-
import {
11-
ProjectDefinition,
12-
WorkspaceDefinition,
13-
WorkspaceHost,
14-
} from '@angular-devkit/core/src/workspace';
15-
import {readJsonWorkspace} from '@angular-devkit/core/src/workspace/json/reader';
9+
import {normalize, workspaces} from '@angular-devkit/core';
1610
import {Tree} from '@angular-devkit/schematics';
11+
import {getWorkspace} from '@schematics/angular/utility/workspace';
1712
import {WorkspacePath} from '../update-tool/file-system';
1813

1914
/** Name of the default Angular CLI workspace configuration files. */
2015
const defaultWorkspaceConfigPaths = ['/angular.json', '/.angular.json'];
2116

2217
/** Gets the tsconfig path from the given target within the specified project. */
2318
export function getTargetTsconfigPath(
24-
project: ProjectDefinition,
19+
project: workspaces.ProjectDefinition,
2520
targetName: string,
2621
): WorkspacePath | null {
2722
const tsconfig = project.targets?.get(targetName)?.options?.tsConfig;
@@ -31,18 +26,15 @@ export function getTargetTsconfigPath(
3126
/** Resolve the workspace configuration of the specified tree gracefully. */
3227
export async function getWorkspaceConfigGracefully(
3328
tree: Tree,
34-
): Promise<WorkspaceDefinition | null> {
29+
): Promise<workspaces.WorkspaceDefinition | null> {
3530
const path = defaultWorkspaceConfigPaths.find(filePath => tree.exists(filePath));
36-
const configBuffer = tree.read(path!);
3731

38-
if (!path || !configBuffer) {
32+
if (!path) {
3933
return null;
4034
}
4135

4236
try {
43-
return await readJsonWorkspace(path, {
44-
readFile: async filePath => tree.read(filePath)!.toString(),
45-
} as WorkspaceHost);
37+
return getWorkspace(tree, path);
4638
} catch {
4739
return null;
4840
}

src/cdk/schematics/utils/schematic-options.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
10-
import {isJsonObject, JsonObject} from '@angular-devkit/core';
9+
import {isJsonObject, JsonObject, workspaces} from '@angular-devkit/core';
1110
import {Schema, Style} from '@schematics/angular/component/schema';
1211
import {isStandaloneApp} from '@schematics/angular/utility/ng-ast-utils';
1312
import {getProjectMainFile} from './project-main-file';
@@ -22,7 +21,7 @@ import {Tree} from '@angular-devkit/schematics';
2221
* This is necessary because the Angular CLI only exposes the default values for the "--style",
2322
* "--inlineStyle", "--skipTests" and "--inlineTemplate" options to the "component" schematic.
2423
*/
25-
export function getDefaultComponentOptions(project: ProjectDefinition): Partial<Schema> {
24+
export function getDefaultComponentOptions(project: workspaces.ProjectDefinition): Partial<Schema> {
2625
// Note: Not all options which are available when running "ng new" will be stored in the
2726
// workspace config. List of options which will be available in the configuration:
2827
// 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
6867
* CLI workspace configuration.
6968
*/
7069
function getDefaultComponentOption<T>(
71-
project: ProjectDefinition,
70+
project: workspaces.ProjectDefinition,
7271
optionNames: string[],
7372
fallbackValue: T,
7473
): T {

src/material/schematics/ng-add/index.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {normalize} from '@angular-devkit/core';
2-
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
1+
import {normalize, workspaces} from '@angular-devkit/core';
32
import {Tree} from '@angular-devkit/schematics';
43
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
54
import {
@@ -41,7 +40,7 @@ describe('ng-add schematic', () => {
4140
});
4241

4342
/** Expects the given file to be in the styles of the specified workspace project. */
44-
function expectProjectStyleFile(project: ProjectDefinition, filePath: string) {
43+
function expectProjectStyleFile(project: workspaces.ProjectDefinition, filePath: string) {
4544
expect(getProjectTargetOptions(project, 'build').styles)
4645
.withContext(`Expected "${filePath}" to be added to the project styles in the workspace.`)
4746
.toContain(filePath);

src/material/schematics/ng-add/theming/theming.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {normalize, logging} from '@angular-devkit/core';
10-
import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
9+
import {normalize, logging, workspaces} from '@angular-devkit/core';
1110
import {
1211
chain,
1312
noop,
@@ -174,7 +173,7 @@ function addThemeStyleToTarget(
174173
* this function can either throw or just show a warning.
175174
*/
176175
function validateDefaultTargetBuilder(
177-
project: ProjectDefinition,
176+
project: workspaces.ProjectDefinition,
178177
targetName: 'build' | 'test',
179178
logger: logging.LoggerApi,
180179
) {

0 commit comments

Comments
 (0)