From 1b9e1bc6f5fe4cf7bba35736b715145e81ef8d1c Mon Sep 17 00:00:00 2001 From: itschip Date: Sat, 24 May 2025 22:29:27 +0200 Subject: [PATCH] feat: add resources directory validation for init and install commands --- src/installer/mod.rs | 4 ++++ src/main.rs | 57 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/installer/mod.rs b/src/installer/mod.rs index 9aa4849..c4de38a 100644 --- a/src/installer/mod.rs +++ b/src/installer/mod.rs @@ -56,6 +56,10 @@ pub fn init_fxpkg(working_dir: &str) { let mut file = std::io::BufWriter::new(file); file.write_all(stub_content.as_bytes()) .expect("Failed to write to modules.json file"); + + println!("Created modules.json file successfully."); + } else { + println!("modules.json file already exists."); } } diff --git a/src/main.rs b/src/main.rs index 8090492..a3e107c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ pub mod registry; use clap::{Args, Parser, Subcommand}; use std::env; +use std::path::Path; #[derive(Debug, Parser)] #[command(name = "fxpkg")] @@ -44,6 +45,32 @@ struct InstallArgs { name: String, } +fn find_resources_directory(current_path: &Path) -> Result { + let mut path = current_path.to_path_buf(); + + // Check if current directory is already "resources" + if path.file_name().and_then(|n| n.to_str()) == Some("resources") { + return Ok(path.to_string_lossy().to_string()); + } + + // Traverse upwards to find "resources" directory + loop { + let resources_path = path.join("resources"); + if resources_path.exists() && resources_path.is_dir() { + return Ok(resources_path.to_string_lossy().to_string()); + } + + // Move to parent directory + if let Some(parent) = path.parent() { + path = parent.to_path_buf(); + } else { + break; + } + } + + Err("Could not find 'resources' directory. Please run 'fxpkg init' from within or above a 'resources' directory.".to_string()) +} + #[tokio::main] async fn main() { dotenv::dotenv().ok(); @@ -53,16 +80,30 @@ async fn main() { match args.commands { Commands::Install(package) => { - let mut pkg_installer = installer::PackageInstall::new(); - pkg_installer - .install(pwd.to_str().unwrap(), &package.name) - .await; + match find_resources_directory(&pwd) { + Ok(resources_path) => { + let mut pkg_installer = installer::PackageInstall::new(); + pkg_installer + .install(&resources_path, &package.name) + .await; + } + Err(error_msg) => { + eprintln!("Error: {}", error_msg); + std::process::exit(1); + } + } } Commands::Init => { - let pwd = env::current_dir().unwrap(); - let path = pwd.to_str().unwrap(); - - installer::init_fxpkg(path); + match find_resources_directory(&pwd) { + Ok(resources_path) => { + installer::init_fxpkg(&resources_path); + println!("Initialized fxpkg in: {}", resources_path); + } + Err(error_msg) => { + eprintln!("Error: {}", error_msg); + std::process::exit(1); + } + } } } }