nodejs项目_一个极简的nodejs项目目录结构文本输出

7ed8e7c446dc0cb86b2eb0e3932cc4dc.png

有时候需要做一些技术分享,对项目目录结构做一个简单的介绍,需要输出一个文本格式的项目目录结构。起初在github上面找了几个项目来做这个事,但是觉得使用很麻烦索性自己动手写一个,按照npm list 的输出方式输出20来行代码搞定使用方便。

代码

const fs = require("fs");
const path = require("path");
const rootDir = path.resolve(__dirname);

const createDirTree = (dir,skip) => {

    console.log('+—— root');
    const recur = (curDir) => {
        fs.readdirSync(curDir,{withFileTypes:true}).forEach((value) => {
            if(skip.includes(value.name)) return ;
            const childPath = path.join(curDir,value.name); 
            const depths = childPath.replace(rootDir,'').split(path.sep).filter(Boolean).length ;
            const isDirectory = value.isDirectory();
            const prefix = `${'| '.repeat(depths -1)}${ isDirectory ? '+' : '`' }-- `; 
            console.log(`${prefix}${value.name}`);
            if(isDirectory){
            recur(path.join(curDir,value.name));
            }
        });
    }
    recur(dir);
}

createDirTree(rootDir,["node_modules",".vscode",".git",".next","_next"])

使用

copy上面代码放到自己想要输出目录结构的项目创建一个js文件用node去执行。

输出例子

+—— root
`-- .gitignore
`-- README.md
+-- components
| +-- Footer
| | `-- index.tsx
| +-- Header
| | `-- index.tsx
| +-- Loyout
| | `-- index.tsx
| +-- styles
+-- mock
| `-- quotes.json
`-- next-env.d.ts
`-- next.config.js
+-- out
| +-- 404
| | `-- index.html
| `-- 404.html
`-- package.json
+-- pages
| +-- P
| | `-- [id].tsx
| `-- about.tsx
| +-- api
| | `-- randomQuote.ts
| `-- index.tsx
| `-- post.tsx
+-- public
+-- styles
| `-- index.less
| `-- reset.less
`-- test.js
`-- tsconfig.json
`-- yarn.lock

github地址: JSlife23/DirTree

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值