背景
写一些技术类的博客、笔记等,经过需要将项目的目录结构记录下来;
手动写比较麻烦,可以借助一些工具实现。
VSCode中有些类似的插件,比如:file-tree-generator,file-tree-to-text-generator等
这里使用 @structure-codes/cli 实现,配合vsCode的 Tree 插件
@structure-codes/cli 的 github地址: https://github.com/structure-codes/cli
安装
npm install @structure-codes/cli -g
配置文件
可以在执行命令的目录下新建配置文件 .treerc(没有配置文件也可以,会走默认配置)
.git
node_modules
这个配置文件是用来屏蔽文件和目录的,配置中的文件和目录不会出现在生成的文件树中
命令介绍
Usage: struct [options] [command] [directory]
Arguments:
directory directory to build structure from (default: ".")
Options:
-o, --output-file <outputFile> file to put tree structure in
-i, --ignore <ignore> ignore these patterns (default: [])
-c, --config-ignore include patterns that are ignored by config
-j, --json print tree in json format
-e, --editor open structure in new vscode window
-s, --silent do not print anything to the console
Examples:
Output tree ignoring dist to file new.tree
$ struct -i dist -o new.tree
示例
执行命令
struct -o test.tree -s ./src
执行完后,会在当前目录下生成一个 test.tree 文件;文件内容为 ./src 目录下的文件树
根据文件数生成文件和目录
struct 命令还可以根据 .tree文件生成对应的目录结构
Usage: struct build [options] <file>
Arguments:
file .tree file to build structure from
Options:
-d, --directory <directory> output directory to build structure in (default: ".")
-h, --help display help for command
Examples:
Build structure from src.tree in directory new-project
$ struct build ./src.tree -o new-project
Tree插件
VSCode中安装 Tree插件;
此扩展添加了对 .tree 文件的语法突出显示支持 - 这不是真正的语言,但这对于查看树命令输出非常有帮助。