-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgenerateData.js
More file actions
161 lines (125 loc) · 4.41 KB
/
generateData.js
File metadata and controls
161 lines (125 loc) · 4.41 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// node buildChangelog.js <token>
const { request } = require("http");
const formatDate = (isoDateStr) => {
const date = new Date(isoDateStr);
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");
const hours = (date.getHours() + 8).toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
const seconds = date.getSeconds().toString().padStart(2, "0");
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};
const app = () => {
// 生成 changelog
const generateChangelog = () => {
const fetchReleases = async (params) => {
const BaseUrl = "https://api.github.com";
const pageSize = 100;
let page = 1;
const releases = [];
const fetchRelease = async (params, page) => {
const { token, owner, repo } = params;
const url = `${BaseUrl}/repos/${owner}/${repo}/releases?page=${page}&per_page=${pageSize}`;
const headers = { "X-GitHub-Api-Version": "2022-11-28" };
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const response = await fetch(url, { headers });
return await response.json();
};
while (page) {
const release = await fetchRelease(params, page);
if (release?.message) {
throw new Error(JSON.stringify(release));
}
releases.push(...release);
if (release.length < pageSize) {
break;
}
page++;
}
return releases;
};
fetchReleases({
token: process.argv.slice(2)[0],
owner: "slowlyo",
repo: "owl-admin",
}).then((res) => {
const content = res
.map(
(release) => `## ${release.tag_name}
> ${formatDate(release.published_at)}
${release.body}
[View on GitHub](${release.html_url})
<br>
<br>
<br>
`
)
.join("\n");
const frontMatter = `---
footer: false
sidebar: false
---
`;
require("fs").writeFileSync(
"./docs/changelog/index.md",
frontMatter + content
);
console.log("changelog done.");
});
};
// 复制 ./donate-data.json 到 ./docs/public/donate-data.json
const copyDonateData = () => {
require("fs").copyFileSync(
"./donate-data.json",
"./docs/public/donate-data.json"
);
};
// 获取扩展数据
const generateExtensionData = async () => {
let data = [];
const url = `https://packagist.org/search.json?tags=owl-admin&per_page=100`;
const query = async (url) => {
const response = await fetch(url);
const json = await response.json();
data.push(...json.results);
if (json?.next) {
await query(json.next);
}
};
await query(url);
// 处理数据
data = await Promise.all(
data
// 排除 slowlyo/owl-admin
.filter((item) => item.name !== "slowlyo/owl-admin")
// 获取最后一个版本的信息
.map(async (item) => {
const url = `https://repo.packagist.org/p2/${item.name}.json`;
const data = await fetch(url);
const json = await data.json();
const lastVersion = json.packages[item.name][0];
item.last_version = lastVersion?.version;
item.latest_update = lastVersion?.time ? formatDate(lastVersion.time) : null;
return item;
})
);
// 按 downloads 排序
data = data.sort((a, b) => b.downloads - a.downloads);
// 生成 ./docs/public/extension-data.json
require("fs").writeFileSync(
"./docs/public/extension-data.json",
JSON.stringify(data, null, 2)
);
console.log("extension-data done.");
};
const run = () => {
generateChangelog();
copyDonateData();
generateExtensionData();
};
run();
};
app();