|
1 | | -use anyhow::{bail, Context, Result}; |
2 | | -#[cfg(windows)] |
3 | | -use fs_extra::file::{copy, CopyOptions}; |
4 | | -use fs_extra::file::{read_to_string, remove}; |
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use fs_extra::file::read_to_string; |
5 | 3 | use lazy_static::lazy_static; |
6 | | -use serde::{de::DeserializeOwned, Deserialize, Serialize}; |
7 | 4 | use serde_json::{from_str, Value}; |
8 | | -use std::{collections::HashMap, env, ffi::OsString, fs, path::PathBuf}; |
| 5 | +use std::{env, ffi::OsString, path::PathBuf}; |
9 | 6 |
|
10 | 7 | lazy_static! { |
11 | 8 | pub static ref NVMD_PATH: Option<PathBuf> = get_nvmd_path().ok(); |
@@ -50,13 +47,13 @@ fn get_installation_path() -> Result<Option<PathBuf>> { |
50 | 47 |
|
51 | 48 | let json_obj: Value = from_str(&setting_content)?; |
52 | 49 | if let Some(directory) = json_obj["directory"].as_str() { |
53 | | - Ok(Some(PathBuf::from(directory))) |
| 50 | + return Ok(Some(PathBuf::from(directory))); |
54 | 51 | } else { |
55 | | - Ok(DEFAULT_INSTALLATION_PATH.clone()) |
| 52 | + return Ok(DEFAULT_INSTALLATION_PATH.clone()); |
56 | 53 | } |
57 | | - } else { |
58 | | - Ok(None) |
59 | 54 | } |
| 55 | + |
| 56 | + Ok(None) |
60 | 57 | } |
61 | 58 |
|
62 | 59 | fn get_default_installation_path() -> Option<PathBuf> { |
@@ -105,132 +102,3 @@ fn get_nvmd_path() -> Result<PathBuf> { |
105 | 102 | home.push(".nvmd"); |
106 | 103 | Ok(home) |
107 | 104 | } |
108 | | - |
109 | | -pub fn package_can_be_removed(name: &String) -> Result<bool> { |
110 | | - if let Some(mut packages_path) = NVMD_PATH.clone() { |
111 | | - packages_path.push("packages.json"); |
112 | | - let packages = read_json::<Packages>(&packages_path)?; |
113 | | - if packages.is_empty() { |
114 | | - return Ok(true); |
115 | | - } |
116 | | - if let Some(package) = packages.get(name) { |
117 | | - if package.is_empty() { |
118 | | - return Ok(true); |
119 | | - } |
120 | | - |
121 | | - if package.len() == 1 && package.contains(&VERSION.clone().unwrap()) { |
122 | | - return Ok(true); |
123 | | - } |
124 | | - } |
125 | | - return Ok(true); |
126 | | - } |
127 | | - Ok(true) |
128 | | -} |
129 | | - |
130 | | -#[derive(Debug, Default, Clone, Deserialize, Serialize)] |
131 | | -#[serde(rename_all = "camelCase")] |
132 | | -pub struct Project { |
133 | | - /// is it active |
134 | | - pub active: bool, |
135 | | - |
136 | | - /// project name |
137 | | - pub name: String, |
138 | | - |
139 | | - /// project path |
140 | | - pub path: String, |
141 | | - |
142 | | - /// the node version of project used |
143 | | - pub version: Option<String>, |
144 | | - |
145 | | - /// create date |
146 | | - pub create_at: Option<String>, |
147 | | - |
148 | | - /// update date |
149 | | - pub update_at: Option<String>, |
150 | | -} |
151 | | - |
152 | | -pub type Packages = HashMap<String, Vec<String>>; |
153 | | - |
154 | | -pub fn read_json<T: DeserializeOwned>(path: &PathBuf) -> Result<T> { |
155 | | - if !path.exists() { |
156 | | - bail!("file not found \"{}\"", path.display()); |
157 | | - } |
158 | | - |
159 | | - let json_str = fs::read_to_string(path) |
160 | | - .with_context(|| format!("failed to read the file \"{}\"", path.display()))?; |
161 | | - |
162 | | - serde_json::from_str::<T>(&json_str).with_context(|| { |
163 | | - format!( |
164 | | - "failed to read the file with json format \"{}\"", |
165 | | - path.display() |
166 | | - ) |
167 | | - }) |
168 | | -} |
169 | | - |
170 | | -#[cfg(unix)] |
171 | | -pub fn link_package(name: &str) -> Result<()> { |
172 | | - use std::os::unix::fs; |
173 | | - if let Some(path) = NVMD_PATH.clone() { |
174 | | - let mut source = path.clone(); |
175 | | - source.push("bin"); |
176 | | - source.push("nvmd"); |
177 | | - let mut alias = path.clone(); |
178 | | - alias.push("bin"); |
179 | | - alias.push(name); |
180 | | - |
181 | | - fs::symlink(source, alias)?; |
182 | | - } |
183 | | - Ok(()) |
184 | | -} |
185 | | - |
186 | | -#[cfg(windows)] |
187 | | -pub fn link_package(name: &str) -> Result<()> { |
188 | | - if let Some(path) = NVMD_PATH.clone() { |
189 | | - let mut exe_source = path.clone(); |
190 | | - exe_source.push("bin"); |
191 | | - exe_source.push("nvmd.exe"); |
192 | | - let mut cmd_source = path.clone(); |
193 | | - cmd_source.push("bin"); |
194 | | - cmd_source.push("npm.cmd"); |
195 | | - |
196 | | - let mut exe_alias = path.clone(); |
197 | | - exe_alias.push("bin"); |
198 | | - exe_alias.push(format!("{}.exe", name)); |
199 | | - let mut cmd_alias = path.clone(); |
200 | | - cmd_alias.push("bin"); |
201 | | - cmd_alias.push(format!("{}.cmd", name)); |
202 | | - |
203 | | - let mut options = CopyOptions::new(); |
204 | | - options.skip_exist = true; |
205 | | - copy(&exe_source, &exe_alias, &options)?; |
206 | | - copy(&cmd_source, &cmd_alias, &options)?; |
207 | | - } |
208 | | - Ok(()) |
209 | | -} |
210 | | - |
211 | | -#[cfg(unix)] |
212 | | -pub fn unlink_package(name: &str) -> Result<()> { |
213 | | - if let Some(mut alias) = NVMD_PATH.clone() { |
214 | | - alias.push("bin"); |
215 | | - alias.push(name); |
216 | | - |
217 | | - remove(alias)?; |
218 | | - } |
219 | | - Ok(()) |
220 | | -} |
221 | | - |
222 | | -#[cfg(windows)] |
223 | | -pub fn unlink_package(name: &str) -> Result<()> { |
224 | | - if let Some(path) = NVMD_PATH.clone() { |
225 | | - let mut exe_alias = path.clone(); |
226 | | - exe_alias.push("bin"); |
227 | | - exe_alias.push(format!("{}.exe", name)); |
228 | | - let mut cmd_alias = path.clone(); |
229 | | - cmd_alias.push("bin"); |
230 | | - cmd_alias.push(format!("{}.cmd", name)); |
231 | | - |
232 | | - remove(exe_alias)?; |
233 | | - remove(cmd_alias)?; |
234 | | - } |
235 | | - Ok(()) |
236 | | -} |
0 commit comments