1. 程式人生 > 實用技巧 >使用 tree-node-cli 自動生成目錄樹結構

使用 tree-node-cli 自動生成目錄樹結構

一、安裝 Node 和 Git

下載 Node.jsGit,網上教程很多,這裡不再贅述。

二、安裝 tree-node-cli

在專案的根資料夾下滑鼠右鍵開啟 Git Bash Here

輸入下面命令進行安裝:

npm install -g tree-node-cli

三、自動生成目錄樹結構

輸入下面命令:

tree -L 4 -I "Scripts|Lib|pycache" > tree.md

解釋:

  • tree -L n 顯示專案的層級。n 表示層級數。比如你想要顯示專案的 4 層結構,可以用tree -L 4
  • tree -I "Scripts|Lib|pycache"
    表示要過濾的資料夾/目錄。
  • tree > tree.md 表示將結構輸出到 tree.md 這個檔案。

更多操作可以輸入 tree --help 進行檢視:

Options:
  -V, --version             output the version number
  -a, --all-files           All files, include hidden files, are printed.
  --dirs-first              List directories before files.
  -d, --dirs-only           List directories only.
  -I, --exclude [patterns]  Exclude files that match the pattern. | separates alternate patterns. Wrap your entire pattern in double quotes. E.g. `"node_modules|coverage".
  -L, --max-depth <n>       Max display depth of the directory tree.
  -r, --reverse             Sort the output in reverse alphabetic order.
  -F, --trailing-slash      Append a '/' for directories.
  -h, --help                output usage information

檢視 tree.md 檔案裡的內容:

這樣就能直接複製內容寫進 GitHub 專案裡的 README 中了,超級方便,而且很有條理。

四、參考

使用tree生成目錄樹結構