mecha10_cli/services/process/
resolve.rs1use super::ProcessService;
8use crate::paths;
9use anyhow::Result;
10
11impl ProcessService {
12 pub fn find_global_binary(node_name: &str) -> Option<std::path::PathBuf> {
26 let cargo_bin = paths::user::cargo_bin(node_name);
27 if cargo_bin.exists() && cargo_bin.is_file() {
28 return Some(cargo_bin);
29 }
30
31 let mecha10_bin = paths::user::bin(node_name);
32 if mecha10_bin.exists() && mecha10_bin.is_file() {
33 return Some(mecha10_bin);
34 }
35
36 None
37 }
38
39 pub fn resolve_node_binary(node_name: &str, is_monorepo_node: bool, project_name: &str) -> String {
55 if is_monorepo_node {
56 if let Some(global_path) = Self::find_global_binary(node_name) {
57 return global_path.to_string_lossy().to_string();
58 }
59 }
60
61 Self::get_local_binary_path(node_name, is_monorepo_node, project_name)
62 }
63
64 fn get_local_binary_path(node_name: &str, is_monorepo_node: bool, project_name: &str) -> String {
66 if is_monorepo_node {
67 paths::target_path("release", project_name)
68 } else {
69 paths::target_path("release", node_name)
70 }
71 }
72
73 pub fn resolve_node_runner_path() -> String {
83 if let Some(global_path) = Self::find_global_binary("mecha10-node-runner") {
84 return global_path.to_string_lossy().to_string();
85 }
86
87 "mecha10-node-runner".to_string()
88 }
89
90 pub(super) fn get_node_version_from_config(&self, node_identifier: &str) -> Option<String> {
92 use crate::types::ProjectConfig;
93
94 let config_path = std::path::Path::new(paths::PROJECT_CONFIG);
95 if !config_path.exists() {
96 return None;
97 }
98
99 let config_content = std::fs::read_to_string(config_path).ok()?;
100 let config: ProjectConfig = serde_json::from_str(&config_content).ok()?;
101
102 config.nodes.get_version(node_identifier).map(|s| s.to_string())
103 }
104
105 pub(super) fn disk_logging_enabled(&self) -> bool {
120 use crate::types::ProjectConfig;
121
122 let config_path = std::path::Path::new(paths::PROJECT_CONFIG);
123 if !config_path.exists() {
124 return true;
125 }
126
127 let config_content = match std::fs::read_to_string(config_path) {
128 Ok(c) => c,
129 Err(_) => return true,
130 };
131
132 let config: ProjectConfig = match serde_json::from_str(&config_content) {
133 Ok(c) => c,
134 Err(_) => return true,
135 };
136
137 config.environments.disk_logging_enabled()
138 }
139
140 pub(super) fn is_local_project_node(&self, node_name: &str) -> bool {
142 use crate::types::{NodeSource, ProjectConfig};
143
144 let config_path = std::path::Path::new(paths::PROJECT_CONFIG);
145 if !config_path.exists() {
146 return false;
147 }
148
149 let config_content = match std::fs::read_to_string(config_path) {
150 Ok(c) => c,
151 Err(_) => return false,
152 };
153
154 let config: ProjectConfig = match serde_json::from_str(&config_content) {
155 Ok(c) => c,
156 Err(_) => return false,
157 };
158
159 config
160 .nodes
161 .find_by_name(node_name)
162 .map(|spec| spec.source == NodeSource::Project)
163 .unwrap_or(false)
164 }
165
166 pub(super) fn resolve_cli_binary() -> Result<String> {
168 if let Some(path) = Self::find_global_binary("mecha10") {
169 return Ok(path.to_string_lossy().to_string());
170 }
171
172 let locations = [paths::user::local_bin("mecha10"), paths::user::cargo_bin("mecha10")];
173
174 for path in &locations {
175 if path.exists() {
176 return Ok(path.to_string_lossy().to_string());
177 }
178 }
179
180 if let Ok(exe) = std::env::current_exe() {
181 return Ok(exe.to_string_lossy().to_string());
182 }
183
184 anyhow::bail!("Could not find mecha10 CLI binary. Ensure it's installed and in your PATH.")
185 }
186
187 pub fn is_node_runner_available() -> bool {
189 Self::find_global_binary("mecha10-node-runner").is_some()
190 }
191}