Skip to content

fix(cdk/schematics): support project index file discovery for object-form and default #30967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions src/cdk/schematics/utils/project-index-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,39 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {Path} from '@angular-devkit/core';
import {join} from 'node:path/posix';
import {getProjectBuildTargets} from './project-targets';
import {ProjectDefinition} from '@schematics/angular/utility';

/** Gets the path of the index file in the given project. */
export function getProjectIndexFiles(project: ProjectDefinition): Path[] {
const paths = getProjectBuildTargets(project)
.filter(t => t.options?.['index'])
.map(t => t.options!['index'] as Path);
/**
* Gets the path of the index file in the given project.
* This only searches the base options for each target and not any defined target configurations.
*/
export function getProjectIndexFiles(project: ProjectDefinition): string[] {
// Use a Set to remove duplicate index files referenced in multiple build targets of a project.
const paths = new Set<string>();

for (const target of getProjectBuildTargets(project)) {
const indexValue = target.options?.['index'];

switch (typeof indexValue) {
case 'string':
// "index": "src/index.html"
paths.add(indexValue);
break;
case 'object':
// "index": { "input": "src/index.html", ... }
if (indexValue && 'input' in indexValue) {
paths.add(indexValue['input'] as string);
}
break;
case 'undefined':
// v20+ supports an optional index field; default of `<project_source_root>/index.html`
// `project_source_root` is the project level `sourceRoot`; default of `<project_root>/src`
paths.add(join(project.sourceRoot ?? join(project.root, 'src'), 'index.html'));
break;
}
}

// Use a set to remove duplicate index files referenced in multiple build targets of a project.
return Array.from(new Set(paths));
return Array.from(paths);
}
Loading