DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/ProcessManagement/NodeResolver/TryNvm.rs
1//! nvm lookup. `NVM_BIN` wins (set inside an nvm-sourced shell). Fallback
2//! walks `$NVM_DIR/versions/node` and picks the lexicographically largest
3//! version (rough proxy for "latest installed").
4
5use std::path::PathBuf;
6
7use crate::ProcessManagement::NodeResolver::{NodeExecutableName, NodeSource, ResolvedNode};
8
9pub fn Fn() -> Option<ResolvedNode::Struct> {
10 if let Ok(NvmBin) = std::env::var("NVM_BIN") {
11 let Candidate = PathBuf::from(NvmBin).join(NodeExecutableName::Fn());
12
13 if Candidate.exists() {
14 return Some(ResolvedNode::Struct { Path:Candidate, Source:NodeSource::Enum::Nvm });
15 }
16 }
17
18 let NvmDir = std::env::var("NVM_DIR").ok().or_else(|| {
19 std::env::var("HOME")
20 .ok()
21 .map(|H| PathBuf::from(H).join(".nvm").to_string_lossy().into_owned())
22 })?;
23
24 let VersionsDirectory = PathBuf::from(&NvmDir).join("versions").join("node");
25
26 let Entries = std::fs::read_dir(&VersionsDirectory).ok()?;
27
28 let mut BestCandidate:Option<PathBuf> = None;
29
30 for Entry in Entries.flatten() {
31 let NodePath = Entry.path().join("bin").join(NodeExecutableName::Fn());
32
33 if !NodePath.exists() {
34 continue;
35 }
36
37 BestCandidate = match BestCandidate {
38 Some(Existing) if Existing > NodePath => Some(Existing),
39
40 _ => Some(NodePath),
41 };
42 }
43
44 BestCandidate.map(|Path| ResolvedNode::Struct { Path, Source:NodeSource::Enum::Nvm })
45}