Skip to content
Closed
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
4 changes: 4 additions & 0 deletions src/installer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}

Expand Down
57 changes: 49 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -44,6 +45,32 @@ struct InstallArgs {
name: String,
}

Copy link

Copilot AI May 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a doc comment explaining the lookup strategy and error cases for find_resources_directory to improve readability and maintainability.

Suggested change
/// Locates the "resources" directory starting from the given path.
///
/// This function first checks if the current directory is named "resources".
/// If not, it traverses upwards through parent directories to find a directory
/// named "resources". If the directory is found, its path is returned as a `String`.
///
/// # Errors
/// Returns an `Err` with a descriptive message if the "resources" directory
/// cannot be found. This typically happens if the function is called from a
/// location that is not within or above a "resources" directory.

Copilot uses AI. Check for mistakes.
fn find_resources_directory(current_path: &Path) -> Result<String, String> {
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());
Comment on lines +48 to +60
Copy link

Copilot AI May 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider returning a PathBuf instead of String to preserve path semantics and avoid unnecessary string conversions.

Suggested change
fn find_resources_directory(current_path: &Path) -> Result<String, String> {
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());
fn find_resources_directory(current_path: &Path) -> Result<PathBuf, String> {
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);
}
// 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);

Copilot uses AI. Check for mistakes.
}

// 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();
Expand All @@ -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);
}
}
Comment on lines +83 to +106
Copy link

Copilot AI May 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling logic for both commands is duplicated; extract the match block into a helper to reduce repetition.

Copilot uses AI. Check for mistakes.
}
}
}