-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerateTOC.js
85 lines (72 loc) · 2.15 KB
/
generateTOC.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// @ts-nocheck
const fs = require("fs");
/**
* 生成 markdown
*/
function generateMarkdown(plugins) {
let toc_md = `# 插件目录 (${plugins.length})
|#|名称|版本|作者|简介|
|-|---|---|----|---|
`;
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i];
let name = plugin.folder
? `[${plugin.name}](./packages/${plugin.folder})`
: `[${plugin.name}](https://github.com/${plugin.author}/${plugin.name})`;
if (plugin.deprecated) {
name += "(已弃用)";
}
const badge = `[](https://www.npmjs.com/package/${plugin.name})`;
const author = plugin.author.name
? `[${plugin.author.name}](${plugin.author.url})`
: `[@${plugin.author}](https://github.com/${plugin.author})`;
toc_md += `|${i + 1}|${name}|${badge}|${author}|${plugin.description}|\n`;
}
return toc_md;
}
/**
* 生成官方插件目录
*/
function generateOfficialPluginsToc() {
fs.readdir("./packages", (err, files) => {
if (err) console.log(err);
let plugins = [];
files.forEach((file) => {
try {
const pkg = require(`../packages/${file}/package.json`);
pkg.folder = file;
plugins.push(pkg);
} catch (err) {}
});
const md = generateMarkdown(plugins);
fs.writeFile("TOC.md", md, (err) => {
if (err) throw err;
console.log(`成功生成插件目录,共 ${plugins.length} 个插件。`);
});
});
}
/**
* 生成社区插件目录
*/
function generateCommunityPluginsToc() {
try {
fs.mkdirSync("./dist/");
} catch ({ code }) {
if (code !== "EEXIST") return;
}
const yaml = require("js-yaml");
const data = yaml.load(fs.readFileSync("./data/plugins.yml", "utf8"));
if (data.community) {
const communityPlugins = data.community;
const communityMd = generateMarkdown(communityPlugins);
fs.writeFile("./dist/community.md", communityMd, (err) => {
if (err) throw err;
console.log(
`成功生成社区插件目录,共 ${communityPlugins.length} 个插件。`
);
});
}
}
// start
generateOfficialPluginsToc();
generateCommunityPluginsToc();