Skip to content

Commit

Permalink
feat: add latest field to package metadata (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored Feb 29, 2024
1 parent 2d3ab2a commit db9fc94
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl serde::Serialize for ScopedPackageName {
/// A package version, like '1.2.3' or '0.0.0-foo'. The version is not prefixed
/// with a v.
/// The version must be a valid semver version.
#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Version(pub deno_semver::Version);

impl Version {
Expand Down
9 changes: 8 additions & 1 deletion api/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::collections::HashMap;
pub struct PackageMetadata {
pub scope: ScopeName,
pub name: PackageName,
pub latest: Option<Version>,
pub versions: HashMap<Version, PackageMetadataVersion>,
}

Expand All @@ -39,10 +40,16 @@ impl PackageMetadata {
scope: &ScopeName,
package_name: &PackageName,
) -> anyhow::Result<Self> {
let versions = db.list_package_versions(scope, package_name).await?;
let mut versions = db.list_package_versions(scope, package_name).await?;
versions.sort_by(|(a, _), (b, _)| b.version.cmp(&a.version));
let latest = versions
.iter()
.find(|(v, _)| !v.is_yanked && v.version.0.pre.is_empty())
.map(|(v, _)| v.version.clone());
let mut out = Self {
scope: scope.to_owned(),
name: package_name.to_owned(),
latest,
versions: HashMap::new(),
};
for (version, _) in versions {
Expand Down
1 change: 1 addition & 0 deletions api/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ pub mod tests {
serde_json::from_slice(&json).unwrap()
};
assert_eq!(package_metadata.name, package_name);
assert_eq!(package_metadata.latest, Some(version));
assert_eq!(package_metadata.versions.len(), 1);
}

Expand Down

0 comments on commit db9fc94

Please sign in to comment.