agmd 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.EN.md +79 -0
- package/README.md +46 -105
- package/bin/agmd.js +49 -171
- package/module.js +145 -0
- package/package.json +2 -1
package/README.EN.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# agmd(auto generate md)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
>Enter 'AGMD' in any folder where documents need to be generated, and the directory MD description can be automatically generated
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
[]( https://camo.githubusercontent.com/28479a7a834310a667f36760a27283f7389e864a/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f6c2f76322d646174657069636b65722e737667 )
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
English| [中文](https://github.com/majun2232/vue3sketchRuler/blob/master/README.md)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Case
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
! [image]( https://github.com/majun2232/auto-generate-md/blob/master/md.png )
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Method of use
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
Global installation (Global installation is required, otherwise it will be reported that the "AGMD" item cannot be recognized as the name of cmdlet, function, script file or runnable program)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
> npm i agmd -g
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
After installation, enter 'AGMD' under the folder where MD needs to be recorded, and the names of folders and files under the relative path will be automatically generated. If comments are written in the header of the file, it will be brought together to automatically generate MD files. The generated file name is' readme md.md ', and the path is the path just entered. At the same level, for the development of large projects, this script may save you some time.
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
Example is some of the files I prepared for the demonstration, which is of no other use
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
### Creative background
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
1. Have you been asked to write a MD description of the directory file?
|
|
50
|
+
|
|
51
|
+
2. Or if the project directory and files have been relocated and reconstructed, the directory description in the MD file needs to be modified again
|
|
52
|
+
|
|
53
|
+
3. After taking over the old project and reading the MD instructions, you can see the file functions in the folder at a glance instead of clicking on the corresponding file
|
|
54
|
+
|
|
55
|
+
4. You need to take notes to analyze the source code project
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
### Function
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
1. Automatically generate folder names and files matching directories (sorted by name)
|
|
64
|
+
|
|
65
|
+
2. Automatically determine the hierarchical directory and indent it
|
|
66
|
+
|
|
67
|
+
3. If there is a comment at the top of the file, it will be judged automatically
|
|
68
|
+
|
|
69
|
+
4. Support recursive search of subordinate files in any file directory (do not execute in a large directory!!! Recursion until there are no files in this directory)
|
|
70
|
+
|
|
71
|
+
5. Current support record js . vue . Ts and folders, of course, also support others. I've written so much about this version. You can mention PR if you need it later
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
### Related articles
|
|
78
|
+
|
|
79
|
+
[Nuggets - auto generate directory MD file](https://juejin.cn/post/7030030599268073508)
|
package/README.md
CHANGED
|
@@ -1,105 +1,46 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
let note = '' // 文件注释
|
|
48
|
-
let arr = filterArr.findIndex(obj => obj === item.name)
|
|
49
|
-
if (arr === -1) {
|
|
50
|
-
const fullPath = path.join(dir, item.name)
|
|
51
|
-
const isDir = fs.lstatSync(fullPath).isDirectory()
|
|
52
|
-
if (isDir) {
|
|
53
|
-
// 递归
|
|
54
|
-
getFileNodes((item.children = []), fullPath, level + 1)
|
|
55
|
-
} else {
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
nodes.push(item)
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
// 控制返回时间节点,不让提前返回
|
|
62
|
-
return nodes
|
|
63
|
-
}
|
|
64
|
-
```
|
|
65
|
-
调用上面的方法,就能够得到一个生成的children的对象,把它打印生成到js文件中,这样子方便后续调试`wirteJs(JSON.stringify(nodes), './readme-file.js')`
|
|
66
|
-
|
|
67
|
-
```js
|
|
68
|
-
/**
|
|
69
|
-
* @description: 把结果写入到js文件
|
|
70
|
-
* @param {data} 要写的数据
|
|
71
|
-
* @return {fileName} 要写入文件地址
|
|
72
|
-
*/
|
|
73
|
-
function wirteJs(data, filePath) {
|
|
74
|
-
let file = path.resolve(__dirname, filePath)
|
|
75
|
-
const pre = 'export default'
|
|
76
|
-
// 异步写入数据到文件
|
|
77
|
-
fs.writeFile(file, pre + data, { encoding: 'utf8' }, err => {})
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
再用一个递归取出所有需要的信息,有人可能会要讲一个递归不能搞定么? 还要搞两个?我之前也是搞的一个,是能生成出来,但是因为生成出来的文件,没有按我们需要的要求,比如文件排前面,按名称排序,所以生成出来的对应不到编辑器,那还是个摆设,下面的两个push就不能合成一个,合成一个就会出现文件夹和文件位置摆放错误,虽然文字还是相同的文字,位置很重要
|
|
81
|
-
|
|
82
|
-
```js
|
|
83
|
-
/**
|
|
84
|
-
* @description: 递归得到文件名+note
|
|
85
|
-
* @param {*} datas
|
|
86
|
-
* @param {*} keys
|
|
87
|
-
* @return {*}
|
|
88
|
-
*/
|
|
89
|
-
function getNote(datas, keys) {
|
|
90
|
-
let nodes = keys ? keys : []
|
|
91
|
-
datas.forEach(obj => {
|
|
92
|
-
if (obj.children) {
|
|
93
|
-
// 文件夹
|
|
94
|
-
let md = setMd(obj)
|
|
95
|
-
nodes.push(md)
|
|
96
|
-
getNote(obj.children, nodes)
|
|
97
|
-
}
|
|
98
|
-
// 文件
|
|
99
|
-
else {
|
|
100
|
-
let md = setMd(obj)
|
|
101
|
-
nodes.push(md)
|
|
102
|
-
}
|
|
103
|
-
})
|
|
104
|
-
return nodes
|
|
105
|
-
}
|
|
1
|
+
# agmd(auto generate md)
|
|
2
|
+
|
|
3
|
+
> 在任何需要生成文档的,文件夹下输入`agmd`, 就能自动生成目录 md 说明
|
|
4
|
+
|
|
5
|
+
[](https://camo.githubusercontent.com/28479a7a834310a667f36760a27283f7389e864a/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f6c2f76322d646174657069636b65722e737667)
|
|
6
|
+
|
|
7
|
+
简体中文 | [English](https://github.com/majun2232/vue3sketchRuler/blob/master/README.EN.md)
|
|
8
|
+
|
|
9
|
+
### 案例
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
### 使用方法
|
|
14
|
+
需要有node环境
|
|
15
|
+
1. 全局安装(必须全局安装,否则会报`无法将“agmd”项识别为 cmdlet、函数、脚本文件或可运行程序的名称`)
|
|
16
|
+
|
|
17
|
+
> npm i agmd -g
|
|
18
|
+
|
|
19
|
+
安装完成后,在需要记录 md 的文件夹下面输入`agmd`,会自动生成相对路径下的文件夹和文件的名字,如果文件里面还有在头部写注释的话,那么会一并带过来自动生成 md 文件。生成的文件名为`readme-md.md`, 路径为刚刚输入命名的路径同级别下,对于工程比较大的开发来说,这个脚本或许会帮你省下些许时间。
|
|
20
|
+
|
|
21
|
+
example,是我为演示准备的一些文件,并没有其他用
|
|
22
|
+
|
|
23
|
+
2. 高级用法
|
|
24
|
+
有些需要把自动生成的文档插入到某个自动生成的 md 当中, 该插件导出了自动生成的 md 数据方法, 还有`getFileNodes`获得所有文件的具体信息, 可以 DIY 做出不同的文档
|
|
25
|
+
> import {getFileNodes, getMd} from agmd
|
|
26
|
+
> 其中 getFileNodes 可以获得具体文件相关的信息
|
|
27
|
+
> getMd 得到最终输出的信息
|
|
28
|
+
|
|
29
|
+
### 创作背景
|
|
30
|
+
|
|
31
|
+
1. 大家有没有被要求写一个目录文件的 md 说明呢?
|
|
32
|
+
2. 或者工程目录和文件被移动位置重构了,这时还需要重新修改 md 文件里面的目录说明
|
|
33
|
+
3. 接手老工程,看了 md 说明,能对文件夹里面的文件功能做到一目了然,而不是点开对应文件去看
|
|
34
|
+
4. 分析源码工程需要做点笔记
|
|
35
|
+
|
|
36
|
+
### 功能
|
|
37
|
+
|
|
38
|
+
1. 自动生成匹配目录的文件夹名和文件(已经按名称进行排序)
|
|
39
|
+
2. 自动进行层级目录判断进行缩进
|
|
40
|
+
3. 如果文件顶部有注释, 那么会自动进行判断
|
|
41
|
+
4. 支持在任意文件目录下递归查找下级文件(不要在很大目录下执行啊!!!递归直到该级目录下没有文件为止)
|
|
42
|
+
5. 目前支持记录 .js .vue .ts 和文件夹, 当然也支持其他, 这个版本我就写这么多, 后续有需要的可以提 pr
|
|
43
|
+
|
|
44
|
+
### 相关文章
|
|
45
|
+
|
|
46
|
+
[掘金-自动生成目录 md 文件](https://juejin.cn/post/7030030599268073508)
|
package/bin/agmd.js
CHANGED
|
@@ -1,171 +1,49 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// 获取文件的头部注释工具
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* @
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (!a.isDir && b.isDir) return 1;
|
|
51
|
-
if (a.isDir && !b.isDir) return -1;
|
|
52
|
-
if ((a.isDir && b.isDir) || (!a.isDir && !b.isDir)) return 0;
|
|
53
|
-
});
|
|
54
|
-
for (let index = 0; index < files.length; index++) {
|
|
55
|
-
const item = files[index];
|
|
56
|
-
let note = ""; // 文件注释
|
|
57
|
-
let arr = filterArr.findIndex((obj) => obj === item.name);
|
|
58
|
-
if (arr === -1) {
|
|
59
|
-
const fullPath = path.join(dir, item.name);
|
|
60
|
-
const isDir = fs.lstatSync(fullPath).isDirectory();
|
|
61
|
-
if (isDir) {
|
|
62
|
-
// 递归
|
|
63
|
-
getFileNodes((item.children = []), fullPath, level + 1);
|
|
64
|
-
} else {
|
|
65
|
-
const index = fullPath.lastIndexOf(".");
|
|
66
|
-
const lastName = fullPath.substring(index);
|
|
67
|
-
// 这里只获取js和vue,ts文件的注释
|
|
68
|
-
if ([".js", ".vue", ".ts"].includes(lastName)) {
|
|
69
|
-
note = getFile(fullPath);
|
|
70
|
-
}
|
|
71
|
-
item.note = note;
|
|
72
|
-
}
|
|
73
|
-
nodes.push(item);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
// 控制返回时间节点,不让提前返回
|
|
77
|
-
return nodes;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* @description: 递归得到文件名+note
|
|
82
|
-
* @param {*} datas
|
|
83
|
-
* @param {*} keys
|
|
84
|
-
* @return {*}
|
|
85
|
-
*/
|
|
86
|
-
function getNote(datas, keys) {
|
|
87
|
-
let nodes = keys ? keys : [];
|
|
88
|
-
datas.forEach((obj) => {
|
|
89
|
-
if (obj.children) {
|
|
90
|
-
// 文件夹
|
|
91
|
-
let md = setMd(obj);
|
|
92
|
-
nodes.push(md);
|
|
93
|
-
getNote(obj.children, nodes);
|
|
94
|
-
}
|
|
95
|
-
// 文件
|
|
96
|
-
else {
|
|
97
|
-
let md = setMd(obj);
|
|
98
|
-
nodes.push(md);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
return nodes;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* @description: 一个obj生成一个一行文字
|
|
106
|
-
* @param {*} obj
|
|
107
|
-
* @return {*}
|
|
108
|
-
*/
|
|
109
|
-
function setMd(obj) {
|
|
110
|
-
let filesString = "";
|
|
111
|
-
// 把文件夹输出,并且level+1
|
|
112
|
-
const blank = " ".repeat(obj.level); // 重复空白
|
|
113
|
-
if (obj.isDir) {
|
|
114
|
-
filesString += `${blank}+ ${obj.name}\n`;
|
|
115
|
-
} else {
|
|
116
|
-
var index = obj.name.lastIndexOf(".");
|
|
117
|
-
const lastName = obj.name.substring(index);
|
|
118
|
-
// 这里只获取js和vue文件的注释,需要其他这里加入
|
|
119
|
-
if ([".js", ".vue", ".ts"].includes(lastName) || index === -1) {
|
|
120
|
-
filesString += `${blank} ${obj.name} ${obj.note}\n`;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return filesString;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* @description: 把结果写入到js文件
|
|
128
|
-
* @param {data} 要写的数据
|
|
129
|
-
* @return {fileName} 要写入文件地址
|
|
130
|
-
*/
|
|
131
|
-
function wirteJs(data, filePath) {
|
|
132
|
-
let file = path.resolve(__dirname, filePath);
|
|
133
|
-
const pre = "export default";
|
|
134
|
-
// 异步写入数据到文件
|
|
135
|
-
fs.writeFile(file, pre + data, { encoding: "utf8" }, (err) => {});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* @description: 把结果写入到js文件
|
|
140
|
-
* @param {data} 要写的数据
|
|
141
|
-
* @return {fileName} 要写入文件地址
|
|
142
|
-
*/
|
|
143
|
-
function wirteMd(data, filePath) {
|
|
144
|
-
let file = path.resolve(__dirname, filePath);
|
|
145
|
-
const pre = "```js\n";
|
|
146
|
-
const last = "```\n";
|
|
147
|
-
// 异步写入数据到文件
|
|
148
|
-
fs.writeFile(file, pre + data + last, { encoding: "utf8" }, (err) => {});
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* @description: 自动生成全流程
|
|
153
|
-
* @param {*}
|
|
154
|
-
* @return {*}
|
|
155
|
-
*/
|
|
156
|
-
function agmd() {
|
|
157
|
-
console.log(path.resolve("./"), "执行位置");
|
|
158
|
-
const nodes = getFileNodes();
|
|
159
|
-
// console.log(__dirname + "\\readme-file.js", "nodes");
|
|
160
|
-
const note = getNote(nodes); // 得到所有note的数组
|
|
161
|
-
const md = note.join(""); // 数组转字符串
|
|
162
|
-
if (md.length > 0) {
|
|
163
|
-
console.log("Automatic generation completed ! ");
|
|
164
|
-
}
|
|
165
|
-
// 得到md对象
|
|
166
|
-
// wirteJs(JSON.stringify(nodes), __dirname + "\\readme-file.js");
|
|
167
|
-
// 得到md文档
|
|
168
|
-
console.log("生成位置", path.resolve("./") + "\\readme-md.md");
|
|
169
|
-
wirteMd(md, path.resolve("./") + "\\readme-md.md");
|
|
170
|
-
}
|
|
171
|
-
agmd();
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// 获取文件的头部注释工具
|
|
3
|
+
import path from "path";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
const __dirname = path.resolve();
|
|
6
|
+
import { getMd } from "../module.js";
|
|
7
|
+
/**
|
|
8
|
+
* @description: 把结果写入到js文件
|
|
9
|
+
* @param {data} 要写的数据
|
|
10
|
+
* @return {fileName} 要写入文件地址
|
|
11
|
+
*/
|
|
12
|
+
function wirteJs(data, filePath) {
|
|
13
|
+
let file = path.resolve(__dirname, filePath);
|
|
14
|
+
const pre = "export default";
|
|
15
|
+
// 异步写入数据到文件
|
|
16
|
+
fs.writeFile(file, pre + data, { encoding: "utf8" }, (err) => {});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @description: 把结果写入到js文件
|
|
21
|
+
* @param {data} 要写的数据
|
|
22
|
+
* @return {fileName} 要写入文件地址
|
|
23
|
+
*/
|
|
24
|
+
function wirteMd(data, filePath) {
|
|
25
|
+
let file = path.resolve(__dirname, filePath);
|
|
26
|
+
const pre = "```js\n";
|
|
27
|
+
const last = "```\n";
|
|
28
|
+
// 异步写入数据到文件
|
|
29
|
+
fs.writeFile(file, pre + data + last, { encoding: "utf8" }, (err) => {});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @description: 自动生成全流程
|
|
34
|
+
* @param {*}
|
|
35
|
+
* @return {*}
|
|
36
|
+
*/
|
|
37
|
+
function agmd() {
|
|
38
|
+
const md = getMd();
|
|
39
|
+
// 得到md对象
|
|
40
|
+
// wirteJs(JSON.stringify(nodes), __dirname + "\\readme-file.js");
|
|
41
|
+
// 得到md文档
|
|
42
|
+
console.log(
|
|
43
|
+
"\x1B[36m%s\x1B[0m",
|
|
44
|
+
"*** location: ",
|
|
45
|
+
path.resolve("./") + "\\readme-md.md"
|
|
46
|
+
);
|
|
47
|
+
wirteMd(md, path.resolve("./") + "\\readme-md.md");
|
|
48
|
+
}
|
|
49
|
+
agmd();
|
package/module.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @description: 获取文件的头部注释
|
|
6
|
+
* @param {*} file
|
|
7
|
+
* @return {*}
|
|
8
|
+
*/
|
|
9
|
+
function getFile(file) {
|
|
10
|
+
var str = fs.readFileSync(file, "utf-8");
|
|
11
|
+
let sarr = str.split(/[\n,]/g);
|
|
12
|
+
// console.log(file);
|
|
13
|
+
let f =
|
|
14
|
+
sarr[0].indexOf("eslint") == -1 &&
|
|
15
|
+
(sarr[0].indexOf("-->") > -1 ||
|
|
16
|
+
sarr[0].indexOf("*/") > -1 ||
|
|
17
|
+
sarr[0].indexOf("//") > -1)
|
|
18
|
+
? sarr[0]
|
|
19
|
+
: "";
|
|
20
|
+
// console.log(f);
|
|
21
|
+
return f;
|
|
22
|
+
}
|
|
23
|
+
// 过滤文件夹
|
|
24
|
+
const filterArr = [
|
|
25
|
+
"img",
|
|
26
|
+
"styles",
|
|
27
|
+
"node_modules",
|
|
28
|
+
".git",
|
|
29
|
+
".github",
|
|
30
|
+
"dist",
|
|
31
|
+
".husky",
|
|
32
|
+
".vscode",
|
|
33
|
+
];
|
|
34
|
+
// 这里只获取某后缀文件的注释,需要其他这里加入
|
|
35
|
+
const includeArrs = [".js", ".vue", ".ts"];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @description: 生成所有文件的node信息
|
|
39
|
+
* @param {*} nodes
|
|
40
|
+
* @param {*} dir
|
|
41
|
+
* @param {*} level
|
|
42
|
+
* @return {*}
|
|
43
|
+
*/
|
|
44
|
+
function getFileNodes(nodes = [], dir = path.resolve("./"), level = 0) {
|
|
45
|
+
let files = fs
|
|
46
|
+
.readdirSync(dir)
|
|
47
|
+
.map((item) => {
|
|
48
|
+
const fullPath = path.join(dir, item);
|
|
49
|
+
const isDir = fs.lstatSync(fullPath).isDirectory();
|
|
50
|
+
return {
|
|
51
|
+
name: item,
|
|
52
|
+
isDir: isDir,
|
|
53
|
+
level,
|
|
54
|
+
};
|
|
55
|
+
})
|
|
56
|
+
// 对文件夹和文件进行排序,要不然生成的和编辑器打开的顺序不对应
|
|
57
|
+
.sort((a, b) => {
|
|
58
|
+
if (!a.isDir && b.isDir) return 1;
|
|
59
|
+
if (a.isDir && !b.isDir) return -1;
|
|
60
|
+
if ((a.isDir && b.isDir) || (!a.isDir && !b.isDir)) return 0;
|
|
61
|
+
});
|
|
62
|
+
for (let index = 0; index < files.length; index++) {
|
|
63
|
+
const item = files[index];
|
|
64
|
+
let note = ""; // 文件注释
|
|
65
|
+
let arr = filterArr.findIndex((obj) => obj === item.name);
|
|
66
|
+
if (arr === -1) {
|
|
67
|
+
const fullPath = path.join(dir, item.name);
|
|
68
|
+
const isDir = fs.lstatSync(fullPath).isDirectory();
|
|
69
|
+
if (isDir) {
|
|
70
|
+
// 递归
|
|
71
|
+
getFileNodes((item.children = []), fullPath, level + 1);
|
|
72
|
+
} else {
|
|
73
|
+
const index = fullPath.lastIndexOf(".");
|
|
74
|
+
const lastName = fullPath.substring(index);
|
|
75
|
+
// 这里只获取js和vue,ts文件的注释
|
|
76
|
+
if ([".js", ".vue", ".ts"].includes(lastName)) {
|
|
77
|
+
note = getFile(fullPath);
|
|
78
|
+
}
|
|
79
|
+
item.note = note;
|
|
80
|
+
}
|
|
81
|
+
nodes.push(item);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// 控制返回时间节点,不让提前返回
|
|
85
|
+
return nodes;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @description: 递归得到文件名+note
|
|
90
|
+
* @param {*} datas
|
|
91
|
+
* @param {*} keys
|
|
92
|
+
* @return {*}
|
|
93
|
+
*/
|
|
94
|
+
function getNote(datas, keys) {
|
|
95
|
+
let nodes = keys ? keys : [];
|
|
96
|
+
datas.forEach((obj) => {
|
|
97
|
+
if (obj.children) {
|
|
98
|
+
// 文件夹
|
|
99
|
+
let md = setMd(obj);
|
|
100
|
+
nodes.push(md);
|
|
101
|
+
getNote(obj.children, nodes);
|
|
102
|
+
}
|
|
103
|
+
// 文件
|
|
104
|
+
else {
|
|
105
|
+
let md = setMd(obj);
|
|
106
|
+
nodes.push(md);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return nodes;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @description: 一个obj生成一个一行文字
|
|
114
|
+
* @param {*} obj
|
|
115
|
+
* @return {*}
|
|
116
|
+
*/
|
|
117
|
+
function setMd(obj) {
|
|
118
|
+
let filesString = "";
|
|
119
|
+
// 把文件夹输出,并且level+1
|
|
120
|
+
const blank = " ".repeat(obj.level); // 重复空白
|
|
121
|
+
if (obj.isDir) {
|
|
122
|
+
filesString += `${blank}+ ${obj.name}\n`;
|
|
123
|
+
} else {
|
|
124
|
+
var index = obj.name.lastIndexOf(".");
|
|
125
|
+
const lastName = obj.name.substring(index);
|
|
126
|
+
// 这里只获取某后缀文件的注释
|
|
127
|
+
if ( includeArrs.includes(lastName) || index === -1) {
|
|
128
|
+
filesString += `${blank} ${obj.name} ${obj.note}\n`;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return filesString;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function getMd() {
|
|
135
|
+
console.log("\x1B[36m%s\x1B[0m", "*** run location: ", path.resolve("./"));
|
|
136
|
+
const nodes = getFileNodes();
|
|
137
|
+
const note = getNote(nodes); // 得到所有note的数组
|
|
138
|
+
const md = note.join(""); // 数组转字符串
|
|
139
|
+
if (md.length > 0) {
|
|
140
|
+
console.log("\x1B[36m%s\x1B[0m", "*** Automatic generation completed ! ");
|
|
141
|
+
}
|
|
142
|
+
return md;
|
|
143
|
+
}
|
|
144
|
+
/* 导出获取md的getMd方法 */
|
|
145
|
+
export { getMd, getFileNodes };
|