WIP install command

This commit is contained in:
SkyfallWasTaken 2024-07-29 15:43:52 +01:00
parent 32845b9006
commit f59bb4def4
9 changed files with 44 additions and 11 deletions

4
Cargo.lock generated
View file

@ -375,12 +375,14 @@ dependencies = [
"clap",
"color-eyre",
"dialoguer",
"dinopkg-npm-registry",
"dinopkg-package-json",
"env_logger",
"exitcode",
"gix-config",
"maplit",
"owo-colors 4.0.0",
"reqwest",
"serde_json",
"spdx",
"syntect",
@ -400,7 +402,7 @@ dependencies = [
[[package]]
name = "dinopkg-package-json"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"serde",
"serde_json",

View file

@ -15,6 +15,7 @@ tokio = { version = "1.38.0", features = [
dinopkg-package-json = { path = "../dinopkg-package-json", features = [
"tokio",
] }
dinopkg-npm-registry = { path = "../dinopkg-npm-registry" }
exitcode = "1.1.2"
env_logger = "0.11.3"
dialoguer = "0.11.0"
@ -25,3 +26,4 @@ serde_json = "1.0.120"
syntect = "5.2.0"
validate_package_name = { path = "../validate_package_name" }
spdx = "0.10.6"
reqwest = "0.12.5"

View file

@ -1,6 +1,7 @@
use clap::{Parser, Subcommand};
pub mod init;
pub mod install;
pub mod run;
#[derive(Parser)]
@ -26,4 +27,11 @@ pub enum Command {
/// Create a package.json file
#[command(aliases = ["create", "innit"])]
Init,
/// Installs dependencies for `package.json`
#[command(aliases = ["i", "add"])]
Install {
/// The name of the package to install
name: String,
},
}

View file

@ -4,7 +4,7 @@ use camino::Utf8PathBuf;
use color_eyre::eyre::eyre;
use color_eyre::Result;
use dialoguer::{theme::ColorfulTheme, Confirm, Input};
use dinopkg_package_json::PackageJson;
use dinopkg_package_json::{PackageJson, AuthorObjOrString};
use gix_config::File as GitConfigFile;
use maplit::hashmap;
use owo_colors::OwoColorize;
@ -96,7 +96,7 @@ pub async fn init() -> Result<()> {
let package_json = PackageJson {
name: package_name,
version,
author: Some(author),
author: Some(AuthorObjOrString::String(author)),
repository: Some(git_repository),
license: Some(license),
description: Some(description),

View file

@ -0,0 +1,10 @@
use color_eyre::Result;
use dinopkg_npm_registry::PackageInfo;
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);
Ok(())
}

View file

@ -19,6 +19,7 @@ async fn main() -> Result<()> {
Command::Run { script_name } => command::run::run(script_name).await?,
Command::Test => command::run::run(Some("test".into())).await?,
Command::Init => command::init::init().await?,
Command::Install { name } => command::install::install_cmd(name).await?,
}
Ok(())
}

View file

@ -1,17 +1,17 @@
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use dinopkg_package_json::PackageJson;
use serde::{Deserialize, Serialize};
const NPM_REGISTRY_ROOT_URL: &str = "https://registry.npmjs.org";
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
pub struct PackageInfo {
/// The name of the package, for example `discord.js`.
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>,
}
@ -23,10 +23,13 @@ pub enum Error {
}
impl PackageInfo {
pub async fn get_package_info(package_name: &str, client: &reqwest::Client) -> Result<PackageInfo, Error> {
pub async fn from_name(
package_name: &str,
client: &reqwest::Client,
) -> Result<PackageInfo, Error> {
let url = format!("{NPM_REGISTRY_ROOT_URL}/{package_name}");
let response = client.get(&url).send().await?;
let package_info = response.json::<PackageInfo>().await?;
Ok(package_info)
}
}
}

View file

@ -1,6 +1,6 @@
[package]
name = "dinopkg-package-json"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
[dependencies]

View file

@ -8,12 +8,12 @@ mod util;
#[serde_as]
#[skip_serializing_none]
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PackageJson {
pub name: String,
pub version: String,
pub author: Option<String>,
pub author: Option<AuthorObjOrString>,
#[serde(default = "default_as_false")]
#[serde(skip_serializing_if = "is_false")]
pub private: bool,
@ -28,6 +28,13 @@ pub struct PackageJson {
pub dev_dependencies: Option<Dependencies>,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum AuthorObjOrString {
Author { name: String, url: Option<String> },
String(String),
}
// serde :/
#[allow(clippy::trivially_copy_pass_by_ref)]
#[inline(always)]