use crate::{GitSource, LocalSource, LockedPackage};
use std::path::PathBuf;
pub(super) fn local_git_source_from_resolved(resolved: &str) -> Option<LocalSource> {
let (url, committish, subpath) = crate::parse_git_spec(resolved)?;
let resolved = committish.clone()?;
Some(LocalSource::Git(GitSource {
url,
committish,
resolved,
integrity: None,
subpath,
}))
}
pub(super) fn local_file_source_from_resolved(resolved: &str) -> Option<LocalSource> {
let rest = resolved.strip_prefix("file:")?;
let path = PathBuf::from(rest);
if LocalSource::path_looks_like_tarball(&path) {
Some(LocalSource::Tarball(path))
} else {
Some(LocalSource::Directory(path))
}
}
pub(super) fn npm_resolved_field(pkg: &LockedPackage) -> Option<String> {
pkg.tarball_url.clone().or_else(|| match &pkg.local_source {
Some(LocalSource::Git(git)) => {
let url = if git.url.starts_with("git://") || git.url.starts_with("git+") {
git.url.clone()
} else {
format!("git+{}", git.url)
};
match &git.subpath {
Some(subpath) => Some(format!("{url}#{}&path:/{subpath}", git.resolved)),
None => Some(format!("{url}#{}", git.resolved)),
}
}
_ => None,
})
}