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

View file

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

View file

@ -1,6 +1,7 @@
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
pub mod init; pub mod init;
pub mod install;
pub mod run; pub mod run;
#[derive(Parser)] #[derive(Parser)]
@ -26,4 +27,11 @@ pub enum Command {
/// Create a package.json file /// Create a package.json file
#[command(aliases = ["create", "innit"])] #[command(aliases = ["create", "innit"])]
Init, 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::eyre::eyre;
use color_eyre::Result; use color_eyre::Result;
use dialoguer::{theme::ColorfulTheme, Confirm, Input}; use dialoguer::{theme::ColorfulTheme, Confirm, Input};
use dinopkg_package_json::PackageJson; use dinopkg_package_json::{PackageJson, AuthorObjOrString};
use gix_config::File as GitConfigFile; use gix_config::File as GitConfigFile;
use maplit::hashmap; use maplit::hashmap;
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
@ -96,7 +96,7 @@ pub async fn init() -> Result<()> {
let package_json = PackageJson { let package_json = PackageJson {
name: package_name, name: package_name,
version, version,
author: Some(author), author: Some(AuthorObjOrString::String(author)),
repository: Some(git_repository), repository: Some(git_repository),
license: Some(license), license: Some(license),
description: Some(description), 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::Run { script_name } => command::run::run(script_name).await?,
Command::Test => command::run::run(Some("test".into())).await?, Command::Test => command::run::run(Some("test".into())).await?,
Command::Init => command::init::init().await?, Command::Init => command::init::init().await?,
Command::Install { name } => command::install::install_cmd(name).await?,
} }
Ok(()) Ok(())
} }

View file

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

View file

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

View file

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