Skip to content

Commit f59bb4d

Browse files
WIP install command
1 parent 32845b9 commit f59bb4d

File tree

9 files changed

+44
-11
lines changed

9 files changed

+44
-11
lines changed

Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/dinopkg-cli/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ tokio = { version = "1.38.0", features = [
1515
dinopkg-package-json = { path = "../dinopkg-package-json", features = [
1616
"tokio",
1717
] }
18+
dinopkg-npm-registry = { path = "../dinopkg-npm-registry" }
1819
exitcode = "1.1.2"
1920
env_logger = "0.11.3"
2021
dialoguer = "0.11.0"
@@ -25,3 +26,4 @@ serde_json = "1.0.120"
2526
syntect = "5.2.0"
2627
validate_package_name = { path = "../validate_package_name" }
2728
spdx = "0.10.6"
29+
reqwest = "0.12.5"

crates/dinopkg-cli/src/command.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clap::{Parser, Subcommand};
22

33
pub mod init;
4+
pub mod install;
45
pub mod run;
56

67
#[derive(Parser)]
@@ -26,4 +27,11 @@ pub enum Command {
2627
/// Create a package.json file
2728
#[command(aliases = ["create", "innit"])]
2829
Init,
30+
31+
/// Installs dependencies for `package.json`
32+
#[command(aliases = ["i", "add"])]
33+
Install {
34+
/// The name of the package to install
35+
name: String,
36+
},
2937
}

crates/dinopkg-cli/src/command/init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use camino::Utf8PathBuf;
44
use color_eyre::eyre::eyre;
55
use color_eyre::Result;
66
use dialoguer::{theme::ColorfulTheme, Confirm, Input};
7-
use dinopkg_package_json::PackageJson;
7+
use dinopkg_package_json::{PackageJson, AuthorObjOrString};
88
use gix_config::File as GitConfigFile;
99
use maplit::hashmap;
1010
use owo_colors::OwoColorize;
@@ -96,7 +96,7 @@ pub async fn init() -> Result<()> {
9696
let package_json = PackageJson {
9797
name: package_name,
9898
version,
99-
author: Some(author),
99+
author: Some(AuthorObjOrString::String(author)),
100100
repository: Some(git_repository),
101101
license: Some(license),
102102
description: Some(description),
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use color_eyre::Result;
2+
use dinopkg_npm_registry::PackageInfo;
3+
4+
pub async fn install_cmd(name: String) -> Result<()> {
5+
let client = reqwest::Client::new();
6+
7+
let package_info = PackageInfo::from_name(&name, &client).await?;
8+
dbg!(package_info);
9+
Ok(())
10+
}

crates/dinopkg-cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ async fn main() -> Result<()> {
1919
Command::Run { script_name } => command::run::run(script_name).await?,
2020
Command::Test => command::run::run(Some("test".into())).await?,
2121
Command::Init => command::init::init().await?,
22+
Command::Install { name } => command::install::install_cmd(name).await?,
2223
}
2324
Ok(())
2425
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use std::collections::HashMap;
22

3-
use serde::{Serialize, Deserialize};
43
use dinopkg_package_json::PackageJson;
4+
use serde::{Deserialize, Serialize};
55

66
const NPM_REGISTRY_ROOT_URL: &str = "https://registry.npmjs.org";
77

8-
#[derive(Serialize, Deserialize)]
8+
#[derive(Serialize, Deserialize, Debug)]
99
pub struct PackageInfo {
1010
/// The name of the package, for example `discord.js`.
1111
name: String,
1212

1313
/// A map of versions to their respective version info.
14-
///
14+
///
1515
/// The key is the version string (e.g. `0.1.0`), and the value is the version's `package.json` info.
1616
versions: HashMap<String, PackageJson>,
1717
}
@@ -23,10 +23,13 @@ pub enum Error {
2323
}
2424

2525
impl PackageInfo {
26-
pub async fn get_package_info(package_name: &str, client: &reqwest::Client) -> Result<PackageInfo, Error> {
26+
pub async fn from_name(
27+
package_name: &str,
28+
client: &reqwest::Client,
29+
) -> Result<PackageInfo, Error> {
2730
let url = format!("{NPM_REGISTRY_ROOT_URL}/{package_name}");
2831
let response = client.get(&url).send().await?;
2932
let package_info = response.json::<PackageInfo>().await?;
3033
Ok(package_info)
3134
}
32-
}
35+
}

crates/dinopkg-package-json/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dinopkg-package-json"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55

66
[dependencies]

crates/dinopkg-package-json/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ mod util;
88

99
#[serde_as]
1010
#[skip_serializing_none]
11-
#[derive(Serialize, Deserialize)]
11+
#[derive(Serialize, Deserialize, Debug)]
1212
#[serde(rename_all = "camelCase")]
1313
pub struct PackageJson {
1414
pub name: String,
1515
pub version: String,
16-
pub author: Option<String>,
16+
pub author: Option<AuthorObjOrString>,
1717
#[serde(default = "default_as_false")]
1818
#[serde(skip_serializing_if = "is_false")]
1919
pub private: bool,
@@ -28,6 +28,13 @@ pub struct PackageJson {
2828
pub dev_dependencies: Option<Dependencies>,
2929
}
3030

31+
#[derive(Serialize, Deserialize, Debug)]
32+
#[serde(untagged)]
33+
pub enum AuthorObjOrString {
34+
Author { name: String, url: Option<String> },
35+
String(String),
36+
}
37+
3138
// serde :/
3239
#[allow(clippy::trivially_copy_pass_by_ref)]
3340
#[inline(always)]

0 commit comments

Comments
 (0)