Skip to content

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Apr 29, 2024
1 parent 6a00056 commit a7dbbf0
Show file tree
Hide file tree
Showing 6 changed files with 1,180 additions and 1,567 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["cdylib"]
napi = { version = "2.12.2", default-features = false, features = ["napi4","serde-json"] }
napi-derive = "2.12.2"
serde_json = "1.0"
serde ={ version = "1.0", features = ["derive"] }
swc_core = {version="0.91.1",features = ["ecma_plugin_transform","__parser"] }
swc_ecmascript = { version = "0.240.0", features = ["parser", "codegen", "visit"] }
wax = "0.6.0"
Expand Down
10 changes: 5 additions & 5 deletions __test__/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { inspectPackageUsage} from '../index.js'


test('xx', (t) => {
// const start = performance.now()
// const res = inspectPackageUsage("shineout","/Users/ityuany/GitRepository/metric-front");
// console.log(res);
// const end = performance.now()
// console.log(`inspectPackageUsage cost ${end - start} ms`);
const start = performance.now()
const res = inspectPackageUsage("shineout","/Users/10015448/GitRepository/fsp-front");
console.log(res);
const end = performance.now()
console.log(`inspectPackageUsage cost ${end - start} ms`);
console.log("xx");

})
21 changes: 15 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ use swc_ecmascript::{
parser::{Parser, TsConfig},
visit::VisitWith,
};
use usage::PackageExportedUsage;
use wax::Glob;

mod usage;
mod vistor;

#[macro_use]
Expand All @@ -34,7 +36,9 @@ pub fn inspect_package_usage(
) -> NapiResult<serde_json::Value> {
let glob = Glob::new(PATTERN).unwrap();

let map = Arc::new(Mutex::new(HashMap::<String, usize>::new()));
// let map = Arc::new(Mutex::new(HashMap::<String, usize>::new()));

let vec = Arc::new(Mutex::new(Vec::<PackageExportedUsage>::new()));

let entries = glob
.walk(workspace)
Expand Down Expand Up @@ -83,18 +87,23 @@ pub fn inspect_package_usage(
imports: vec![],
};
module_result.visit_with(&mut import_controller);
let mut map = map.lock().unwrap();
// let mut map = map.lock().unwrap();
let mut vec = vec.lock().unwrap();
for import in import_controller.imports {
*map.entry(import).or_insert(0) += 1;
vec.push(PackageExportedUsage {
file_path: path.display().to_string(),
exported_name: import,
})
// *map.entry(import).or_insert(0) += 1;
}
}
Err(error) => {
println!("Error: {:?}", error);
println!("Error: {:?} {:?}", error, path);
}
}
});

let cloned_map = map.lock().unwrap().clone();
let vec = vec.lock().unwrap().clone();

Ok(json!(cloned_map))
Ok(json!(vec))
}
7 changes: 7 additions & 0 deletions src/usage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use serde::Serialize;

#[derive(Clone, Serialize)]
pub struct PackageExportedUsage {
pub exported_name: String,
pub file_path: String,
}
10 changes: 6 additions & 4 deletions src/vistor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use swc_ecmascript::{
visit::Visit,
};

use crate::usage::PackageExportedUsage;

pub struct ImportVisitor {
pub imports: Vec<String>,
pub package_name: String,
Expand All @@ -16,11 +18,11 @@ impl Visit for ImportVisitor {
ImportSpecifier::Named(named) => {
self.imports.push(named.local.sym.to_string());
}
ImportSpecifier::Default(default) => {
self.imports.push(default.local.sym.to_string());
ImportSpecifier::Default(_) => {
self.imports.push("ES:DEFAULT".to_string());
}
ImportSpecifier::Namespace(namespace) => {
self.imports.push(namespace.local.sym.to_string());
ImportSpecifier::Namespace(_) => {
self.imports.push("ES:NAMESPACE".to_string());
}
}
}
Expand Down
Loading

0 comments on commit a7dbbf0

Please sign in to comment.