Node.js 打印vite react+go项目目录树


发布者 ourjs  发布时间 1767142644082
关键字 Node.JS 
想要用 Node.js 实现打印当前目录树的功能,支持排除指定文件 / 文件夹、控制目录深度,且输出样式和系统 tree 命令一致,适配 Windows/macOS/Linux(包括 WSL)。
 
const fs = require('fs').promises;
const path = require('path');

/**
 * Node.js 打印目录树
 * @param {string} rootDir - 根目录路径(默认当前目录)
 * @param {number} maxDepth - 最大遍历深度(默认无限)
 * @param {string[]} exclude - 要排除的文件/文件夹名列表
 * @param {string} indent - 内部递归用:缩进字符
 * @param {string} prefix - 内部递归用:前缀字符
 * @param {number} currentDepth - 内部递归用:当前深度
 */
async function printDirTree(
  rootDir = process.cwd(),
  maxDepth = Infinity,
  exclude = ['.git', 'node_modules', '__pycache__', '.DS_Store', 'corepy', 'svr', 'tree.js'],
  indent = '',
  prefix = '',
  currentDepth = 0,
  isLast = true,
) {
  if (currentDepth > maxDepth) return;

  try {
    const absolutePath = path.resolve(rootDir);
    const dirName = path.basename(absolutePath);

    if (currentDepth === 0) {
      console.log(`${dirName}`);
    } else {
      console.log(`${indent}${prefix}${dirName}`);
    }

    if (currentDepth >= maxDepth) return;

    const entries = await fs.readdir(absolutePath, { withFileTypes: true });
    const filteredEntries = entries.filter(entry => {
      return !exclude.includes(entry.name);
    });

    for (let i = 0; i < filteredEntries.length; i++) {
      const entry = filteredEntries[i];
      const isCurrLast = i === filteredEntries.length - 1;

      const newIndent = indent + (isLast ? '   ' : '│   ');
      const newPrefix = isCurrLast ? '└── ' : '├── ';
      const entryPath = path.join(absolutePath, entry.name);
      const isOne = filteredEntries.length < 2

      // 如果是目录,递归遍历;如果是文件,直接打印
      if (entry.isDirectory()) {
        await printDirTree(
          entryPath,
          maxDepth,
          exclude,
          newIndent,
          newPrefix,
          currentDepth + 1,
          i === filteredEntries.length - 1,
        );
      } else {
        console.log(`${newIndent}${newPrefix}${entry.name}`);
      }
    }
  } catch (err) {
    console.error(`访问目录失败:${rootDir}`, err.message);
  }
}

// ========== 配置项(可自定义) ==========
const CONFIG = {
  rootDir: process.cwd(),
  maxDepth: 4,
};

printDirTree(CONFIG.rootDir, CONFIG.maxDepth, CONFIG.exclude)

效果如下:

node tree.js
web-ide
   ├── .gitignore
   ├── backend
   │   ├── cmd
   │   │   ├── gormgen
   │   │   │   ├── main.go
   │   │   │   └── README.md
   │   │   ├── handlergen
   │   │   │   ├── handler_template.go.tpl
   │   │   │   ├── main.go
   │   │   │   ├── README.md
   │   │   │   └── routers_template.go.tpl
   │   │   └── mfmt
   │   │      ├── main.go
   │   │      └── README.md
   │   ├── configs
   │   │   ├── configs.go
   │   │   ├── constants.go
   │   │   ├── dev_configs.toml
   │   │   ├── fat_configs.toml
   │   │   ├── pro_configs.toml
   │   │   └── uat_configs.toml
   │   ├── Dockerfile
   │   ├── docs
   │   │   ├── docs.go
   │   │   ├── swagger.json
   │   │   └── swagger.yaml
   │   ├── go.mod
   │   ├── go.sum
   │   ├── img
   │   ├── internal
   │   │   ├── alert
   │   │   │   └── alert.go
   │   │   ├── api
   │   │   │   ├── adapter_xml
   │   │   │   ├── admin
   │   │   │   ├── code
   │   │   │   ├── eae_project
   │   │   │   ├── project_xml
   │   │   │   └── users_registration
   │   │   ├── code
   │   │   │   ├── code.go
   │   │   │   ├── en-us.go
   │   │   │   ├── README.md
   │   │   │   └── zh-cn.go
   │   │   ├── metrics
   │   │   │   ├── metrics.go
   │   │   │   └── prometheus.go
   │   │   ├── pkg
   │   │   │   ├── color
   │   │   │   ├── core
   │   │   │   ├── cors
   │   │   │   ├── cryptoaes
   │   │   │   ├── cryptorsa
   │   │   │   ├── debug
   │   │   │   ├── env
   │   │   │   ├── errors
   │   │   │   ├── httpclient
   │   │   │   ├── idgen
   │   │   │   ├── jwtoken
   │   │   │   ├── logger
   │   │   │   ├── password
   │   │   │   ├── shutdown
   │   │   │   ├── timeutil
   │   │   │   ├── trace
   │   │   │   └── validation
   │   │   ├── proposal
   │   │   │   ├── alert.go
   │   │   │   ├── metrics.go
   │   │   │   └── session.go
   │   │   ├── repository
   │   │   │   ├── mongo
   │   │   │   ├── mysql
   │   │   │   └── redis
   │   │   ├── router
   │   │   │   ├── interceptor
   │   │   │   └── router.go
   │   │   └── service
   │   │      ├── adapter.xml
   │   │      ├── adapter_xml
   │   │      ├── example
   │   │      ├── paser_cpp_class.go
   │   │      └── tools
   │   ├── logs
   │   │   └── bt-web-ide-access.log
   │   ├── main.go
   │   ├── README.md
   │   ├── scripts
   │   │   ├── hotreload.bat
   │   │   ├── hotreload.sh
   │   │   ├── swagger.bat
   │   │   └── swagger.sh
   │   ├── ui
   │   │   ├── css
   │   │   │   ├── index-CqBL6GLZ.css
   │   │   │   ├── index-CrDt0J4n.css
   │   │   │   ├── index-Dac6-GEX.css
   │   │   │   ├── index-Dcd_m2ht.css
   │   │   │   ├── index-DF4E9a5B.css
   │   │   │   ├── index-DMJwW_jD.css
   │   │   │   └── index-IGqcm5NO.css
   │   │   ├── eot
   │   │   │   └── themify-BifPJsew.eot
   │   │   ├── fbxml
   │   │   │   └── Runtime.All.json
   │   │   ├── img
   │   │   │   └── SE_logo-LIO-white_header.svg
   │   │   ├── index.html
   │   │   ├── js
   │   │   │   ├── index-1EVSy1Ms.js
   │   │   │   ├── index-B6LUCqyT.js
   │   │   │   ├── index-B87EuGk4.js
   │   │   │   ├── index-BDZPoFWR.js
   │   │   │   ├── index-BLGwa8Ey.js
   │   │   │   ├── index-Bu-cTs_h.js
   │   │   │   ├── index-BZ2Q405h.js
   │   │   │   ├── index-CPCxahhe.js
   │   │   │   ├── index-DjPabg08.js
   │   │   │   ├── index-DuMTyoBl.js
   │   │   │   ├── index-DzinWrW5.js
   │   │   │   ├── index-Vo3pejat.js
   │   │   │   ├── index-xcCKSrnQ.js
   │   │   │   └── login-B35mOfnp.js
   │   │   ├── login.html
   │   │   ├── svg
   │   │   │   └── themify-Bfliuhp5.svg
   │   │   ├── ttf
   │   │   │   └── themify-DuU3rJAt.ttf
   │   │   └── woff
   │   │      └── themify-BgsFA67P.woff
   │   ├── vendor
   │   │   ├── github.com
   │   │   │   ├── alexmullins
   │   │   │   ├── beorn7
   │   │   │   ├── bwmarrin
   │   │   │   ├── bytedance
   │   │   │   ├── cespare
   │   │   │   ├── chenzhuoyu
   │   │   │   ├── dgryski
   │   │   │   ├── fsnotify
   │   │   │   ├── gabriel-vasile
   │   │   │   ├── gin-contrib
   │   │   │   ├── gin-gonic
   │   │   │   ├── go-openapi
   │   │   │   ├── go-playground
   │   │   │   ├── go-redis
   │   │   │   ├── go-resty
   │   │   │   ├── go-sql-driver
   │   │   │   ├── goccy
   │   │   │   ├── golang
   │   │   │   ├── golang-jwt
   │   │   │   ├── hashicorp
   │   │   │   ├── jinzhu
   │   │   │   ├── josharian
   │   │   │   ├── json-iterator
   │   │   │   ├── klauspost
   │   │   │   ├── KyleBanks
   │   │   │   ├── leodido
   │   │   │   ├── magiconair
   │   │   │   ├── mailru
   │   │   │   ├── mattn
   │   │   │   ├── matttproud
   │   │   │   ├── mitchellh
   │   │   │   ├── modern-go
   │   │   │   ├── montanaflynn
   │   │   │   ├── pelletier
   │   │   │   ├── pkg
   │   │   │   ├── prometheus
   │   │   │   ├── PuerkitoBio
   │   │   │   ├── rs
   │   │   │   ├── sagikazarmark
   │   │   │   ├── sourcegraph
   │   │   │   ├── spf13
   │   │   │   ├── subosito
   │   │   │   ├── swaggo
   │   │   │   ├── twitchyliquid64
   │   │   │   ├── ugorji
   │   │   │   ├── xdg-go
   │   │   │   └── youmark
   │   │   ├── go.mongodb.org
   │   │   │   └── mongo-driver
   │   │   ├── go.uber.org
   │   │   │   ├── multierr
   │   │   │   └── zap
   │   │   ├── golang.org
   │   │   │   └── x
   │   │   ├── google.golang.org
   │   │   │   └── protobuf
   │   │   ├── gopkg.in
   │   │   │   ├── ini.v1
   │   │   │   ├── natefinch
   │   │   │   ├── yaml.v2
   │   │   │   └── yaml.v3
   │   │   ├── gorm.io
   │   │   │   ├── datatypes
   │   │   │   ├── driver
   │   │   │   ├── gen
   │   │   │   ├── gorm
   │   │   │   ├── hints
   │   │   │   └── plugin
   │   │   └── modules.txt
   │   └── wiki
   │      ├── api.md
   │      └── mysql.md
   ├── codeserver
   │   ├── docker-compose-webide.service
   │   ├── Dockerfile
   │   ├── README.md
   │   └── volumes
   │      ├── config
   │      │   └── code-server
   │      ├── local
   │      │   ├── README.md
   │      │   └── share
   │      ├── local.v0.1.zip
   │      ├── project
   │      │   ├── project_3
   │      │   ├── project_4
   │      │   ├── project_53
   │      │   ├── project_54
   │      │   ├── project_6
   │      │   ├── project_7
   │      │   └── templates
   │      └── templates.zip
   ├── devops
   ├── docker-compose.yml
   ├── frontend
   │   ├── .vite
   │   │   └── deps
   │   │      ├── package.json
   │   │      └── _metadata.json
   │   ├── dist
   │   │   └── ui
   │   │      ├── css
   │   │      ├── eot
   │   │      ├── index.html
   │   │      ├── js
   │   │      ├── login.html
   │   │      ├── svg
   │   │      ├── ttf
   │   │      └── woff
   │   ├── eslint.config.js
   │   ├── package.json
   │   ├── public
   │   │   └── ui
   │   │      ├── fbxml
   │   │      └── img
   │   ├── README.md
   │   ├── src
   │   │   ├── assets
   │   │   │   ├── Colors.css
   │   │   │   └── themify-icons
   │   │   ├── Common
   │   │   │   ├── utils.ts
   │   │   │   └── WaitFor.ts
   │   │   ├── components
   │   │   │   ├── Message.ts
   │   │   │   ├── navbar.css
   │   │   │   ├── Navbar.tsx
   │   │   │   ├── sidebar.css
   │   │   │   └── Sidebar.tsx
   │   │   ├── Dashboard
   │   │   │   ├── Dashboard.tsx
   │   │   │   ├── DashboardContent.css
   │   │   │   └── DashboardContent.tsx
   │   │   ├── EventBus
   │   │   │   ├── EventPubSub.ts
   │   │   │   └── EventType.ts
   │   │   ├── FBDesigner
   │   │   │   ├── FBData.ts
   │   │   │   ├── FBDesigner.css
   │   │   │   ├── FBDesigner.tsx
   │   │   │   ├── FBGRaph.css
   │   │   │   ├── FBGraph.Interface.ts
   │   │   │   ├── FBGraph.Network.css
   │   │   │   ├── FBGraph.Network.ts
   │   │   │   ├── FBGraph.tsx
   │   │   │   ├── FBInterfaceEditor.tsx
   │   │   │   ├── FBNode.tsx
   │   │   │   ├── FBPropertyPanel.css
   │   │   │   ├── FBPropertyPanel.tsx
   │   │   │   ├── FBToolbar.css
   │   │   │   ├── FBToolbar.tsx
   │   │   │   ├── FBXml.ts
   │   │   │   └── FBXmlTypes.ts
   │   │   ├── layout
   │   │   │   ├── Layout.tsx
   │   │   │   └── LayoutWithoutNavbar.tsx
   │   │   ├── pages
   │   │   │   ├── index.css
   │   │   │   ├── Index.tsx
   │   │   │   ├── login.css
   │   │   │   └── login.tsx
   │   │   ├── Project
   │   │   │   └── Project.interface.ts
   │   │   ├── Services
   │   │   │   ├── API.ts
   │   │   │   ├── Request.interface.ts
   │   │   │   └── Request.ts
   │   │   ├── Stores
   │   │   │   ├── CacheStore.ts
   │   │   │   ├── FBInterfaceEditorStore.ts
   │   │   │   ├── FBLibraryStore.ts
   │   │   │   ├── ProjectStore.ts
   │   │   │   ├── ProjectXmlStore.ts
   │   │   │   └── UserStore.ts
   │   │   ├── styled
   │   │   │   ├── Content.ts
   │   │   │   └── ContentBody.ts
   │   │   ├── types
   │   │   │   └── jquery.plugins.d.ts
   │   │   ├── views
   │   │   │   ├── Chatbox.tsx
   │   │   │   ├── developer.css
   │   │   │   ├── Developer.tsx
   │   │   │   ├── Home.tsx
   │   │   │   ├── login.css
   │   │   │   ├── Login.tsx
   │   │   │   └── Welcome.tsx
   │   │   └── vite-env.d.ts
   │   ├── tools
   │   │   ├── makelibrary.js
   │   │   └── makeLibraryFromFolder.js
   │   ├── tsconfig.app.json
   │   ├── tsconfig.json
   │   ├── tsconfig.node.json
   │   ├── ui
   │   │   ├── api
   │   │   │   └── login
   │   │   ├── img
   │   │   │   ├── logo.png
   │   │   │   └── SE_logo-LIO-white_header.svg
   │   │   ├── index.html
   │   │   └── login.html
   │   ├── vite.config.ts
   │   └── yarn.lock
   ├── ollamaui
   │   ├── .env
   │   ├── .eslintrc.json
   │   ├── .gitignore
   │   ├── .next
   │   │   ├── app-build-manifest.json
   │   │   ├── build-manifest.json
   │   │   ├── cache
   │   │   │   ├── images
   │   │   │   ├── swc
   │   │   │   └── webpack
   │   │   ├── package.json
   │   │   ├── react-loadable-manifest.json
   │   │   ├── server
   │   │   │   ├── app
   │   │   │   ├── app-paths-manifest.json
   │   │   │   ├── edge-runtime-webpack.js
   │   │   │   ├── interception-route-rewrite-manifest.js
   │   │   │   ├── middleware-build-manifest.js
   │   │   │   ├── middleware-manifest.json
   │   │   │   ├── middleware-react-loadable-manifest.js
   │   │   │   ├── next-font-manifest.js
   │   │   │   ├── next-font-manifest.json
   │   │   │   ├── pages-manifest.json
   │   │   │   ├── server-reference-manifest.js
   │   │   │   ├── server-reference-manifest.json
   │   │   │   ├── static
   │   │   │   ├── vendor-chunks
   │   │   │   └── webpack-runtime.js
   │   │   ├── static
   │   │   │   ├── chunks
   │   │   │   ├── css
   │   │   │   ├── development
   │   │   │   ├── media
   │   │   │   └── webpack
   │   │   ├── trace
   │   │   └── types
   │   │      ├── app
   │   │      └── package.json
   │   ├── components.json
   │   ├── doc
   │   │   └── EAE61499_chat.md
   │   ├── Dockerfile
   │   ├── LICENSE
   │   ├── next-env.d.ts
   │   ├── next.config.mjs
   │   ├── package-lock.json
   │   ├── package.json
   │   ├── postcss.config.js
   │   ├── public
   │   │   ├── next.svg
   │   │   ├── ollama.png
   │   │   ├── user.jpg
   │   │   └── vercel.svg
   │   ├── README.md
   │   ├── src
   │   │   ├── app
   │   │   │   ├── (chat)
   │   │   │   ├── api
   │   │   │   ├── favicon.ico
   │   │   │   ├── globals.css
   │   │   │   └── hooks
   │   │   ├── components
   │   │   │   ├── button-with-tooltip.tsx
   │   │   │   ├── chat
   │   │   │   ├── code-display-block.tsx
   │   │   │   ├── edit-username-form.tsx
   │   │   │   ├── emoji-picker.tsx
   │   │   │   ├── image-embedder.tsx
   │   │   │   ├── mode-toggle.tsx
   │   │   │   ├── pull-model-form.tsx
   │   │   │   ├── pull-model.tsx
   │   │   │   ├── sidebar-skeleton.tsx
   │   │   │   ├── sidebar.tsx
   │   │   │   ├── ui
   │   │   │   ├── user-settings.tsx
   │   │   │   └── username-form.tsx
   │   │   ├── lib
   │   │   │   ├── model-helper.ts
   │   │   │   └── utils.ts
   │   │   ├── providers
   │   │   │   └── theme-provider.tsx
   │   │   └── utils
   │   │      └── initial-questions.ts
   │   ├── tailwind.config.ts
   │   ├── tsconfig.json
   │   └── yarn.lock
   └── README.md








  开源的 OurJS
OurJS开源博客已经迁移到 OnceOA 平台。

  关注我们
扫一扫即可关注我们:
OnceJS

OnceOA