diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 571e15b..df53d43 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,7 +69,7 @@ jobs: restore-cache: false tools: cargo-deny - - if: steps.filter.outputs.src == 'true' + - if: steps.filter.outpts.src == 'true' run: cargo deny check unused-deps: diff --git a/Cargo.lock b/Cargo.lock index 9d5be96..ae9012b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -146,7 +146,7 @@ dependencies = [ ] [[package]] -name = "depcheck" +name = "depclean" version = "0.1.0" dependencies = [ "colored", diff --git a/Cargo.toml b/Cargo.toml index b568b3b..6d5e2b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "depcheck" +name = "depclean" version = "0.1.0" edition = "2021" diff --git a/depclean-darwin-arm64 b/depclean-darwin-arm64 new file mode 100755 index 0000000..73f22aa Binary files /dev/null and b/depclean-darwin-arm64 differ diff --git a/npm/depclean-darwin-arm64/depclean b/npm/depclean-darwin-arm64/depclean new file mode 100755 index 0000000..73f22aa Binary files /dev/null and b/npm/depclean-darwin-arm64/depclean differ diff --git a/npm/depclean-darwin-arm64/package.json b/npm/depclean-darwin-arm64/package.json new file mode 100644 index 0000000..5ce0d82 --- /dev/null +++ b/npm/depclean-darwin-arm64/package.json @@ -0,0 +1 @@ +{"name":"@depclean/darwin-arm64","version":"0.0.2","author":"mysteryven","license":"MIT","homepage":"https://github.com/mysteryven/depclean","bugs":"https://github.com/mysteryven/depclean/issues","repository":{"type":"git","url":"git+https://github.com/mysteryven/depclean.git","directory":"npm"},"os":["darwin"],"cpu":["arm64"]} \ No newline at end of file diff --git a/npm/depclean/package.json b/npm/depclean/package.json index bcab951..194c4b7 100644 --- a/npm/depclean/package.json +++ b/npm/depclean/package.json @@ -1,29 +1 @@ -{ - "name": "depclean", - "version": "0.7.1", - "description": "A rust version depcheck to help you find and remove unused dependencies in your package.json", - "keywords": [ - "depclean", - "depcheck", - "npm", - "node", - "rust" - ], - "author": "mysteryven", - "license": "MIT", - "homepage": "https://github.com/mysteryven/depclean", - "bugs": "https://github.com/mysteryven/depclean/issues", - "repository": { - "type": "git", - "url": "https://github.com/mysteryven/depclean", - "directory": "npm" - }, - "bin": "bin/depclean", - "engines": { - "node": ">=14.*" - }, - "files": [ - "bin/depclean", - "README.md" - ] -} +{"name":"depclean","version":"0.0.2","description":"A rust version depcheck to help you find and remove unused dependencies in your package.json","keywords":["depclean","depcheck","npm","node","rust"],"author":"mysteryven","license":"MIT","homepage":"https://github.com/mysteryven/depclean","bugs":"https://github.com/mysteryven/depclean/issues","repository":{"type":"git","url":"git+https://github.com/mysteryven/depclean.git","directory":"npm"},"bin":{"depclean":"bin/depclean"},"engines":{"node":">=14.*"},"files":["bin/depclean","README.md"],"optionalDependencies":{"@depclean/darwin-arm64":"0.0.2"}} \ No newline at end of file diff --git a/src/dependencies.rs b/src/dependencies.rs index cb8e108..fb3d040 100644 --- a/src/dependencies.rs +++ b/src/dependencies.rs @@ -11,10 +11,11 @@ pub struct PackageJSON { dependencies: FxHashMap, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Deserialize)] pub struct DependenceContainer { /// dependencies collect from package.json - dependencies: FxHashSet, + #[serde(default)] + dependencies: FxHashSet, } impl DependenceContainer { diff --git a/src/lib.rs b/src/lib.rs index 913d62e..db25708 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,4 @@ -use std::{ - env::current_dir, - path::Path, - sync::Arc, -}; +use std::{env::current_dir, path::Path, sync::Arc}; use colorful::Colorful; use dependencies::DependenceBuilder; @@ -38,9 +34,6 @@ impl DepChecker { /// # Panics /// if package.json not found in the root directory pub fn check(&mut self, root_path: &Path) -> Vec { - if !root_path.join("package.json").exists() { - eprintln!("package.json not found in the root directory"); - } let paths: FxHashSet> = Walk::new(root_path.to_path_buf()) .paths() .into_iter() @@ -79,7 +72,15 @@ impl DepChecker { } pub fn run_with_path(&mut self, path: &Path) { + if !path.join("package.json").exists() { + eprintln!("Didn't find package.json in current directory, did you run this command in the right place?"); + return; + } let unused_deps = self.check(path); + if unused_deps.is_empty() { + println!("No unused dependencies found, good!"); + return; + } let dep_text = if unused_deps.len() > 1 { "dependencies" } else { diff --git a/src/main.rs b/src/main.rs index e7a11a9..273d16b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +use depclean::DepChecker; + fn main() { - println!("Hello, world!"); + let mut checker = DepChecker::new(); + checker.run(); } diff --git a/src/runtime.rs b/src/runtime.rs index 620b94e..2e6ac18 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -11,7 +11,7 @@ use crate::{ Atom, }; -const EXTENSIONS: [&str; 4] = ["js", "jsx", "mjs", "cjs", "ts", "tsx"]; +const EXTENSIONS: &[&str] = &["js", "mjs", "cjs", "jsx", "ts", "mts", "cts", "tsx"]; pub struct DepCheckerContext<'a> { semantic: Rc>, @@ -64,15 +64,26 @@ impl Runtime { /// # Panics /// if the file extension is not one of "js", "mjs", "cjs", "jsx", "ts", "mts", "cts", "tsx" /// - /// check js kind files, includes .js, .jsx, .ts, .tsx - /// analyze their esm and cjs dependencies + /// Analyze their esm and cjs dependencies, return the used dependencies + /// for file: + /// + /// ```js + /// import A from './a.js'; + /// import B from 'b/foo.mjs'; + /// const C = require('c') + /// ``` + /// + /// We will get `["b/foo.mjs", "c"]` pub fn check_js_files(&self, path: &Path) -> Vec { let Ok(source_type) = SourceType::from_path(path) else { eprintln!("Unsupported file type: {:?}", path); return vec![]; }; - let source_text = fs::read_to_string(path).unwrap(); + let Ok(source_text) = fs::read_to_string(path) else { + eprintln!("Failed to read file: {:?}", path); + return vec![]; + }; let allocator = Allocator::default(); let ret = Parser::new(&allocator, &source_text, source_type) .allow_return_outside_function(true)