Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add package json module #1

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ repository = "https://github.com/anonrig/pacquet"
pacquet_cli = { version = "0.0.1", path = "crates/cli" }
pacquet_registry = { version = "0.0.1", path = "crates/registry" }
pacquet_tarball = { version = "0.0.1", path = "crates/tarball" }
pacquet_package_json = { version = "0.0.1", path = "crates/package_json" }

clap = { version = "4" }
flate2 = { version = "1.0.26" }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ TODO:

Commands:

- [x] init: `cargo run -- init`
- [ ] add: `cargo run -- add fast-querystring`
- [ ] flags...
- [ ] remove
Expand Down
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ path = "src/main.rs"

[dependencies]
pacquet_registry = { workspace = true }
pacquet_package_json = { workspace = true }

clap = { workspace = true }
rayon = { workspace = true }
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use clap::{Arg, Command};

fn init_package_command() -> Command {
Command::new("init").about("Initialize a package")
}

fn add_package_command() -> Command {
Command::new("add").about("Add a package").arg(Arg::new("package"))
}
Expand All @@ -10,5 +14,5 @@ pub fn get_commands() -> Command {
.version("alpha")
.author("Yagiz Nizipli")
.arg_required_else_help(true)
.subcommand(add_package_command())
.subcommands([add_package_command(), init_package_command()])
}
4 changes: 4 additions & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod commands;

use pacquet_package_json::PackageJson;
use pacquet_registry::RegistryManager;

use crate::commands::get_commands;
Expand All @@ -24,6 +25,9 @@ pub async fn run_commands() {
if let Some(package_name) = subcommand.get_one::<String>("package") {
registry_manager.get_package(package_name).await.expect("TODO: panic message");
}
} else if matches.subcommand_matches("init").is_some() {
let pkg = PackageJson::from_current_directory();
pkg.create_if_needed();
}
}

Expand Down
15 changes: 15 additions & 0 deletions crates/package_json/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "pacquet_package_json"
version = "0.0.1"
publish = false
authors.workspace = true
description.workspace = true
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }
54 changes: 54 additions & 0 deletions crates/package_json/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::{collections::HashMap, env, ffi::OsStr, io::Write, path::PathBuf};

use serde_json;

pub struct PackageJson {
path: PathBuf,
}

impl PackageJson {
pub fn from_current_directory() -> Self {
PackageJson {
path: env::current_dir()
.expect("current directory should exist")
.as_path()
.join("package.json"),
}
}

pub fn from_path(path: PathBuf) -> Self {
PackageJson { path }
}

pub fn create_if_needed(&self) {
if self.path.exists() {
return;
}

let folder_name = self
.path
.parent()
.expect("should have a parent folder")
.file_name()
.unwrap_or(OsStr::new(""));

let mut file = std::fs::File::create(&self.path).unwrap();
let empty_object: HashMap<String, String> = HashMap::new();
let mut scripts = HashMap::new();
scripts.insert("test", "echo \"Error: no test specified\" && exit 1");

let mut contents = HashMap::new();
contents.insert("name", serde_json::to_value(folder_name.to_str().unwrap()).unwrap());
contents.insert("version", serde_json::to_value("1.0.0").unwrap());
contents.insert("description", serde_json::to_value("").unwrap());
contents.insert("main", serde_json::to_value("index.js").unwrap());
contents.insert("author", serde_json::to_value("").unwrap());
contents.insert("license", serde_json::to_value("MIT").unwrap());
contents.insert("scripts", serde_json::to_value(&scripts).unwrap());
contents.insert("dependencies", serde_json::to_value(&empty_object).unwrap());
contents.insert("devDependencies", serde_json::to_value(&empty_object).unwrap());

let serialized = serde_json::to_string_pretty(&contents).unwrap();
file.write_all(serialized.as_bytes()).unwrap();
}
}
3 changes: 1 addition & 2 deletions crates/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ impl RegistryManager {
let extract_destination = node_modules.join(package.get_latest_tag());

if !package_folder.exists() {
std::fs::create_dir_all(package_folder.as_path())
.expect("package folder creation failed");
std::fs::create_dir(package_folder.as_path()).expect("package folder creation failed");
}

if !node_modules.exists() {
Expand Down