Skip to content

Commit

Permalink
fix: Stop tracking the npm prefix directory as this is not the defa…
Browse files Browse the repository at this point in the history
…ult behavior of the Node engine.

Signed-off-by: The1111mp <The1111mp@outlook.com>
  • Loading branch information
1111mp committed Nov 22, 2023
1 parent 25e7716 commit c302655
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 57 deletions.
55 changes: 4 additions & 51 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,23 @@
use std::{
env,
ffi::OsString,
io::{BufRead, BufReader, ErrorKind},
path::PathBuf,
process::{ExitStatus, Stdio},
};
use std::{env, ffi::OsString, io::ErrorKind, path::PathBuf, process::ExitStatus};

use fs_extra::file::read_to_string;
use lazy_static::lazy_static;
use serde_json::{from_str, Value};

use crate::command as CommandTool;

lazy_static! {
pub static ref NVMD_PATH: PathBuf = get_nvmd_path();
pub static ref VERSION: String = get_version();
pub static ref DEFAULT_INSTALLTION_PATH: PathBuf = get_default_installtion_path();
pub static ref INSTALLTION_PATH: PathBuf = get_installtion_path();
pub static ref NPM_PREFIX: PathBuf = get_npm_prefix();
pub static ref ENV_PATH: OsString = get_env_path(false);
pub static ref BINARY_ENV_PATH: OsString = get_env_path(true);
}

fn get_npm_prefix() -> PathBuf {
let mut command = CommandTool::create_command("npm");

let child = command
.env("PATH", ENV_PATH.clone())
.args(["config", "get", "prefix"])
.stdout(Stdio::piped())
.spawn()
.expect("nvmd-desktop: get npm perfix error");

let output = child.stdout.unwrap();
let lines = BufReader::new(output).lines();
let mut perfix = String::from("");
for line in lines {
let cur_line = line.unwrap();
if PathBuf::from(&cur_line).is_dir() {
perfix = cur_line;
}
}

PathBuf::from(perfix)
pub static ref ENV_PATH: OsString = get_env_path();
}

fn get_env_path(binary: bool) -> OsString {
fn get_env_path() -> OsString {
if VERSION.is_empty() {
return OsString::from("");
}

let bin_path = match Some(binary) {
Some(true) => get_binary_bin_path(),
Some(false) => get_bin_path(),
None => get_bin_path(),
};
let bin_path = get_bin_path();

if !PathBuf::from(&bin_path).exists() {
return OsString::from("");
Expand Down Expand Up @@ -85,16 +48,6 @@ fn get_bin_path() -> OsString {
nvmd_path.into_os_string()
}

fn get_binary_bin_path() -> OsString {
let mut nvmd_path = NPM_PREFIX.clone();

if cfg!(unix) {
nvmd_path.push("bin");
}

nvmd_path.into_os_string()
}

// $HOME/.nvmd/setting.json -> directory
fn get_installtion_path() -> PathBuf {
let mut setting_path = NVMD_PATH.clone();
Expand Down
9 changes: 4 additions & 5 deletions src/run/binary.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::path::PathBuf;

use super::{ExitStatus, OsStr, OsString};

use crate::{
command as CommandTool,
common::{BINARY_ENV_PATH, NPM_PREFIX},
common::{ENV_PATH, INSTALLTION_PATH, VERSION},
};

pub(super) fn command(exe: &OsStr, args: &[OsString]) -> Result<ExitStatus, String> {
let mut lib_path = PathBuf::from(NPM_PREFIX.clone());
let mut lib_path = INSTALLTION_PATH.clone();
lib_path.push(VERSION.clone());
if cfg!(unix) {
// unix
lib_path.push("bin");
Expand All @@ -20,7 +19,7 @@ pub(super) fn command(exe: &OsStr, args: &[OsString]) -> Result<ExitStatus, Stri
}

let child = CommandTool::create_command(exe)
.env("PATH", BINARY_ENV_PATH.clone())
.env("PATH", ENV_PATH.clone())
.args(args)
.status();

Expand Down
9 changes: 8 additions & 1 deletion src/run/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,14 @@ where
A: AsRef<OsStr>,
{
match arg.as_ref().to_str() {
Some(a) => a.starts_with('-') || a == "install" || a == "i" || a == "uninstall",
Some(a) => {
a.starts_with('-')
|| a == "install"
|| a == "i"
|| a == "uninstall"
|| a.starts_with("yarn")
|| a.starts_with("pnpm")
}
None => false,
}
}
Expand Down

0 comments on commit c302655

Please sign in to comment.