Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Apr 28, 2024
1 parent fba6e6a commit eab46c6
Show file tree
Hide file tree
Showing 49 changed files with 69 additions and 63 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"napi",
"vistor"
]
}
File renamed without changes.
4 changes: 2 additions & 2 deletions package-exported-usage/Cargo.toml → Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ crate-type = ["cdylib"]

[dependencies]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
napi = { version = "2.12.2", default-features = false, features = ["napi4","serde-json"] }
napi-derive = "2.12.2"
serde_json = "1.0"
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"
rayon = "1.10"
num_cpus = "1.13.0"

[build-dependencies]
napi-build = "2.0.1"
Expand Down
15 changes: 15 additions & 0 deletions __test__/index.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import test from 'ava'
import { performance } from 'perf_hooks';
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`);
console.log("xx");

})
File renamed without changes.
2 changes: 1 addition & 1 deletion package-exported-usage/index.d.ts → index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

/* auto-generated by NAPI-RS */

export function inspectPackageUsage(packageName: string, workspace: string): void
export function inspectPackageUsage(packageName: string, workspace: string): NapiResult
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 0 additions & 12 deletions package-exported-usage/__test__/index.spec.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion package-exported-usage/package.json → package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shined/package-exported-usage",
"version": "0.0.0",
"version": "0.0.1",
"main": "index.js",
"types": "index.d.ts",
"napi": {
Expand Down
File renamed without changes.
62 changes: 15 additions & 47 deletions package-exported-usage/src/lib.rs → src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,37 @@
#![deny(clippy::all)]
// use std::path::Path;
use rayon::prelude::*;

use crate::vistor::ImportVisitor;
use napi::Result as NapiResult;
use rayon::prelude::*;
use serde_json::json;
use std::{
collections::HashMap,
fs::read_to_string,
sync::{Arc, Mutex},
};

use swc_core::{
common::{input, FileName, SourceFile, SourceMap, Spanned},
ecma::parser::{lexer::Lexer, EsConfig, StringInput, Syntax},
common::{FileName, SourceMap},
ecma::parser::{lexer::Lexer, StringInput, Syntax},
};

use swc_ecmascript::{
ast::{ImportDecl, ImportSpecifier},
visit::Visit,
};

use swc_ecmascript::{
ast::EsVersion,
parser::{Parser, TsConfig},
visit::{VisitAllWith, VisitWith},
visit::VisitWith,
};
use wax::Glob;

mod vistor;

#[macro_use]
extern crate napi_derive;

struct ImportVisitor {
imports: Vec<String>,
package_name: String,
}

impl Visit for ImportVisitor {
fn visit_import_decl(&mut self, n: &ImportDecl) {
if n.src.value == self.package_name {
for specifier in &n.specifiers {
match specifier {
ImportSpecifier::Named(named) => {
self.imports.push(named.local.sym.to_string());
}
ImportSpecifier::Default(default) => {
self.imports.push(default.local.sym.to_string());
}
ImportSpecifier::Namespace(namespace) => {
self.imports.push(namespace.local.sym.to_string());
}
}

// self.imports.push(specifier.clone());
}
}
}
}

static PATTERN: &str = "**/*.{js,ts,jsx,tsx}";

#[napi]
pub fn inspect_package_usage(package_name: String, workspace: String) {
pub fn inspect_package_usage(
package_name: String,
workspace: String,
) -> NapiResult<serde_json::Value> {
let glob = Glob::new(PATTERN).unwrap();

let map = Arc::new(Mutex::new(HashMap::<String, usize>::new()));
Expand All @@ -68,10 +41,6 @@ pub fn inspect_package_usage(package_name: String, workspace: String) {
.not(["**/node_modules/**", "**/*.d.ts"])
.unwrap();

let num_cpus = num_cpus::get();

println!("num_cpus: {:?}", num_cpus);

entries.par_bridge().for_each(|entry| {
let e = entry.unwrap();
let path = e.path();
Expand Down Expand Up @@ -113,7 +82,6 @@ pub fn inspect_package_usage(package_name: String, workspace: String) {
package_name: package_name.clone(),
imports: vec![],
};

module_result.visit_with(&mut import_controller);
let mut map = map.lock().unwrap();
for import in import_controller.imports {
Expand All @@ -126,7 +94,7 @@ pub fn inspect_package_usage(package_name: String, workspace: String) {
}
});

println!("---> {:?}", map.lock().unwrap());
let cloned_map = map.lock().unwrap().clone();

println!("package-exported-usage");
Ok(json!(cloned_map))
}
29 changes: 29 additions & 0 deletions src/vistor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use swc_ecmascript::{
ast::{ImportDecl, ImportSpecifier},
visit::Visit,
};

pub struct ImportVisitor {
pub imports: Vec<String>,
pub package_name: String,
}

impl Visit for ImportVisitor {
fn visit_import_decl(&mut self, n: &ImportDecl) {
if n.src.value == self.package_name {
for specifier in &n.specifiers {
match specifier {
ImportSpecifier::Named(named) => {
self.imports.push(named.local.sym.to_string());
}
ImportSpecifier::Default(default) => {
self.imports.push(default.local.sym.to_string());
}
ImportSpecifier::Namespace(namespace) => {
self.imports.push(namespace.local.sym.to_string());
}
}
}
}
}
}
File renamed without changes.

0 comments on commit eab46c6

Please sign in to comment.