-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (92 loc) · 2.98 KB
/
index.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
const axios = require('axios').default
const dayjs = require('dayjs')
const fs = require('fs')
const path = require('path')
require('dotenv').config()
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
dayjs.extend(utc)
dayjs.extend(timezone)
if (process.env.TOKEN)
axios.defaults.headers = {
Authorization: `token ${process.env.TOKEN}`,
}
if (!process.env.REPOSITORY) {
console.log('需要配置好REPOSITORY才可以正常使用')
return
}
// 数据格式化
function createItem({ title, body: description }) {
const result = title.match(/\[(.*?)\]\((.*?)\)/)
return {
name: result[1],
url: result[2],
description,
}
}
// 数据排序
function labelSort(list) {
// 对列表数据按照name正则进行提取并排序
const seqList = list
.filter((item) => /\d+#/.test(item.name))
.sort((a, b) =>
Number(a.name.match(/(\d+)#/)[1] - Number(b.name.match(/(\d+)#/)[1]))
)
// 获得不符合正则的项目,按照首字母进行排序
// [js中sort方法的排序问题及localeCompare方法](https://juejin.cn/post/6844903816597504008)
const otherList = list
.filter((item) => !/\d+#/.test(item.name))
.sort((a, b) => a.name.localeCompare(b.name, 'zh-CN'))
// 拼合数据
const finalList = seqList.concat(otherList)
return finalList
}
async function main() {
// 获取标签
let { data: labels } = await axios.get(
`https://api.github.com/repos/${process.env.REPOSITORY}/labels`
)
labels = labelSort(labels)
// * axios的url记得encode,否则Request path contains unescaped characters
// #符号对应%23 [url参数中有+、空格、=、%、&、#等特殊符号的处理_Benjamin——前端攻城师-CSDN博客](https://blog.csdn.net/cuew1987/article/details/10952509)
const promises = labels.map((label) =>
axios.get(
encodeURI(
`https://api.github.com/repos/${process.env.REPOSITORY}/issues?labels=${label.name}`
).replace('#', '%23')
)
)
const result = await Promise.all(promises)
const list = []
for (let i in result) {
const { data: items } = result[i]
const labelName = labels[i].name.replace(/\d+#/, '')
if (items.length === 0) {
continue
}
const newItems = []
for (let item of items) {
if (/\[(.*?)\]\((.*?)\)/.test(item.title)) newItems.push(createItem(item))
}
list.push({
label: labelName,
items: newItems,
})
}
// 在之前就遇到过这种情况,欸,搞成i18n了,应该是时区切换
const obj = {
author: process.env.REPOSITORY.split('/')[0],
github: `https://github.com/${process.env.REPOSITORY}`,
updateTime: dayjs().tz('Asia/ShangHai').format(),
list,
}
console.log(JSON.stringify(obj, null, 2))
if (!fs.existsSync('api')) {
fs.mkdirSync('api')
}
fs.writeFileSync(path.join(__dirname, '/api/urls.json'), JSON.stringify(obj))
console.log(
`api url: https://cdn.jsdelivr.net/gh/${process.env.REPOSITORY}/api/urls.json`
)
}
main()