Skip to content

Commit 5a9ec69

Browse files
committed
begin dependency feature
Signed-off-by: Luke-zhang-04 <[email protected]>
1 parent 9011596 commit 5a9ec69

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/onefetch/deps.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use {
2+
crate::onefetch::error::*,
3+
std::{ffi::OsStr, fs},
4+
std::collections::HashMap,
5+
};
6+
7+
mod Parsers {
8+
fn npm(contents: &str) -> i32 {
9+
println!(contents)
10+
return 0
11+
}
12+
}
13+
14+
15+
type DependencyParser = fn(&str) -> i32;
16+
let package_managers: HashMap<&str, (&str, DependencyParser)> = [
17+
("npm", ("package.json", Parsers.npm)),
18+
].iter().cloned().collect();
19+
20+
pub struct Detector {}
21+
22+
impl Detector {
23+
pub fn get_dep_count(&self, dir: &str) -> Result<String> {
24+
fn is_package_file<S: AsRef<str>>(file_name: S) -> bool {
25+
package_managers
26+
.iter()
27+
.any(|&info| file_name.as_ref().starts_with(info[0]))
28+
}
29+
30+
let mut package_files = fs::read_dir(dir)
31+
.chain_err(|| "Could not read directory")?
32+
.filter_map(std::result::Result::ok)
33+
.map(|entry| entry.path())
34+
.filter(|entry| {
35+
entry.is_file()
36+
&& entry
37+
.file_name()
38+
.map(OsStr::to_string_lossy)
39+
.map(is_package_file)
40+
.unwrap_or_default()
41+
})
42+
.filter_map(|entry| {
43+
let contents = fs::read_to_string(entry).unwrap_or_default();
44+
self.analyze(&contents)
45+
})
46+
.collect::<Vec<_>>();
47+
}
48+
}

0 commit comments

Comments
 (0)