-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(2396): metadata checks should run on prerelease tags (#2397)
## Proposed change - Use `npm view <package> versions` instead of `npm view <package@range> version` to include the prerelease tags - Extends the `NpmSemverResolver` used for yarn to include prerelease tags ## Related issues <!-- Please make sure to follow the [contribution guidelines](https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md) --> <!-- * 🐛 Fix #issue --> * 🐛 Fix resolves #2396 <!-- * 🚀 Feature #issue --> <!-- * 🚀 Feature resolves #issue --> <!-- * Pull Request #issue -->
- Loading branch information
Showing
6 changed files
with
143 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
.../extractors/src/core/comparator/package-managers-extractors/custom-npm-semver-resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { npmHttpUtils, NpmSemverFetcher, NpmSemverResolver } from '@yarnpkg/plugin-npm'; | ||
import { Descriptor, miscUtils, Package, ResolveOptions, structUtils } from '@yarnpkg/core'; | ||
import { Range, SemVer, valid } from 'semver'; | ||
|
||
/** | ||
* Unexposed constant from yarn https://github.com/yarnpkg/berry/blob/master/packages/plugin-npm/sources/constants.ts | ||
*/ | ||
const PROTOCOL = `npm:`; | ||
|
||
/** | ||
* Extends NpmSeverResolver to support prerelease tags | ||
* Check original code from https://github.com/yarnpkg/berry/blob/master/packages/plugin-npm/sources/NpmSemverResolver.ts | ||
*/ | ||
export class CustomNpmSemverResolver extends NpmSemverResolver { | ||
/** @inheritDoc */ | ||
public override async getCandidates(descriptor: Descriptor, _dependencies: Record<string, Package>, opts: ResolveOptions) { | ||
const range = new Range(descriptor.range.slice(PROTOCOL.length), {includePrerelease: true}); | ||
const registryData = await npmHttpUtils.getPackageMetadata(descriptor, { | ||
cache: opts.fetchOptions?.cache, | ||
project: opts.project, | ||
version: valid(range.raw) ? range.raw : undefined | ||
}); | ||
|
||
const candidates = miscUtils.mapAndFilter(Object.keys(registryData.versions), (version) => { | ||
try { | ||
const candidate = new SemVer(version, {includePrerelease: true}); | ||
if (range.test(candidate)) { | ||
return candidate; | ||
} | ||
} catch { } | ||
|
||
return miscUtils.mapAndFilter.skip; | ||
}); | ||
|
||
const noDeprecatedCandidates = candidates.filter((version) => !registryData.versions[version.raw].deprecated); | ||
|
||
// If there are versions that aren't deprecated, use them | ||
const finalCandidates = noDeprecatedCandidates.length > 0 | ||
? noDeprecatedCandidates | ||
: candidates; | ||
|
||
finalCandidates.sort((a, b) => -a.compare(b)); | ||
|
||
return finalCandidates.map(version => { | ||
const versionLocator = structUtils.makeLocator(descriptor, `${PROTOCOL}${version.raw}`); | ||
const archiveUrl = registryData.versions[version.raw].dist.tarball; | ||
|
||
if (NpmSemverFetcher.isConventionalTarballUrl(versionLocator, archiveUrl, {configuration: opts.project.configuration})) { | ||
return versionLocator; | ||
} else { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
return structUtils.bindLocator(versionLocator, {__archiveUrl: archiveUrl}); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.