-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-doc.js
136 lines (124 loc) · 4.03 KB
/
sync-doc.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const urllib = require('urllib')
const path = require('path')
const fs = require('fs-extra')
// const rm = require('rimraf')
// const pinyin = require('pinyin')
const AV = require('leancloud-storage')
const appId = 'iYzWnL2H72jtQgNQPXUvjFqU-gzGzoHsz'
const appKey = 'OR3zEynwWJ7f8bk95AdiGFzJ'
const serverURLs = 'https://api.iming.work'
AV.init({ appId, appKey, serverURLs })
const docPath = path.resolve(__dirname, 'docs/detail')
const configPath = path.resolve(__dirname, 'docs/.vitepress/sync-doc.json')
const diffConfigPath = path.resolve(__dirname, './docs/public/sync-doc-config.json')
const json = []
function getAv () {
const instance = new AV.Query('Article')
instance.select(['title', 'tag', 'isOuterLink', 'type'])
instance.notEqualTo('type', 'record-days')
instance.descending('createdAt')
return instance.find()
}
function getDetail (id) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(new AV.Query('Article').get(id))
}, 300)
})
}
async function getDocConfig () {
// return new Promise((resolve, reject) => {
// // urllib.request('http://localhost:3000/sync-doc-config.json', {
// })
const data = await urllib.request('https://iming.work/demo/vitepress-blog/dist/sync-doc-config.json', {
method: 'GET',
dataType: 'json'
}).catch(() => {
return fs.readFile(diffConfigPath, 'utf8').catch(() => ({}))
})
return typeof data === 'string' ? JSON.parse(data) : data
}
async function genDirsAndMd (result) {
const diffConfig = await getDocConfig()
for (const year of Object.keys(result).reverse()) {
let jsonItem = json.find(x => x.text.startsWith(year))
if (!jsonItem) {
jsonItem = { text: `${year}年`, items: [] }
json.push(jsonItem)
}
const value = result[year]
for (const item of value) {
// const needUpdate = !diffConfig[item.id] || (new Date(item.updatedAt).getTime() > diffConfig[item.id])
/**
* 不更新首页的链接不会生成,无法点击
* @type {boolean}
*/
const needUpdate = true
jsonItem.items.push(item)
item.link = `/${path.join('detail', item.id)}`
if (needUpdate) {
console.log(`生成[${item.text}] ${item.createdAt}...`)
// const pinyin = getPinYin(item.text)
const { input } = (await getDetail(item.id)).toJSON()
const targetFile = path.join(docPath, '../', `${item.link}.md`)
fs.outputFileSync(targetFile, `# ${item.text}\n\n${input}`)
diffConfig[item.id] = Date.now()
}
}
}
return diffConfig
}
// function getPinYin (str) {
// return pinyin(str.replace(/\//g, '_'), {
// style: pinyin.STYLE_NORMAL, // 设置拼音风格
// heteronym: true,
// segment: true // 启用分词
// // group: true
// }).flat().join('').replace(/\s+/g, '')
// }
async function run () {
console.log('开始执行...')
// rm.sync(docPath)
fs.ensureDirSync(docPath)
const res = await getAv()
const result = {}
res.forEach(j => {
const x = j.toJSON()
const year = x.createdAt.split('T')[0].split('-')[0]
if (!result[year]) {
result[year] = []
}
if (!x.tag || !x.tag.startsWith('http')) {
result[year].push({
text: x.title,
id: j.id,
tag: x.tag,
createdAt: x.createdAt,
updatedAt: x.updatedAt
})
}
})
// 分组
const diffConfig = await genDirsAndMd(result)
await fs.outputFile(configPath, JSON.stringify(json, null, 2))
await fs.outputFile(diffConfigPath, JSON.stringify(diffConfig, null, 2))
/**
* 重写文件时间
* 如果文件未修改,通过 github action 自动拉取后的文件时间非文章编辑时间
* 需要再次修改
*/
json.reduce((arr, cur) => arr.concat(cur.items), []).forEach(item => {
const targetFile = path.join(docPath, '../', `${item.link}.md`)
if (fs.pathExistsSync(targetFile)) {
fs.utimesSync(targetFile, new Date(item.createdAt), new Date(item.updatedAt))
}
})
console.log('写入配置完成')
}
(async () => {
try {
await run()
} catch (e) {
console.log(e)
}
})()