diff --git a/src/get_current_exe_path.rs b/src/get_current_exe_path.rs deleted file mode 100644 index 5995ceb..0000000 --- a/src/get_current_exe_path.rs +++ /dev/null @@ -1,13 +0,0 @@ -use std::env; -use std::path::PathBuf; - -pub fn get_current_exe_path() -> Option { - let current_exe_path = match env::current_exe() { - Ok(path) => path, - Err(e) => { - eprintln!("Error getting current exe path: {:?}", e); - return None; - } - }; - Some(current_exe_path) -} diff --git a/src/get_dist_path.rs b/src/get_dist_path.rs new file mode 100644 index 0000000..234db59 --- /dev/null +++ b/src/get_dist_path.rs @@ -0,0 +1,29 @@ +use std::env; +use std::fs; +use std::path::PathBuf; + +pub fn get_dist_path() -> Option { + let exe_path = match env::current_exe() { + Ok(path) => path, + Err(e) => { + eprintln!("Error getting current exe path: {:?}", e); + return None; + } + }; + + if let Ok(real_path) = fs::read_link(&exe_path) { + let project_root = real_path.parent()?.parent()?.to_path_buf(); + let dist_path = project_root.join("dist"); + if dist_path.exists() { + return Some(dist_path); + } + } + + let project_root = exe_path.parent()?.parent()?.to_path_buf(); + let dist_path = project_root.join("dist"); + if dist_path.exists() { + return Some(dist_path); + } + + None +} diff --git a/src/get_todoctor_version.rs b/src/get_todoctor_version.rs index c07c2b0..7cbe233 100644 --- a/src/get_todoctor_version.rs +++ b/src/get_todoctor_version.rs @@ -1,25 +1,31 @@ -use crate::get_current_exe_path::get_current_exe_path; +use crate::get_dist_path::get_dist_path; use serde_json::Value; use tokio::fs; pub async fn get_todoctor_version() -> Option { - let current_exe_path = match get_current_exe_path() { + let dist_path = match get_dist_path() { Some(path) => path, None => { - eprintln!("Error: could not get current exe path."); + eprintln!("Error: could not get dist directory path."); return None; } }; - let parent_dir = match current_exe_path.parent().and_then(|p| p.parent()) { - Some(path) => path, + let package_json_path = match dist_path.parent() { + Some(parent_dir) => parent_dir.join("package.json"), None => { - eprintln!("Error: could not get parent directory."); + eprintln!("Error: could not get parent directory of dist."); return None; } }; - let package_json_path = parent_dir.join("package.json"); + if !package_json_path.exists() { + eprintln!( + "Error: package.json not found at {}", + package_json_path.display() + ); + return None; + } let package_json_content = match fs::read_to_string(&package_json_path).await { diff --git a/src/lib.rs b/src/lib.rs index f2e1f7f..4f68663 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ pub mod copy_dir_recursive; pub mod exec; pub mod get_comments; pub mod get_current_directory; -pub mod get_current_exe_path; +pub mod get_dist_path; pub mod get_files_list; pub mod get_history; pub mod get_line_from_position; diff --git a/src/main.rs b/src/main.rs index 6083f38..e63cce7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use todoctor::copy_dir_recursive::copy_dir_recursive; use todoctor::exec::exec; use todoctor::get_comments::get_comments; use todoctor::get_current_directory::get_current_directory; -use todoctor::get_current_exe_path::get_current_exe_path; +use todoctor::get_dist_path::get_dist_path; use todoctor::get_files_list::get_files_list; use todoctor::get_history::get_history; use todoctor::get_line_from_position::get_line_from_position; @@ -31,7 +31,6 @@ use tokio::fs; use tokio::sync::Semaphore; const TODOCTOR_DIR: &str = "todoctor"; -const TODO_JSON_FILE: &str = "todoctor/data.json"; const HISTORY_TEMP_FILE: &str = "todo_history_temp.json"; #[derive(Parser, Debug)] @@ -295,12 +294,8 @@ async fn main() { let json_string: String = serde_json::to_string(&json_data) .expect("Error: Could not serialize data"); - let current_exe_path: PathBuf = - get_current_exe_path().expect("Error: Could not get current exe path."); - let script_dir: &Path = current_exe_path - .parent() - .expect("Error: Could not get script directory."); - let dist_path: PathBuf = script_dir.join("../dist"); + let dist_path: PathBuf = + get_dist_path().expect("Error: Could not get current dist path."); copy_dir_recursive(&dist_path, Path::new(TODOCTOR_DIR)) .await @@ -328,6 +323,4 @@ async fn main() { if let Err(e) = open::that(&index_path) { eprintln!("Error: Cannot open index.html: {:?}", e); } - - println!("Data successfully written to {}", TODO_JSON_FILE); }