-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenBar.js
74 lines (60 loc) · 2.33 KB
/
genBar.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
const fs = require("fs")
const path = require('path')
const readLine = require('readline')
const https = require('https')
/** 运行目录 */
const cwd = process.cwd()
/** 读取文件夹 */
function readDirs() {
const dirs = fs.readdirSync(path.resolve(cwd, '.'), { encoding: "utf-8" })
// console.log("文件夹", dirs)
return dirs.filter(_ => _.indexOf('.') == -1) // 没有点的就是文件夹
}
/** 读取单个文件的第一行 */
async function readFileLine(filePath) {
return new Promise((resolve, reject) => {
var arr = [];
var readObj = readLine.createInterface({
input: fs.createReadStream(filePath)
});
readObj.on('line', function (line) {
arr.push(line)
})
readObj.on('close', function () {
resolve(arr[0])
})
})
}
const dirs = readDirs();
(async () => {
let temp = ``;
for (let i = 0; i < dirs.length; i++) {
// 类型标题
const oneTitle = dirs[i].toString()
console.log('%cgenBar.js line:42 oneTitle', 'color: #007acc;', oneTitle);
if (oneTitle.indexOf('public') > -1) continue // 说明是静态文件目录
const files = fs.readdirSync(path.resolve(cwd, oneTitle), { encoding: "utf-8" })
temp += `- ${oneTitle} \r\n\r\n`;
for (let j = 0; j < files.length; j++) {
const filePath = files[j];
// 文件名
const url = oneTitle + '/' + filePath;
const title = await readFileLine(path.resolve(cwd, oneTitle + '/' + filePath))
temp += ` - [${title.replace(/#/g, '').trim()}](${url}) \r\n`
}
temp += `\r\n`
}
fs.writeFileSync(path.resolve(cwd, '_sidebar.md'), temp, { encoding: "utf-8" })
// node 获取最新一条git commit记录的message, 用child_process来实现
const { execSync } = require('child_process')
const commitMessage = execSync('git log -1 --pretty=format:"%s"').toString()
console.log(commitMessage)
const options = {
hostname: 'notice.xiaokyo.com',
port: 443,
path: `/enterprise/sendText?secret=1d269784-1f24-4e0c-8396-c5a1a90da001&content=${encodeURIComponent('文档更新成功了~, \r\nlast update commit message: \r\n' + commitMessage)}`,
method: 'GET'
};
const req = https.request(options)
req.end();
})()