Skip to content

Commit 95554fd

Browse files
committed
refactor(@angular/build): use utility helper to get project root paths
The `application`, `karma`, and `unit-test` builders now all use the same utility function to get the project root and project source root paths. This helps ensure consistent behavior and removes repeat code within each.
1 parent 6b46e5b commit 95554fd

File tree

4 files changed

+44
-29
lines changed

4 files changed

+44
-29
lines changed

packages/angular/build/src/builders/application/options.ts

+2-21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
generateSearchDirectories,
2525
loadPostcssConfiguration,
2626
} from '../../utils/postcss-configuration';
27+
import { getProjectRootPaths, normalizeDirectoryPath } from '../../utils/project-metadata';
2728
import { urlJoin } from '../../utils/url';
2829
import {
2930
Schema as ApplicationBuilderOptions,
@@ -160,12 +161,7 @@ export async function normalizeOptions(
160161
// ref: https://github.com/nodejs/node/issues/7726
161162
realpathSync(context.workspaceRoot);
162163
const projectMetadata = await context.getProjectMetadata(projectName);
163-
const projectRoot = normalizeDirectoryPath(
164-
path.join(workspaceRoot, (projectMetadata.root as string | undefined) ?? ''),
165-
);
166-
const projectSourceRoot = normalizeDirectoryPath(
167-
path.join(workspaceRoot, (projectMetadata.sourceRoot as string | undefined) ?? 'src'),
168-
);
164+
const { projectRoot, projectSourceRoot } = getProjectRootPaths(workspaceRoot, projectMetadata);
169165

170166
// Gather persistent caching option and provide a project specific cache location
171167
const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot);
@@ -613,21 +609,6 @@ function normalizeEntryPoints(
613609
}
614610
}
615611

616-
/**
617-
* Normalize a directory path string.
618-
* Currently only removes a trailing slash if present.
619-
* @param path A path string.
620-
* @returns A normalized path string.
621-
*/
622-
function normalizeDirectoryPath(path: string): string {
623-
const last = path[path.length - 1];
624-
if (last === '/' || last === '\\') {
625-
return path.slice(0, -1);
626-
}
627-
628-
return path;
629-
}
630-
631612
function normalizeGlobalEntries(
632613
rawEntries: ({ bundleName?: string; input: string; inject?: boolean } | string)[] | undefined,
633614
defaultName: string,

packages/angular/build/src/builders/karma/application_builder.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { globSync } from 'tinyglobby';
1818
import { BuildOutputFileType } from '../../tools/esbuild/bundler-context';
1919
import { emitFilesToDisk } from '../../tools/esbuild/utils';
2020
import { createVirtualModulePlugin } from '../../tools/esbuild/virtual-module-plugin';
21+
import { getProjectRootPaths } from '../../utils/project-metadata';
2122
import { buildApplicationInternal } from '../application/index';
2223
import { ApplicationBuilderInternalOptions } from '../application/options';
2324
import { Result, ResultFile, ResultKind } from '../application/results';
@@ -317,9 +318,9 @@ async function getProjectSourceRoot(context: BuilderContext): Promise<string> {
317318
}
318319

319320
const projectMetadata = await context.getProjectMetadata(projectName);
320-
const sourceRoot = (projectMetadata.sourceRoot ?? projectMetadata.root ?? '') as string;
321+
const { projectSourceRoot } = getProjectRootPaths(context.workspaceRoot, projectMetadata);
321322

322-
return path.join(context.workspaceRoot, sourceRoot);
323+
return projectSourceRoot;
323324
}
324325

325326
function normalizePolyfills(polyfills: string | string[] | undefined): [string[], string[]] {

packages/angular/build/src/builders/unit-test/options.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { type BuilderContext, targetFromTargetString } from '@angular-devkit/architect';
1010
import path from 'node:path';
1111
import { normalizeCacheOptions } from '../../utils/normalize-cache';
12+
import { getProjectRootPaths } from '../../utils/project-metadata';
1213
import type { Schema as UnitTestOptions } from './schema';
1314

1415
export type NormalizedUnitTestOptions = Awaited<ReturnType<typeof normalizeOptions>>;
@@ -21,12 +22,7 @@ export async function normalizeOptions(
2122
// Setup base paths based on workspace root and project information
2223
const workspaceRoot = context.workspaceRoot;
2324
const projectMetadata = await context.getProjectMetadata(projectName);
24-
const projectRoot = normalizeDirectoryPath(
25-
path.join(workspaceRoot, (projectMetadata.root as string | undefined) ?? ''),
26-
);
27-
const projectSourceRoot = normalizeDirectoryPath(
28-
path.join(workspaceRoot, (projectMetadata.sourceRoot as string | undefined) ?? 'src'),
29-
);
25+
const { projectRoot, projectSourceRoot } = getProjectRootPaths(workspaceRoot, projectMetadata);
3026

3127
// Gather persistent caching option and provide a project specific cache location
3228
const cacheOptions = normalizeCacheOptions(projectMetadata, workspaceRoot);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { join } from 'node:path';
10+
11+
/**
12+
* Normalize a directory path string.
13+
* Currently only removes a trailing slash if present.
14+
* @param path A path string.
15+
* @returns A normalized path string.
16+
*/
17+
export function normalizeDirectoryPath(path: string): string {
18+
const last = path[path.length - 1];
19+
if (last === '/' || last === '\\') {
20+
return path.slice(0, -1);
21+
}
22+
23+
return path;
24+
}
25+
26+
export function getProjectRootPaths(
27+
workspaceRoot: string,
28+
projectMetadata: { root?: string; sourceRoot?: string },
29+
) {
30+
const projectRoot = normalizeDirectoryPath(join(workspaceRoot, projectMetadata.root ?? ''));
31+
const rawSourceRoot = projectMetadata.sourceRoot;
32+
const projectSourceRoot = normalizeDirectoryPath(
33+
rawSourceRoot === undefined ? join(projectRoot, 'src') : join(workspaceRoot, rawSourceRoot),
34+
);
35+
36+
return { projectRoot, projectSourceRoot };
37+
}

0 commit comments

Comments
 (0)