Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Oct 21, 2024
1 parent dab9521 commit 3ea119a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
20 changes: 16 additions & 4 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,23 @@ pub fn test(file: String, args: module_graph::model::JsArgs) {

#[napi]
pub fn check_cycle(
options: Option<module_graph::Options>,
args: module_graph::model::JsArgs,
) -> Result<module_graph::model::GroupGraphics> {
module_graph::check_cycle(options).map_err(|err| {
napi::Error::new(napi::Status::GenericFailure, err.to_string())
})
let cwd = args.get_cwd();

let ignore = args.get_ignore();

let pattern = args.get_pattern();

let mut graph = module_graph::graph::Graph::new(module_graph::model::Args {
alias: args.get_alias(),
modules: args.get_modules(),
cwd: cwd.as_str(),
ignore: ignore.iter().map(|s| s.as_str()).collect(),
pattern: pattern.as_str(),
});
let res = graph.check_cycle();
res.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
}

#[napi]
Expand Down
46 changes: 34 additions & 12 deletions crates/module_graph/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{collections::HashMap, env::current_dir};

use camino::Utf8PathBuf;
use napi_derive::napi;
use serde::Serialize;

#[derive(Debug, Clone)]
#[napi(object)]
pub struct JsArgs {
pub cwd: Option<String>,
Expand All @@ -12,18 +14,37 @@ pub struct JsArgs {
pub modules: Option<Vec<String>>,
}

impl Default for JsArgs {
fn default() -> Self {
Self {
cwd: Some(current_dir().unwrap().display().to_string()),
pattern: Some("**/*.{js,ts,jsx,tsx}".to_string()),
ignore: Some(vec![
"**/node_modules/**".to_string(),
"**/*.d.ts".to_string(),
]),
alias: Some(HashMap::new()),
modules: Some(vec!["node_modules".to_string()]),
}
impl JsArgs {
pub fn get_cwd(&self) -> String {
let default_cwd = current_dir().unwrap().display().to_string();
Utf8PathBuf::from(self.cwd.clone().unwrap_or(default_cwd))
.join("")
.into_string()
}

pub fn get_pattern(&self) -> String {
self
.pattern
.clone()
.unwrap_or("**/*.{js,ts,jsx,tsx}".to_string())
}

pub fn get_ignore(&self) -> Vec<String> {
self.ignore.clone().unwrap_or(vec![
"**/node_modules/**".to_string(),
"**/*.d.ts".to_string(),
])
}

pub fn get_alias(&self) -> HashMap<String, Vec<String>> {
self.alias.clone().unwrap_or(HashMap::new())
}

pub fn get_modules(&self) -> Vec<String> {
self
.modules
.clone()
.unwrap_or(vec!["node_modules".to_string()])
}
}

Expand All @@ -44,6 +65,7 @@ pub struct Edge {
pub ast_node: beans::AstNode,
}

#[derive(Debug, Serialize)]
#[napi(object)]
pub struct GroupGraphics {
pub dictionaries: HashMap<String, String>,
Expand Down

0 comments on commit 3ea119a

Please sign in to comment.