├── .gitignore ├── package.json ├── pnpm-lock.yaml ├── README.md └── bin └── cli.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "folder-print", 3 | "version": "0.0.8", 4 | "description": "A terminal command line tool for printing the tree structure of the current directory.", 5 | "main": "index.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "bin": { 11 | "folder-print": "./bin/cli.js", 12 | "fp": "./bin/cli.js" 13 | }, 14 | "keywords": [ 15 | "folder", 16 | "file", 17 | "directory structure", 18 | "tree", 19 | "log", 20 | "file-tree", 21 | "folder-print" 22 | ], 23 | "author": "荣顶", 24 | "license": "ISC", 25 | "homepage": "https://github.com/wangrongding/folder-print/", 26 | "repository": { 27 | "type": "git", 28 | "url": "https://github.com/wangrongding/folder-print.git" 29 | }, 30 | "dependencies": { 31 | "chalk": "^5.1.2", 32 | "commander": "^9.4.1" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /pnpm-lock.yaml: -------------------------------------------------------------------------------- 1 | lockfileVersion: 5.4 2 | 3 | specifiers: 4 | chalk: ^5.1.2 5 | commander: ^9.4.1 6 | 7 | dependencies: 8 | chalk: registry.npmmirror.com/chalk/5.1.2 9 | commander: registry.npmmirror.com/commander/9.4.1 10 | 11 | packages: 12 | 13 | registry.npmmirror.com/chalk/5.1.2: 14 | resolution: {integrity: sha512-E5CkT4jWURs1Vy5qGJye+XwCkNj7Od3Af7CP6SujMetSMkLs8Do2RWJK5yx1wamHV/op8Rz+9rltjaTQWDnEFQ==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/chalk/-/chalk-5.1.2.tgz} 15 | name: chalk 16 | version: 5.1.2 17 | engines: {node: ^12.17.0 || ^14.13 || >=16.0.0} 18 | dev: false 19 | 20 | registry.npmmirror.com/commander/9.4.1: 21 | resolution: {integrity: sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==, registry: https://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/commander/-/commander-9.4.1.tgz} 22 | name: commander 23 | version: 9.4.1 24 | engines: {node: ^12.20.0 || >=14} 25 | dev: false 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # folder-print 2 | 3 | ## 介绍(Description) 4 | 5 | 快速生成文件夹目录结构,支持定义目录层级,支持生成到 markdown 文件。 6 | Quickly generate folder directory structure, support for defining directory hierarchy, support for generating to markdown files. 7 | 8 | ## 安装(Install) 9 | 10 | ```sh 11 | npm i folder-print -g 12 | ``` 13 | 14 | 当然你也可以直接使用 npx 运行 15 | Of course you can also run it directly with npx 16 | 17 | ```sh 18 | npx folder-print 19 | ``` 20 | 21 | ## 使用(usage) 22 | 23 | ### 输出当前目录的树形结构:(默认输出最大层级为 10) 24 | 25 | Output the tree structure of the current directory: (default output maximum level is 10) 26 | 27 | ```sh 28 | fp 29 | # or 30 | folder-print 31 | # 免安装(No installation) 32 | npx folder-print 33 | ``` 34 | 35 | ![](https://assets.fedtop.com/picbed/202210182135912.png) 36 | 37 | ### 指定输出最大层级 38 | 39 | (Specify the maximum level of output) 40 | 41 | ```sh 42 | # 指定输出最大层级(Specify the maximum level of output) 43 | fp -d 0 44 | # or 45 | folder-print -d 0 46 | # 免安装(No installation) 47 | npx folder-print -d 0 48 | ``` 49 | 50 | 指定输出最大层级 51 | Specify the maximum level of output 52 | ![](https://assets.fedtop.com/picbed/202210182134984.png) 53 | 54 | ### 将输出结果保存到文件中 55 | 56 | Save the output result to a file 57 | 58 | ```sh 59 | # 将输出结果保存到文件中(Save the output result to a file) 60 | fp -p 61 | # or 62 | folder-print -p 63 | # 免安装(No installation) 64 | npx folder-print -p 65 | ``` 66 | 67 | ![](https://assets.fedtop.com/picbed/202210191352186.png) 68 | 69 | 75 | -------------------------------------------------------------------------------- /bin/cli.js: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node --no-warnings 2 | 3 | import fs from "fs"; 4 | import path from "path"; 5 | import { Command } from "commander"; 6 | import chalk from "chalk"; 7 | 8 | import packageJsonData from "../package.json" assert { type: "json" }; 9 | const { version } = packageJsonData; 10 | const __dirname = path.resolve(); 11 | // const { version } = JSON.parse(fs.readFileSync("package.json", "utf8")); 12 | const program = new Command(); 13 | 14 | program 15 | .name("folder-print") 16 | .description("Using the CLI to generate the tree structure of the directory") 17 | .version(version, "-v, --version, -V"); 18 | program.option( 19 | "-d, --depth ", 20 | "Set the depth of the folder to be traversed", 21 | "10" 22 | ); 23 | program.option("-p, --print", "Generate a markdown file"); 24 | // program 25 | // .command("list") 26 | // .alias("ls") 27 | // .description("List all the registries") 28 | // .action(test); 29 | // program.parse(); 30 | program.parse(process.argv); 31 | // 打印参数 32 | // console.log(program.opts()); 33 | const depth = program.opts().depth; 34 | const print = program.opts().print; 35 | 36 | // 需要过滤的文件夹 37 | const filterDir = [ 38 | "node_modules", 39 | ".git", 40 | ".idea", 41 | "dist", 42 | "build", 43 | ".vscode", 44 | ".DS_Store", 45 | ]; 46 | 47 | // 项目名称 48 | const folderName = `🌸 ${ 49 | __dirname.split("/")[__dirname.split("/").length - 1] 50 | } 🌸 \n`; 51 | const colorFolderName = `🌸 ${chalk.greenBright( 52 | __dirname.split("/")[__dirname.split("/").length - 1] 53 | )} 🌸\n`; 54 | 55 | let content = folderName; 56 | let colorContent = colorFolderName; 57 | // 遍历文件夹,打印目录结构 58 | export function printTree(dir = __dirname, spaceNum) { 59 | // 超出深度,不再遍历 60 | if (spaceNum > depth) { 61 | return; 62 | } 63 | // 读取文件夹 64 | const files = fs.readdirSync(dir).sort(); 65 | // 按文件夹和首字母排序 66 | files.sort((a, b) => { 67 | const aIsDir = fs.statSync(path.join(dir, a)).isDirectory(); 68 | const bIsDir = fs.statSync(path.join(dir, b)).isDirectory(); 69 | if (aIsDir && !bIsDir) { 70 | return -1; 71 | } else if (!aIsDir && bIsDir) { 72 | return 1; 73 | } else { 74 | return a.localeCompare(b); 75 | } 76 | }); 77 | 78 | const length = files.length - 1; 79 | // 遍历文件夹 80 | files.forEach((file, index) => { 81 | const filePath = path.join(dir, file); 82 | const stats = fs.statSync(filePath); 83 | // 过滤文件夹 84 | if (filterDir.includes(file)) { 85 | return; 86 | } 87 | if (stats.isFile()) { 88 | const str = `${generateSpace(spaceNum)}${generateLine( 89 | index, 90 | length 91 | )}${file}`; 92 | content += `${str}\n`; 93 | colorContent += `${str}\n`; 94 | } else if (stats.isDirectory()) { 95 | const str = `${generateSpace(spaceNum)}${generateLine( 96 | index, 97 | length 98 | )}${file}`; 99 | const colorStr = `${generateSpace(spaceNum)}${generateLine( 100 | index, 101 | length 102 | )}${chalk.hex("#4dc4ff").bold(file)}`; 103 | content += `${str}\n`; 104 | colorContent += `${colorStr}\n`; 105 | printTree(filePath, spaceNum + 1); 106 | } 107 | }); 108 | } 109 | 110 | // 生成指定的连接符 111 | function generateLine(index, length) { 112 | if (index === length) { 113 | return "└─ "; 114 | } 115 | return "├─ "; 116 | } 117 | 118 | // 生成指定的空格 119 | function generateSpace(num) { 120 | let space = ``; 121 | for (let i = 0; i < num; i++) { 122 | // space += ` `; 123 | space += `│ `; 124 | } 125 | return space; 126 | } 127 | 128 | printTree(__dirname, 0); 129 | if (print) { 130 | // console.log(content); 131 | content = `\`\`\`sh \n${content}\`\`\``; 132 | fs.writeFileSync("folder-tree.md", content, "utf8"); 133 | console.log("✅ 已将目录结构生成到 folder-tree.md 文件中"); 134 | } else { 135 | console.log(colorContent); 136 | } 137 | --------------------------------------------------------------------------------