Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aeb49dd

Browse files
committed
perf(@angular/cli): cache dependency lookups during ng add
This commit introduces a caching mechanism for project dependency version lookups within the `ng add` command. A private class field now stores the results of `findProjectVersion`. This avoids redundant file system reads when checking for peer dependency conflicts, leading to a performance improvement. The cache is cleared at the beginning of each `run` execution to ensure freshness.
1 parent e5aed6d commit aeb49dd

File tree

1 file changed

+12
-0
lines changed
  • packages/angular/cli/src/commands/add

1 file changed

+12
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export default class AddCommandModule
8383
protected override allowPrivateSchematics = true;
8484
private readonly schematicName = 'ng-add';
8585
private rootRequire = createRequire(this.context.root + '/');
86+
#projectVersionCache = new Map<string, string | null>();
8687

8788
override async builder(argv: Argv): Promise<Argv<AddCommandArgs>> {
8889
const localYargs = (await super.builder(argv))
@@ -131,6 +132,7 @@ export default class AddCommandModule
131132
}
132133

133134
async run(options: Options<AddCommandArgs> & OtherOptions): Promise<number | void> {
135+
this.#projectVersionCache.clear();
134136
const { logger } = this.context;
135137
const { collection, skipConfirmation } = options;
136138

@@ -567,6 +569,11 @@ export default class AddCommandModule
567569
}
568570

569571
private async findProjectVersion(name: string): Promise<string | null> {
572+
const cachedVersion = this.#projectVersionCache.get(name);
573+
if (cachedVersion !== undefined) {
574+
return cachedVersion;
575+
}
576+
570577
const { logger, root } = this.context;
571578
let installedPackage;
572579
try {
@@ -576,6 +583,7 @@ export default class AddCommandModule
576583
if (installedPackage) {
577584
try {
578585
const installed = await fetchPackageManifest(dirname(installedPackage), logger);
586+
this.#projectVersionCache.set(name, installed.version);
579587

580588
return installed.version;
581589
} catch {}
@@ -590,10 +598,14 @@ export default class AddCommandModule
590598
const version =
591599
projectManifest.dependencies?.[name] || projectManifest.devDependencies?.[name];
592600
if (version) {
601+
this.#projectVersionCache.set(name, version);
602+
593603
return version;
594604
}
595605
}
596606

607+
this.#projectVersionCache.set(name, null);
608+
597609
return null;
598610
}
599611

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /