directory-tree: 遍历目录的工具

一人工具包
更新于: 2023-10-06 20:49:23

What

一个工具: Convert a directory tree to a JS object.

安装

yarn add directory-tree

cheatsheet

用法代码
目录遍历
const dirTree = require("directory-tree");
const tree = dirTree("test_data/some_dir");

console.log("tree: ", JSON.stringify(tree, null, 2));
结合一些属性
const dirTree = require("directory-tree");
const filteredTree = dirTree("/some/path", {
  attributes: ["mode", "mtime", "size", "type"],
});
特定的后缀-
const dirTree = require("directory-tree");
const filteredTree = dirTree("/some/path", { extensions: /\.txt/ });
自定义-
const PATH = require('path');
const dirTree = require('directory-tree');

const tree = dirTree('./test/test_data', {extensions:/\.txt$/}, (item, PATH, stats) => {
  console.log(item);
});

参考