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 e4631e4

Browse files
committed
refactor(@angular/cli): isolate ng add version filtering logic
This commit refactors the `findCompatiblePackageVersionTask` method within the `ng add` command to improve its structure and clarity. The logic for filtering and sorting package versions has been extracted into a new private helper method, `#getPotentialVersionManifests`. This change separates the data preparation from the main orchestration logic, making the primary method easier to read and maintain.
1 parent 5e53409 commit e4631e4

File tree

1 file changed

+39
-31
lines changed
  • packages/angular/cli/src/commands/add

1 file changed

+39
-31
lines changed

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

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { assertIsError } from '../../utilities/error';
2828
import {
2929
NgAddSaveDependency,
3030
PackageManifest,
31+
PackageMetadata,
3132
fetchPackageManifest,
3233
fetchPackageMetadata,
3334
} from '../../utilities/package-metadata';
@@ -288,40 +289,16 @@ export default class AddCommandModule
288289
}
289290

290291
// Allow prelease versions if the CLI itself is a prerelease
291-
const allowPrereleases = prerelease(VERSION.full);
292-
293-
const versionExclusions = packageVersionExclusions[packageMetadata.name];
294-
const versionManifests = Object.values(packageMetadata.versions).filter(
295-
(value: PackageManifest) => {
296-
// Already checked the 'latest' version
297-
if (latestManifest.version === value.version) {
298-
return false;
299-
}
300-
// Prerelease versions are not stable and should not be considered by default
301-
if (!allowPrereleases && prerelease(value.version)) {
302-
return false;
303-
}
304-
// Deprecated versions should not be used or considered
305-
if (value.deprecated) {
306-
return false;
307-
}
308-
// Excluded package versions should not be considered
309-
if (
310-
versionExclusions &&
311-
satisfies(value.version, versionExclusions, { includePrerelease: true })
312-
) {
313-
return false;
314-
}
315-
316-
return true;
317-
},
318-
);
319-
320-
// Sort in reverse SemVer order so that the newest compatible version is chosen
321-
versionManifests.sort((a, b) => compare(b.version, a.version, true));
292+
const allowPrereleases = !!prerelease(VERSION.full);
293+
const versionManifests = this.#getPotentialVersionManifests(packageMetadata, allowPrereleases);
322294

323295
let found = false;
324296
for (const versionManifest of versionManifests) {
297+
// Already checked the 'latest' version
298+
if (latestManifest?.version === versionManifest.version) {
299+
continue;
300+
}
301+
325302
const conflicts = await this.getPeerDependencyConflicts(versionManifest);
326303
if (conflicts) {
327304
if (options.verbose || rejectionReasons.length < DEFAULT_CONFLICT_DISPLAY_LIMIT) {
@@ -354,6 +331,37 @@ export default class AddCommandModule
354331
}
355332
}
356333

334+
#getPotentialVersionManifests(
335+
packageMetadata: PackageMetadata,
336+
allowPrereleases: boolean,
337+
): PackageManifest[] {
338+
const versionExclusions = packageVersionExclusions[packageMetadata.name];
339+
const versionManifests = Object.values(packageMetadata.versions).filter(
340+
(value: PackageManifest) => {
341+
// Prerelease versions are not stable and should not be considered by default
342+
if (!allowPrereleases && prerelease(value.version)) {
343+
return false;
344+
}
345+
// Deprecated versions should not be used or considered
346+
if (value.deprecated) {
347+
return false;
348+
}
349+
// Excluded package versions should not be considered
350+
if (
351+
versionExclusions &&
352+
satisfies(value.version, versionExclusions, { includePrerelease: true })
353+
) {
354+
return false;
355+
}
356+
357+
return true;
358+
},
359+
);
360+
361+
// Sort in reverse SemVer order so that the newest compatible version is chosen
362+
return versionManifests.sort((a, b) => compare(b.version, a.version, true));
363+
}
364+
357365
private async loadPackageInfoTask(
358366
context: AddCommandTaskContext,
359367
task: AddCommandTaskWrapper,

0 commit comments

Comments
(0)

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