Skip to content

Commit da53acc

Browse files
committed
refactor(@angular/cli): respect workspace cache configuration in ng add
This change updates the 'ng add' command to dynamically determine the base directory for temporary package manager operations by consulting the workspace configuration. If caching is enabled and the cache path is located within the workspace root, it is added to the list of candidate directories alongside 'node_modules'. This ensures that the package manager correctly inherits project settings even when the user has customized the location of the '.angular' directory, while maintaining the requirement that the directory must be within the project context for configuration inheritance to work.
1 parent 0b60377 commit da53acc

File tree

1 file changed

+14
-3
lines changed
  • packages/angular/cli/src/commands/add

1 file changed

+14
-3
lines changed

packages/angular/cli/src/commands/add/cli.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Listr, ListrRenderer, ListrTaskWrapper, color, figures } from 'listr2';
1010
import assert from 'node:assert';
1111
import fs from 'node:fs/promises';
1212
import { createRequire } from 'node:module';
13-
import { dirname, join } from 'node:path';
13+
import { dirname, join, relative, resolve } from 'node:path';
1414
import npa from 'npm-package-arg';
1515
import semver, { Range, compare, intersects, prerelease, satisfies, valid } from 'semver';
1616
import { Argv } from 'yargs';
@@ -34,6 +34,7 @@ import {
3434
import { assertIsError } from '../../utilities/error';
3535
import { isTTY } from '../../utilities/tty';
3636
import { VERSION } from '../../utilities/version';
37+
import { getCacheConfig } from '../cache/utilities';
3738

3839
class CommandError extends Error {}
3940

@@ -300,9 +301,19 @@ export default class AddCommandModule
300301
task: AddCommandTaskWrapper,
301302
): Promise<void> {
302303
let tempDirectory: string | undefined;
303-
for (const path of ['.angular/cache', 'node_modules']) {
304+
const tempOptions = ['node_modules'];
305+
306+
const cacheConfig = getCacheConfig(this.context.workspace);
307+
if (cacheConfig.enabled) {
308+
const cachePath = resolve(this.context.root, cacheConfig.path);
309+
if (!relative(this.context.root, cachePath).startsWith('..')) {
310+
tempOptions.push(cachePath);
311+
}
312+
}
313+
314+
for (const tempOption of tempOptions) {
304315
try {
305-
const directory = join(this.context.root, path);
316+
const directory = resolve(this.context.root, tempOption);
306317
if ((await fs.stat(directory)).isDirectory()) {
307318
tempDirectory = directory;
308319
break;

0 commit comments

Comments
 (0)