Skip to content

Commit

Permalink
WIP install command
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyfallWasTaken committed Jul 29, 2024
1 parent 32845b9 commit f59bb4d
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/dinopkg-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
8 changes: 8 additions & 0 deletions crates/dinopkg-cli/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::{Parser, Subcommand};

pub mod init;
pub mod install;
pub mod run;

#[derive(Parser)]
Expand All @@ -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,
},
}
4 changes: 2 additions & 2 deletions crates/dinopkg-cli/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
10 changes: 10 additions & 0 deletions crates/dinopkg-cli/src/command/install.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
1 change: 1 addition & 0 deletions crates/dinopkg-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
13 changes: 8 additions & 5 deletions crates/dinopkg-npm-registry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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>,
}
Expand All @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion crates/dinopkg-package-json/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dinopkg-package-json"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand Down
11 changes: 9 additions & 2 deletions crates/dinopkg-package-json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)]
Expand Down

0 comments on commit f59bb4d

Please sign in to comment.