| skipped 8 lines |
9 | 9 | | use std::collections::HashMap; |
10 | 10 | | use std::env; |
11 | 11 | | use std::fs::read_to_string; |
12 | | - | use std::path::PathBuf; |
| 12 | + | use std::path::{Path, PathBuf}; |
13 | 13 | | use std::process::{exit, Command}; |
14 | 14 | | |
15 | 15 | | // Get PATH env and join it with bin_dir |
| skipped 13 lines |
29 | 29 | | // A function to find the closest file |
30 | 30 | | // Starting from current directory |
31 | 31 | | // Recusively until it finds the file or reach root directory |
32 | | - | fn find_closest_files(_current_dir: &PathBuf, name: &str, stop_on_first: bool) -> Vec<PathBuf> { |
| 32 | + | fn find_closest_files(_current_dir: &Path, name: &str, stop_on_first: bool) -> Vec<PathBuf> { |
33 | 33 | | let mut closest_file: Vec<PathBuf> = Vec::new(); |
34 | | - | let mut current_dir = _current_dir.clone(); |
| 34 | + | let mut current_dir = Path::new(_current_dir); |
35 | 35 | | loop { |
36 | 36 | | let path = current_dir.join(name); |
37 | 37 | | |
| skipped 4 lines |
42 | 42 | | } |
43 | 43 | | } |
44 | 44 | | match current_dir.parent() { |
45 | | - | Some(p) => current_dir = p.to_path_buf(), |
| 45 | + | Some(p) => current_dir = p, |
46 | 46 | | None => break, |
47 | 47 | | } |
48 | 48 | | } |
| skipped 25 lines |
74 | 74 | | exit(status.code().unwrap_or(1)); |
75 | 75 | | } |
76 | 76 | | |
77 | | - | fn resolve_bin_path(bin_name: &str, dirs: &Vec<PathBuf>) -> Option<PathBuf> { |
| 77 | + | fn resolve_bin_path(bin_name: &str, dirs: &[PathBuf]) -> Option<PathBuf> { |
78 | 78 | | for dir in dirs { |
79 | 79 | | let path = dir.join(bin_name); |
80 | 80 | | if path.exists() { |
| skipped 84 lines |
165 | 165 | | let mut forwarded = app_args.forwarded.clone(); |
166 | 166 | | |
167 | 167 | | if !app_args.interactive && app_args.command == "run" && script_name.is_empty() { |
168 | | - | println!("\nAvailable scripts:\n"); |
169 | | - | for (name, value) in scripts.unwrap() { |
170 | | - | println!("{}", name); |
171 | | - | println!(" {}", value); |
| 168 | + | match scripts { |
| 169 | + | Some(scripts) => { |
| 170 | + | println!("\n{}:\n", Style::new().bold().paint("Available scripts")); |
| 171 | + | for (name, value) in scripts { |
| 172 | + | println!("{}", Purple.paint(name)); |
| 173 | + | println!(" {}", value.as_str().unwrap()); |
| 174 | + | } |
| 175 | + | return; |
| 176 | + | } |
| 177 | + | None => { |
| 178 | + | eprintln!("No scripts found"); |
| 179 | + | exit(1); |
| 180 | + | } |
172 | 181 | | } |
173 | | - | return; |
174 | 182 | | } |
175 | 183 | | |
176 | 184 | | if !script_name.is_empty() && app_args.interactive { |
| skipped 41 lines |
218 | 226 | | |
219 | 227 | | let npm_script = scripts |
220 | 228 | | .and_then(|s| s.get(script_name.as_str())) |
221 | | - | .and_then(|script| { |
| 229 | + | .map(|script| { |
222 | 230 | | let script = script.as_str().map(|script| script.to_string()); |
223 | | - | Some(script.unwrap_or_default()) |
| 231 | + | script.unwrap_or_default() |
224 | 232 | | }); |
225 | | - | if npm_script.is_some() { |
226 | | - | let script = replace_run_commands(&npm_script.unwrap()); |
| 233 | + | if let Some(script) = npm_script { |
| 234 | + | let script = replace_run_commands(&script); |
227 | 235 | | print_script_info(&script_name, &script, &forwarded); |
228 | 236 | | let envs = HashMap::from([("PATH".to_string(), get_path_env(bin_dirs))]); |
229 | 237 | | run_command( |
| skipped 6 lines |
236 | 244 | | return; |
237 | 245 | | } |
238 | 246 | | let resolved_bin = resolve_bin_path(script_name.as_str(), &bin_dirs); |
239 | | - | if resolved_bin.is_some() { |
240 | | - | let bin_path = resolved_bin.unwrap(); |
241 | | - | print_script_info(&script_name, &bin_path.to_str().unwrap(), &forwarded); |
| 247 | + | if let Some(bin_path) = resolved_bin { |
| 248 | + | print_script_info(&script_name, bin_path.to_str().unwrap(), &forwarded); |
242 | 249 | | let envs = HashMap::from([("PATH".to_string(), get_path_env(bin_dirs))]); |
243 | 250 | | run_command( |
244 | 251 | | &[bin_path.to_str().unwrap(), &forwarded], |
| skipped 14 lines |