227 lines
8.0 KiB
JavaScript
227 lines
8.0 KiB
JavaScript
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
import { resolve } from 'node:path';
|
|
|
|
const DATA_DIR = resolve(process.cwd(), 'public/data');
|
|
mkdirSync(DATA_DIR, { recursive: true });
|
|
|
|
const SKIP_SECTIONS = /^(下载|download|downloads|已知限制|known limitations|许可证|license|构建与许可证)/i;
|
|
|
|
function normalizeTitle(raw) {
|
|
return String(raw || '')
|
|
.replace(/<[^>]+>/g, '')
|
|
.replace(/^#+\s*/, '')
|
|
.trim();
|
|
}
|
|
|
|
function parseSections(markdown) {
|
|
const lines = String(markdown || '').replace(/\r\n/g, '\n').split('\n');
|
|
const sections = [];
|
|
let current = null;
|
|
let skipping = false;
|
|
|
|
for (const rawLine of lines) {
|
|
const line = rawLine.trim();
|
|
if (!line) continue;
|
|
|
|
const htmlHeading = line.match(/^<h[2-4][^>]*>(.*?)<\/h[2-4]>$/i);
|
|
const mdHeading = !htmlHeading ? line.match(/^#{2,4}\s+(.+)$/) : null;
|
|
|
|
if (htmlHeading || mdHeading) {
|
|
const title = normalizeTitle(htmlHeading ? htmlHeading[1] : mdHeading[1]);
|
|
if (!title || SKIP_SECTIONS.test(title)) {
|
|
skipping = true;
|
|
current = null;
|
|
} else {
|
|
skipping = false;
|
|
current = { title, items: [] };
|
|
sections.push(current);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (skipping || /^\[中文\]\(#cn-/i.test(line) || line.startsWith('>')) continue;
|
|
|
|
const bullet = line.match(/^[-*]\s+(.+)$/);
|
|
if (bullet && bullet[1].trim()) {
|
|
if (!current) {
|
|
current = { title: '', items: [] };
|
|
sections.push(current);
|
|
}
|
|
current.items.push(bullet[1].trim());
|
|
}
|
|
}
|
|
|
|
return sections.filter((s) => s.items.length > 0);
|
|
}
|
|
|
|
function parseBilingualBody(body) {
|
|
const text = String(body || '').replace(/\r\n/g, '\n');
|
|
if (!text.trim()) return { 'zh-CN': [], 'en-US': [] };
|
|
|
|
const cn = text.match(/<h[2-4][^>]*\bid=["']cn-[^"']*["'][^>]*>/i);
|
|
const en = text.match(/<h[2-4][^>]*\bid=["']en-[^"']*["'][^>]*>/i);
|
|
|
|
if (cn && en && cn.index !== undefined && en.index !== undefined) {
|
|
const cnChunk = cn.index < en.index ? text.slice(cn.index, en.index) : text.slice(cn.index);
|
|
const enChunk = en.index > cn.index ? text.slice(en.index) : text.slice(en.index, cn.index);
|
|
const zhSections = parseSections(cnChunk);
|
|
const enSections = parseSections(enChunk);
|
|
return {
|
|
'zh-CN': zhSections.length ? zhSections : enSections,
|
|
'en-US': enSections.length ? enSections : zhSections,
|
|
};
|
|
}
|
|
|
|
const single = parseSections(text);
|
|
return { 'zh-CN': single, 'en-US': single };
|
|
}
|
|
|
|
function classifyChannel(tag, prerelease) {
|
|
const lower = tag.toLowerCase();
|
|
if (lower === 'dev' || lower.includes('-dev')) return 'dev';
|
|
if (prerelease || lower.includes('alpha') || lower.includes('beta') || lower.includes('rc')) return 'alpha';
|
|
return 'stable';
|
|
}
|
|
|
|
function isClientTag(tag) {
|
|
if (!tag || /^runtime-/i.test(tag)) return false;
|
|
return /^v\d+\.\d+/i.test(tag) || /^dev$/i.test(tag) || /^alpha/i.test(tag);
|
|
}
|
|
|
|
async function fetchJson(url) {
|
|
const res = await fetch(url, {
|
|
headers: {
|
|
Accept: 'application/vnd.github+json',
|
|
'User-Agent': 'Reims-Go-Landing-Sync/2.0',
|
|
},
|
|
});
|
|
if (!res.ok) throw new Error(`${res.status} ${url}`);
|
|
return res.json();
|
|
}
|
|
|
|
async function fetchText(url) {
|
|
const res = await fetch(url, {
|
|
headers: { 'User-Agent': 'Reims-Go-Landing-Sync/2.0' },
|
|
});
|
|
if (!res.ok) throw new Error(`${res.status} ${url}`);
|
|
return res.text();
|
|
}
|
|
|
|
async function syncReleases() {
|
|
try {
|
|
const [repo, rawReleases] = await Promise.all([
|
|
fetchJson('https://api.github.com/repos/Nwflower/Reims-Go'),
|
|
fetchJson('https://api.github.com/repos/Nwflower/Reims-Go/releases?per_page=25'),
|
|
]);
|
|
|
|
const list = Array.isArray(rawReleases) ? rawReleases : [];
|
|
const entries = [];
|
|
let latestStableRaw = null;
|
|
|
|
for (const rel of list) {
|
|
if (!rel || rel.draft) continue;
|
|
const tag = String(rel.tag_name || '').trim();
|
|
if (!isClientTag(tag)) continue;
|
|
|
|
const assets = Array.isArray(rel.assets) ? rel.assets : [];
|
|
const exeAsset = assets.find((a) => /windows-amd64\.exe$/i.test(a.name));
|
|
const zipAsset = assets.find((a) => /windows-amd64\.zip$/i.test(a.name));
|
|
const shaAsset = assets.find((a) => /^SHA256SUMS\.txt$/i.test(a.name));
|
|
const prerelease = Boolean(rel.prerelease);
|
|
const channel = classifyChannel(tag, prerelease);
|
|
const published = rel.published_at || rel.created_at || '';
|
|
|
|
if (channel === 'stable' && !latestStableRaw) {
|
|
latestStableRaw = { rel, exeAsset, zipAsset, shaAsset };
|
|
}
|
|
|
|
entries.push({
|
|
tag,
|
|
name: String(rel.name || `Reims-Go ${tag}`).trim(),
|
|
published_at: published,
|
|
date: published ? published.slice(0, 10) : '',
|
|
html_url: rel.html_url || `https://github.com/Nwflower/Reims-Go/releases/tag/${encodeURIComponent(tag)}`,
|
|
prerelease,
|
|
channel,
|
|
exe: exeAsset ? { name: exeAsset.name, size: exeAsset.size, url: exeAsset.browser_download_url } : null,
|
|
zip: zipAsset ? { name: zipAsset.name, size: zipAsset.size, url: zipAsset.browser_download_url } : null,
|
|
sha256_url: shaAsset?.browser_download_url || null,
|
|
sections: parseBilingualBody(rel.body || ''),
|
|
});
|
|
}
|
|
|
|
entries.sort((a, b) => {
|
|
if (a.channel === 'stable' && b.channel !== 'stable') return -1;
|
|
if (a.channel !== 'stable' && b.channel === 'stable') return 1;
|
|
return (b.published_at || '').localeCompare(a.published_at || '');
|
|
});
|
|
|
|
const top = latestStableRaw?.rel;
|
|
const exeAsset = latestStableRaw?.exeAsset;
|
|
const zipAsset = latestStableRaw?.zipAsset;
|
|
const shaAsset = latestStableRaw?.shaAsset;
|
|
|
|
let sha256Text = '';
|
|
const digestLines = [exeAsset, zipAsset]
|
|
.filter((a) => a?.name && a?.digest && /^sha256:[a-f0-9]{64}$/i.test(a.digest))
|
|
.map((a) => `${a.digest.replace(/^sha256:/i, '')} ${a.name}`);
|
|
if (digestLines.length > 0) {
|
|
sha256Text = digestLines.join('\n');
|
|
} else if (shaAsset?.browser_download_url) {
|
|
try {
|
|
sha256Text = await fetchText(shaAsset.browser_download_url);
|
|
} catch {
|
|
sha256Text = '';
|
|
}
|
|
}
|
|
|
|
const nowIso = new Date().toISOString();
|
|
const latest = {
|
|
synced_at: nowIso,
|
|
stars: typeof repo.stargazers_count === 'number' ? repo.stargazers_count : 1600,
|
|
version: top?.tag_name || entries[0]?.tag || 'v0.2.12',
|
|
published_at: top?.published_at || nowIso,
|
|
html_url: top?.html_url || 'https://github.com/Nwflower/Reims-Go/releases/latest',
|
|
exe: exeAsset ? { name: exeAsset.name, size: exeAsset.size, url: exeAsset.browser_download_url } : null,
|
|
zip: zipAsset ? { name: zipAsset.name, size: zipAsset.size, url: zipAsset.browser_download_url } : null,
|
|
sha256_url: shaAsset?.browser_download_url || null,
|
|
sha256_sums: sha256Text,
|
|
};
|
|
|
|
const bundle = {
|
|
source: 'static-fallback',
|
|
synced_at: nowIso,
|
|
latest,
|
|
entries,
|
|
};
|
|
|
|
writeFileSync(resolve(DATA_DIR, 'releases.json'), JSON.stringify(latest, null, 2) + '\n', 'utf8');
|
|
writeFileSync(resolve(DATA_DIR, 'releases-bundle.json'), JSON.stringify(bundle, null, 2) + '\n', 'utf8');
|
|
console.log(`[sync] releases.json & releases-bundle.json updated: ${latest.version} (${latest.stars} stars, ${entries.length} releases)`);
|
|
} catch (err) {
|
|
console.warn('[sync] skipped releases fetch, keeping local fallback:', err.message);
|
|
}
|
|
}
|
|
|
|
async function syncPlugins() {
|
|
const urls = [
|
|
'https://raw.githubusercontent.com/reims-go/plugins/dist/index.json',
|
|
'https://cdn.jsdelivr.net/gh/reims-go/plugins@dist/index.json',
|
|
];
|
|
for (const url of urls) {
|
|
try {
|
|
const idx = await fetchJson(url);
|
|
if (idx && Array.isArray(idx.plugins)) {
|
|
writeFileSync(resolve(DATA_DIR, 'plugins.json'), JSON.stringify(idx, null, 2) + '\n', 'utf8');
|
|
console.log(`[sync] plugins.json updated (${idx.plugins.length} plugins) from ${url}`);
|
|
return;
|
|
}
|
|
} catch (err) {
|
|
console.warn(`[sync] failed ${url}:`, err.message);
|
|
}
|
|
}
|
|
console.warn('[sync] keeping local fallback plugins.json');
|
|
}
|
|
|
|
await Promise.all([syncReleases(), syncPlugins()]);
|