Skip to content

Commit 2b54c1a

Browse files
authored
Update update_links.js
1 parent 153c07e commit 2b54c1a

1 file changed

Lines changed: 41 additions & 32 deletions

File tree

scripts/update_links.js

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,49 @@
1-
import { execSync } from "child_process";
2-
import fs from "fs";
3-
import path from "path";
4-
5-
const repo = "ScriptingApp/Community-Scripts";
6-
const branch = "main";
7-
const readmePath = path.resolve("README.md");
8-
9-
// 获取这次提交中新增的 .scripting 文件
10-
const diffOutput = execSync("git diff --name-status HEAD~1 HEAD", { encoding: "utf-8" });
11-
const addedFiles = diffOutput
12-
.split("\n")
13-
.filter(line => (line.startsWith("A") || line.startsWith("M")) && line.endsWith(".scripting"))
14-
.map(line => line.split("\t")[1]);
15-
16-
if (addedFiles.length === 0) {
17-
console.log("No new .scripting files added.");
18-
process.exit(0);
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
// 项目根目录
5+
const scriptingDir = path.resolve(__dirname, "../");
6+
const readmePath = path.resolve(scriptingDir, "README.md");
7+
8+
let collectedLinks = [];
9+
10+
// 遍历目录,收集 .scripting 文件
11+
function walkDir(dir, callback) {
12+
fs.readdirSync(dir).forEach((file) => {
13+
const filepath = path.join(dir, file);
14+
if (fs.statSync(filepath).isFile() && filepath.endsWith(".scripting")) {
15+
callback(filepath);
16+
}
17+
});
18+
}
19+
20+
// 生成 scripting.fun 链接
21+
function generateScriptLink(filename) {
22+
const baseUrl = "https://github.com/ScriptingApp/Community-Scripts/raw/refs/heads/main/";
23+
const fullUrl = baseUrl + encodeURIComponent(path.basename(filename));
24+
const name = path.basename(filename, ".scripting"); // 去掉后缀
25+
const link = `https://scripting.fun/import_scripts?urls=${encodeURIComponent(`[\"${fullUrl}\"]`)}`;
26+
return { name, link };
1927
}
2028

21-
// 生成分享链接
22-
const links = addedFiles.map(f => {
23-
const relative = encodeURIComponent(f);
24-
const rawUrl = `https://github.com/${repo}/raw/refs/heads/${branch}/${relative}`;
25-
const shareUrl = `https://scripting.fun/import_scripts?urls=%5B%22${encodeURIComponent(rawUrl)}%22%5D`;
26-
return `- [${path.basename(f)}](${shareUrl})`;
29+
// 收集所有链接
30+
walkDir(scriptingDir, (file) => {
31+
collectedLinks.push(generateScriptLink(file));
2732
});
2833

29-
let readme = fs.existsSync(readmePath) ? fs.readFileSync(readmePath, "utf-8") : "";
34+
// 写入 README.md
35+
let readmeContent = fs.existsSync(readmePath) ? fs.readFileSync(readmePath, "utf-8") : "";
36+
const startTag = "<!-- SCRIPTS_LINKS_START -->";
37+
const endTag = "<!-- SCRIPTS_LINKS_END -->";
38+
const linksMarkdown = collectedLinks.map(({ name, link }) => `- [${name}](${link})`).join("\n");
39+
const replacement = `${startTag}\n${linksMarkdown}\n${endTag}`;
3040

31-
const marker = "<!-- AUTO_LINKS -->";
32-
if (!readme.includes(marker)) {
33-
// 如果没有标记,追加到文件末尾
34-
readme += `\n\n${marker}\n## 分享链接\n\n${links.join("\n")}\n${marker}\n`;
41+
// 替换或追加
42+
if (readmeContent.includes(startTag)) {
43+
readmeContent = readmeContent.replace(new RegExp(`${startTag}[\\s\\S]*?${endTag}`), replacement);
3544
} else {
36-
// 在 marker 前插入新增的链接
37-
readme = readme.replace(marker, `${links.join("\n")}\n\n${marker}`);
45+
readmeContent += `\n\n${replacement}\n`;
3846
}
3947

40-
fs.writeFileSync(readmePath, readme);
48+
fs.writeFileSync(readmePath, readmeContent, "utf-8");
49+
console.log("README.md updated with latest links.");

0 commit comments

Comments
 (0)