This repository was archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-links.mjs
More file actions
97 lines (83 loc) · 2.77 KB
/
Copy pathupdate-links.mjs
File metadata and controls
97 lines (83 loc) · 2.77 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
import chalk from 'chalk';
import {
readFile,
readFileSync,
readdir,
readdirSync,
writeFileSync,
} from 'fs';
import { extname, join } from 'path';
// Paths
const directoryPath = './';
const blackListPath = './src/rule-providers';
const packagePath = 'package.json';
const readme = 'README.md';
/** `jsdelivr` root point */
const rootEndpoint = 'https://cdn.jsdelivr.net';
const keys = ['npm', 'gh/onemoon'];
// Get package's name
const packageContent = readFileSync(packagePath);
const projectName = JSON.parse(packageContent).name + '@latest';
// Get rule-set's path
const ruleSetFile = readdirSync(blackListPath).filter(
(f) => extname(f) === '.yaml'
);
// Generate full path
const linkList = ruleSetFile.map((fileName) => {
return {
type: getType(fileName),
links: keys.map(
(key) =>
new URL(
join(rootEndpoint, key, projectName, 'dist/rule-providers', fileName)
).href
),
};
});
// Generate jsdelivr link, and insert into README.md
readdir(directoryPath, (err, files) => {
if (err) {
console.error('Error reading directory', err);
return;
}
// Filter the files to include only .md files
const mdFiles = files
.filter((file) => extname(file) === '.md')
.filter((f) => f === readme);
// Read the content of each .md file
mdFiles.forEach((mdFile) => {
const filePath = join(directoryPath, mdFile);
const linksStr = linkList
.map(({ links, type }) => {
const linkStr = links.join('\n\n');
return `### ${type}\n\n\`\`\`md\n${linkStr}\n\`\`\``;
})
.join('\n\n');
// console.log(linksStr);
readFile(filePath, 'utf8', (err, mdContent) => {
if (err) {
console.error('Error reading file', filePath, err);
return;
}
// # Rule sets
const regex = /(?<=\:\n)[\s\S]*(?=\n## Software)/;
const replacedContent = mdContent.replace(
regex,
`\n## jsdelivr link of rule providers\n\n${linksStr}\n`
);
// console.log(replacedContent);
writeFileSync(filePath, replacedContent, 'utf8');
console.log(chalk.greenBright('Update readme successfully 🎉 ⚡️'));
});
});
});
function getType(str) {
const match = str.match(/\.(\w+)\.yaml$/); // 匹配文件名中以 . 开头、以 .yaml 结尾的字段,例如 classical
const extracted = match ? match[1] : ''; // 如果匹配成功,则提取匹配的内容;否则为 ''
return extracted;
}
function getUpperCaseType(str) {
const match = str.match(/\.(\w+)\.yaml$/); // 匹配文件名中以 . 开头、以 .yaml 结尾的字段,例如 classical
const extracted = match ? match[1] : ''; // 如果匹配成功,则提取匹配的内容;否则为 ''
return extracted.charAt(0).toUpperCase() + extracted.slice(1); // 将首字母大写
}