-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
scripts.mjs
128 lines (112 loc) · 5.21 KB
/
scripts.mjs
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
import 'dotenv/config'
import fs from 'fs';
import path from 'path';
import { Octokit } from 'octokit';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const repoData = require('./repo.json');
function getProjectLineString(item) {
const dt = [];
dt[0] = item.github ? `[${item.github.replace(/^jaywcjlove\//, '')}](https://github.com/${item.github})` : '-';
dt[1] = item.homepage ? `[\`#homepage\`](${item.homepage})` : '-';
dt[2] = item.github ? `[![GitHub stars](https://img.shields.io/github/stars/${item.github}?style=flat)](https://github.com/${item.github}/stargazers)` : ' ';
dt[3] = item.github ? `[![GitHub last commit](https://img.shields.io/github/last-commit/${item.github}?style=flat&label=last)](https://github.com/${item.github}/commits)` : ' ';
dt[4] = item.npm ? `[![NPM Downloads](https://img.shields.io/npm/dm/${item.npm}.svg?label=&logo=npm&style=flat&labelColor=ffacab&color=dd4e4c)](https://www.npmjs.com/package/${item.npm})` : ' ';
const npmVersion = item.npm ? `[![npm version](https://img.shields.io/npm/v/${item.npm}.svg?label=&logo=npm)](https://www.npmjs.com/package/${item.npm})` : '';
const githubVersion = item.version ? `![GitHub package version](https://img.shields.io/github/v/tag/${item.github}?style=flat&label=&labelColor=555&logo=github)` : '';
dt[5] = npmVersion ? npmVersion : githubVersion;
if (item.stars === false) {
dt[2] = ' '
}
if (item.lastCommit === false) {
dt[3] = ' '
}
return dt.join(' | ');
}
const octokit = new Octokit({ auth: process.env.AUTH || '' });
const reposData = [];
function compare(a, b) {
if (a < b ) {
return 1;
}
if (a > b ) {
return -1;
}
return 0;
}
;(async () => {
const result = await octokit.request('GET /users/{username}/repos?per_page=100&page=1', {
username: 'jaywcjlove'
});
if (result.data && result.data.length) {
reposData.push(result.data);
console.log(`\x1b[35;1m Page 1 data:\x1b[0m \x1b[32;1m${result.data.length}\x1b[0m`);
}
const result2 = await octokit.request('GET /users/{username}/repos?per_page=100&page=2', {
username: 'jaywcjlove'
});
if (result2.data && result2.data.length) {
reposData.push(result2.data);
console.log(`\x1b[35;1m Page 2 data:\x1b[0m \x1b[32;1m${result2.data.length}\x1b[0m`);
}
reposData.flat().forEach(({ full_name, ...rest }) => {
if (!repoData[full_name]) {
repoData[full_name] = {};
}
});
const repoDataPath = path.relative(process.cwd(), 'repo.json');
await fs.writeFileSync(repoDataPath, JSON.stringify(repoData, null, 2));
const baseData = reposData.flat()
.map(({ name, full_name, homepage, archived, watchers_count, forks_count, stargazers_count }) => {
if (archived) return;
const result = {
name, github: full_name, homepage, watchers_count, forks_count, stargazers_count
}
if (repoData[full_name]) {
if (repoData[full_name].npm) {
result.npm = repoData[full_name].npm_name || name;
}
result.version = repoData[full_name].version;
result.category = repoData[full_name].category;
result.stars = repoData[full_name].stars;
result.lastCommit = repoData[full_name].lastCommit;
}
return result;
})
.filter(Boolean)
.sort((a, b) => compare(a.stargazers_count, b.stargazers_count));
const dataPath = path.relative(process.cwd(), 'data.json');
await fs.writeFileSync(dataPath, JSON.stringify(baseData, null, 2));
const mdPath = path.resolve(process.cwd(), 'README.md');
const mdstr = fs.readFileSync(mdPath);
const markdownTable = [
'**Handbook** | **Homepage** | **Stars** | **Last Commit** | **Downloads** | **Version** ',
':--- | --- | :--- | :--- | :--- | :--- ',
[], // 2
'**Github Actions** | **Homepage** | **Stars** | **Last Commit** | **Downloads** | **Version** ',
[], // 4
'**Rehype Plugins** | **Homepage** | **Stars** | **Last Commit** | **Downloads** | **Version** ',
[], // 6
'**SwiftUI Plugins** | **Homepage** | **Stars** | **Last Commit** | **Downloads** | **Version** ',
[], // 8
'**Other Project** | **Homepage** | **Stars** | **Last Commit** | **Downloads** | **Version** ',
[], // 10
];
baseData.forEach((item) => {
if (item.category === 'handbook') {
markdownTable[2].push(getProjectLineString(item));
} else if (item.category === 'action') {
markdownTable[4].push(getProjectLineString(item));
} else if (item.category === 'rehype') {
markdownTable[6].push(getProjectLineString(item));
} else if (item.category === 'swift') {
markdownTable[8].push(getProjectLineString(item));
} else {
markdownTable[10].push(getProjectLineString(item));
}
});
let markdown = mdstr.toString().replace(/<!--repos-start--\>(.*)\s+([\s\S]*?)(\s.+)?<!--repos-end-->/, `<!--repos-start-->\n\n${markdownTable.flat().join('\n')}\n\n<!--repos-end-->`);
// //innerMarkdown = innerMarkdown.replace(/<!--repos-handbook-start-->(.*)\s+([\s\S]*?)(\s.+)?<!--repos-handbook-end-->/, `<!--repos-handbook-start-->\n\n${getMdTableStr(handbook)}\n\n<!--repos-handbook-end-->`)
console.log(`\x1b[35;1m baseData data:\x1b[0m \x1b[32;1m${baseData.length}\x1b[0m`);
fs.writeFileSync(mdPath, markdown);
})();