Skip to content

Commit

Permalink
feat: improve node process detector (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jujulego authored Dec 12, 2024
1 parent 46e7700 commit 8dcfcc7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ring-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ owo-colors = ["ring-color/owo-colors", "ring-tag/owo-colors", "dep:owo-colors"]

[dependencies]
anyhow = "1.0.93"
itertools = "0.13.0"
regex = "1.11.1"
rgb = "0.8.50"
semver = { version = "1.0.23", features = ["serde"] }
Expand Down
30 changes: 21 additions & 9 deletions crates/ring-web/src/node_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use std::sync::OnceLock;
use sysinfo::Pid;

// Parameters
static SHOULD_HIDE_REGEX: OnceLock<[Regex; 2]> = OnceLock::new();
const HIDE_PACKAGES: [&str; 2] = ["npm", "yarn"];
static HIDE_SCRIPT_REGEX: OnceLock<[Regex; 3]> = OnceLock::new();

fn should_hide_regex() -> &'static [Regex; 2] {
SHOULD_HIDE_REGEX.get_or_init(|| [
Regex::new(r"yarn\.[cm]?js$").unwrap(),
fn hide_script_regex() -> &'static [Regex; 3] {
HIDE_SCRIPT_REGEX.get_or_init(|| [
Regex::new(r"yarn-([0-9]+\.){3}[cm]?js$").unwrap(),
Regex::new(r"WebStorm[/\\]plugins[/\\]javascript-plugin[/\\]jsLanguageServicesImpl[/\\]").unwrap(),
Regex::new(r"typescript[/\\]lib[/\\]typingsInstaller\.[cm]?js$").unwrap(),
])
}

Expand Down Expand Up @@ -62,11 +64,21 @@ impl ProcessUnit for NodeProcess {
}

fn should_hide(&self) -> bool {
self.running_script.clone()
.is_some_and(|script| {
let path = script.path().to_str().unwrap();
should_hide_regex().iter().any(|re| re.is_match(path))
})
if let Some(script) = &self.running_script {
// Hide some packages
let package = script.package()
.and_then(|pkg| pkg.name());

if package.is_some_and(|name| HIDE_PACKAGES.contains(&name)) {
return true;
}

// Hide some scripts
let path = script.path().to_str().unwrap();
hide_script_regex().iter().any(|re| re.is_match(path))
} else {
false
}
}
}

Expand Down
32 changes: 16 additions & 16 deletions crates/ring-web/src/web_process_detector.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::collections::VecDeque;
use crate::{NodeProcess, WebUnitDetector};
use ring_process_unit::{ProcessUnit, ProcessUnitDetector};
use std::path::Path;
use std::rc::Rc;
use itertools::Itertools;
use sysinfo::{Pid, Process};
use tracing::info;
use tracing::{info, trace};

pub struct WebProcessDetector {
web_unit_detector: Rc<WebUnitDetector>
Expand All @@ -22,35 +24,33 @@ impl ProcessUnitDetector for WebProcessDetector {
.and_then(|n| n.to_str());

if exe == Some("node") {
let mut args = &process.cmd()[1..];
let mut args = process.cmd()[1..].iter()
.map(|arg| arg.to_str().unwrap().to_string())
.collect::<VecDeque<_>>();

trace!("parsing node args: {}", args.iter().join(" "));

while !args.is_empty() {
let arg = args[0].to_str().unwrap();
let arg = args.pop_front().unwrap();

if arg.starts_with('-') {
if ["-r", "--require", "--import"].contains(&arg) {
args = &args[2..];
} else {
args = &args[1..];
if ["-r", "--require", "--import"].contains(&arg.as_str()) {
args.pop_front();
}
} else {
args.push_front(arg);
break;
}
}

if let Some(script_path) = args.first().map(Path::new) {
if let Some(script_path) = args.pop_front().as_deref().map(Path::new) {
info!("recognized {pid} as a node process");

let mut args: Vec<_> = process.cmd()[2..].iter()
.map(|arg| arg.to_str().unwrap())
.map(|arg| arg.to_string())
.collect();

let process = if let Some(running_script) = self.web_unit_detector.detect_script(script_path)? {
NodeProcess::new(*pid, args, Some(running_script))
NodeProcess::new(*pid, args.into(), Some(running_script))
} else {
args.insert(0, script_path.file_name().unwrap().to_str().unwrap().to_string());
NodeProcess::new(*pid, args, None)
args.push_front(script_path.file_name().unwrap().to_str().unwrap().to_string());
NodeProcess::new(*pid, args.into(), None)
};

return Ok(Some(Rc::new(process)));
Expand Down

0 comments on commit 8dcfcc7

Please sign in to comment.