2 tree-cli 的使用方法

31 阅读1分钟

上一小节,我们安装了 tree-cli,是为了方便生成项目树,查看当前目录结构的。

如上小节代码 package.json中的配置

  "scripts": {
    "tree": "treee -a -l 4 --ignore 'node_modules, dist, .git' --directoryFirst -f"
  },

快速使用

# 查看当前项目目录树(默认 4 层深度,忽略 node_modules 等)
pnpm tree

常用命令

# 基础用法 - 列出所有文件和目录
pnpm tree

# 只显示目录结构(不显示文件)
npx treee -d --ignore 'node_modules, dist, .git'

# 控制显示深度(-l 指定层数)
npx treee -l 2 --ignore 'node_modules, dist, .git'
npx treee -l 6 --ignore 'node_modules, dist, .git'

# 输出到文件(-o 指定输出文件)
npx treee -l 4 --ignore 'node_modules, dist, .git' -o docs/tree-output.txt

# 显示隐藏文件(-a)
npx treee -a --ignore 'node_modules, dist, .git'

# 目录优先排列 + 标记文件类型(-f 在目录后加 /)
npx treee --directoryFirst -f --ignore 'node_modules, dist, .git'

# 忽略更多目录
npx treee --ignore 'node_modules, dist, .git, coverage, .pnpm-store'

# 不显示统计报告
npx treee --noreport --ignore 'node_modules, dist, .git'

参数说明

参数说明
-l <n>最大显示深度
-d只显示目录
-f目录后加 /,socket 文件加 =,FIFO 加 |
-a显示隐藏文件(以 . 开头的文件)
-o <file>输出到指定文件
--ignore '<dirs>'忽略指定目录/文件,逗号分隔
--base <path>指定根目录
--directoryFirst目录排在文件前面
--noreport不打印末尾的统计信息
--fullpath显示完整路径