1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![feature(trait_alias)]
use neon::prelude::*;
use serde::Serialize;
use serde_json;
use std::fs::{canonicalize, metadata, read_dir, Metadata};
use std::io::Result;
use std::path::Path;
#[derive(Serialize, Debug)]
pub struct FileNode {
name: String,
path: String,
size: u64,
children: Vec<FileNode>,
}
fn walk(dir: &Path) -> Result<FileNode> {
let mut root: FileNode = path2node(&dir)?;
if dir.is_dir() {
for entry in read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
root.children.push(walk(&path)?);
} else {
let child = path2node(&entry.path())?;
root.children.push(child);
}
}
}
Ok(root)
}
fn path2node(path: &Path) -> Result<FileNode> {
let path = &canonicalize(path)?;
let data: Metadata = metadata(&path)?;
let node = FileNode {
name: path.file_name().unwrap().to_string_lossy().to_string(),
path: path.to_string_lossy().to_string(),
size: data.len(),
children: Vec::new(),
};
Ok(node)
}
pub fn filetree(path: &Path) -> Result<FileNode> {
let root = walk(path)?;
Ok(root)
}
fn hello(mut cx: FunctionContext) -> JsResult<JsString> {
Ok(cx.string("hello node"))
}
#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("hello", hello)?;
Ok(())
}
#[test]
fn test_output() {
let tree = filetree(Path::new("./node_modules")).unwrap();
println!("{}", serde_json::to_string(&tree).unwrap());
}