perf(@angular/cli): lazily initialize package manager in command context#32854
perf(@angular/cli): lazily initialize package manager in command context#32854alan-agius4 wants to merge 1 commit intoangular:mainfrom
Conversation
2d01580 to
94b9bb5
Compare
There was a problem hiding this comment.
Code Review
This pull request refactors the CommandContext to treat the packageManager as a Promise, allowing for lazy initialization. This change requires updating multiple command modules to asynchronously resolve the package manager before use. Furthermore, the SmartDefaultProvider interface and the CoreSchemaRegistry have been updated to support asynchronous providers. Review feedback focuses on improving code readability and performance by avoiding redundant await calls on the packageManager promise in AddCommandModule and gatherVersionInfo.
| manifest = await ( | ||
| await this.context.packageManager | ||
| ).getManifest(context.packageIdentifier, { | ||
| registry, | ||
| }); |
There was a problem hiding this comment.
| packageManager: { | ||
| name: context.packageManager.name, | ||
| version: await context.packageManager.getVersion(), | ||
| name: (await context.packageManager).name, | ||
| version: await (await context.packageManager).getVersion(), | ||
| }, |
There was a problem hiding this comment.
To avoid awaiting the packageManager promise twice and to improve readability, you can resolve it once. An async IIFE can be used here to keep the logic self-contained within the object property.
packageManager: await (async () => {
const pm = await context.packageManager;
return {
name: pm.name,
version: await pm.getVersion(),
};
})(),94b9bb5 to
1073ddc
Compare
This change refactors the `CommandContext` to defer the initialization of the `packageManager` instance until it is first accessed. This optimization avoids unnecessary configuration discovery and disk I/O for commands that do not require the package manager, such as `ng help`, and certain shell completions. As part of this refactoring: - The `packageManager` property in `CommandContext` is now a `Promise<PackageManager>`. - Internal command modules and utility functions have been updated to `await` the package manager's lazy initialization. - In `@angular-devkit/core`, `CoreSchemaRegistry` was updated to rename `_sourceMap` to `_sourceProvider` and optimize the resolution of smart defaults by awaiting values during the `_set` operation.
1073ddc to
920e161
Compare
This change refactors the
CommandContextto defer the initialization of thepackageManagerinstance until it is first accessed. This optimization avoids unnecessary configuration discovery and disk I/O for commands that do not require the package manager, such asng help, and certain shell completions.As part of this refactoring:
packageManagerproperty inCommandContextis now aPromise<PackageManager>.awaitthe package manager's lazy initialization.@angular-devkit/core,CoreSchemaRegistrywas updated to rename_sourceMapto_sourceProviderand optimize the resolution of smart defaults by awaiting values during the_setoperation.