Skip to content

Commit 4b53891

Browse files
committed
refactor: strict types & improve performance
Signed-off-by: The1111mp <[email protected]>
1 parent 1664e1b commit 4b53891

File tree

15 files changed

+609
-756
lines changed

15 files changed

+609
-756
lines changed

Cargo.lock

Lines changed: 79 additions & 207 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ authors = ["[email protected]"]
99
[dependencies]
1010
anyhow = "1.0"
1111
cfg-if = "1.0"
12-
chrono = "0.4"
1312
clap = { version = "4.5", features = ["derive"] }
1413
dirs = "5.0"
1514
fs_extra = "1.3"
1615
lazy_static = "1"
1716
regex = "1.10"
1817
serde = { version = "1.0", features = ["derive"] }
1918
serde_json = "1.0"
19+
time = { version = "0.3", features = ["local-offset"] }
2020
version-compare = "0.2"

src/common.rs

Lines changed: 7 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
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;
53
use lazy_static::lazy_static;
6-
use serde::{de::DeserializeOwned, Deserialize, Serialize};
74
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};
96

107
lazy_static! {
118
pub static ref NVMD_PATH: Option<PathBuf> = get_nvmd_path().ok();
@@ -50,13 +47,13 @@ fn get_installation_path() -> Result<Option<PathBuf>> {
5047

5148
let json_obj: Value = from_str(&setting_content)?;
5249
if let Some(directory) = json_obj["directory"].as_str() {
53-
Ok(Some(PathBuf::from(directory)))
50+
return Ok(Some(PathBuf::from(directory)));
5451
} else {
55-
Ok(DEFAULT_INSTALLATION_PATH.clone())
52+
return Ok(DEFAULT_INSTALLATION_PATH.clone());
5653
}
57-
} else {
58-
Ok(None)
5954
}
55+
56+
Ok(None)
6057
}
6158

6259
fn get_default_installation_path() -> Option<PathBuf> {
@@ -105,132 +102,3 @@ fn get_nvmd_path() -> Result<PathBuf> {
105102
home.push(".nvmd");
106103
Ok(home)
107104
}
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-
}
File renamed without changes.

src/run/corepack.rs renamed to src/core/corepack.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use super::Result;
22
use super::{ExitStatus, OsStr, OsString};
33

4-
use anyhow::bail;
5-
use lazy_static::lazy_static;
6-
4+
use crate::utils::help::{link_package, unlink_package};
5+
use crate::utils::package::package_can_be_removed;
76
use crate::{
87
command as CommandTool,
9-
common::{link_package, package_can_be_removed, unlink_package, ENV_PATH, VERSION},
8+
common::{ENV_PATH, VERSION},
109
};
1110

11+
use anyhow::bail;
12+
use lazy_static::lazy_static;
13+
1214
lazy_static! {
1315
static ref ENABLE: OsString = OsString::from("enable");
1416
static ref DISABLE: OsString = OsString::from("disable");
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)