Add install scaffold, dist-keys

This commit is contained in:
SkyfallWasTaken 2024-07-30 11:26:24 +01:00
parent e646347714
commit c5dd2c6bba
4 changed files with 40 additions and 5 deletions

View file

@ -1,10 +1,39 @@
use color_eyre::Result;
use dinopkg_npm_registry::PackageInfo;
use owo_colors::OwoColorize;
pub async fn install_cmd(name: String) -> Result<()> {
let client = reqwest::Client::new();
let package_info = PackageInfo::from_name(&name, &client).await?;
dbg!(package_info);
let latest_version = package_info.dist_keys.get("latest").unwrap();
let latest_package = package_info.versions.get(latest_version).unwrap();
print_dep_version(&latest_package.name, &latest_package.version, false);
if let Some(deps) = &latest_package.dependencies {
for (dep_name, dep_version) in deps {
print_dep_version(dep_name, dep_version, false);
}
}
if let Some(deps) = &latest_package.dev_dependencies {
for (dep_name, dep_version) in deps {
print_dep_version(dep_name, dep_version, true);
}
}
Ok(())
}
fn print_dep_version(name: &String, version: &String, is_dev: bool) {
println!(
" {} {} {}{}",
"Installing".green().bold(),
name,
version,
if is_dev {
format!("{}", " (dev)".dimmed().bold())
} else {
"".into()
}
);
}

View file

@ -8,12 +8,15 @@ const NPM_REGISTRY_ROOT_URL: &str = "https://registry.npmjs.org";
#[derive(Serialize, Deserialize, Debug)]
pub struct PackageInfo {
/// The name of the package, for example `discord.js`.
name: String,
pub name: String,
/// A map of versions to their respective version info.
///
/// The key is the version string (e.g. `0.1.0`), and the value is the version's `package.json` info.
versions: HashMap<String, PackageJson>,
pub versions: HashMap<String, PackageJson>,
#[serde(rename = "dist-tags")]
pub dist_keys: HashMap<String, String>,
}
#[derive(thiserror::Error, Debug)]

View file

@ -13,3 +13,6 @@ tokio = { version = "1.38.0", features = ["fs"], optional = true }
[dev-dependencies]
maplit = "1.0.2"
pretty_assertions = "1.4.0"
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] }

View file

@ -44,14 +44,14 @@ pub enum RepositoryVariant {
// serde :/
#[allow(clippy::trivially_copy_pass_by_ref)]
#[cfg(not(tarpaullin_include))]
#[cfg(not(tarpaulin_include))]
#[inline(always)]
fn is_false(value: &bool) -> bool {
!value
}
#[inline(always)]
#[cfg(not(tarpaullin_include))]
#[cfg(not(tarpaulin_include))]
const fn default_as_false() -> bool {
false
}