Skip to content

Commit

Permalink
fix: fix getting dist dir
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Oct 19, 2024
1 parent 60a5407 commit 1c707a0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
13 changes: 0 additions & 13 deletions src/get_current_exe_path.rs

This file was deleted.

29 changes: 29 additions & 0 deletions src/get_dist_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::env;
use std::fs;
use std::path::PathBuf;

pub fn get_dist_path() -> Option<PathBuf> {
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
}
20 changes: 13 additions & 7 deletions src/get_todoctor_version.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 3 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

0 comments on commit 1c707a0

Please sign in to comment.