chore: trim excessive comments

This commit is contained in:
wonfen
2026-08-19 07:27:43 +08:00
parent 42b91b6294
commit 807f9a206d
103 changed files with 293 additions and 2517 deletions
+8 -47
View File
@@ -13,14 +13,7 @@ import { extract } from 'tar'
import { resolveServiceRelease } from './service-release.mjs'
import { log_debug, log_error, log_info, log_success } from './utils.mjs'
/**
* Prebuild script with optimization features:
* 1. Skip downloading mihomo core if it already exists (unless --force is used)
* 2. Cache version information for 1 hour to avoid repeated version checks
* 3. Use file hash to detect changes and skip unnecessary chmod/copy operations
* 4. Use --force or -f flag to force re-download and update all resources
*
*/
/** Prepares platform resources, caching versions and unchanged files unless `--force` is used. */
const cwd = process.cwd()
const TEMP_DIR = path.join(cwd, 'node_modules/.verge')
@@ -76,9 +69,7 @@ const SIDECAR_DIR = path.join(cwd, 'src-tauri', 'sidecar')
// Linux service binaries are bundled as externalBin sidecars (see tauri.linux.conf.json)
const SERVICE_DIR = platform === 'linux' ? SIDECAR_DIR : RESOURCES_DIR
// =======================
// Version Cache
// =======================
// Version cache
async function loadVersionCache() {
try {
if (fs.existsSync(VERSION_CACHE_FILE)) {
@@ -114,9 +105,7 @@ async function setCachedVersion(key, version) {
await saveVersionCache(cache)
}
// =======================
// Hash Cache & File Hash
// =======================
// File hash cache
async function calculateFileHash(filePath) {
try {
const fileBuffer = await fsp.readFile(filePath)
@@ -170,9 +159,7 @@ async function updateHashCache(targetPath) {
}
}
// =======================
// Meta maps (stable & alpha)
// =======================
// Mihomo release maps
const META_ALPHA_VERSION_URL =
'https://github.com/MetaCubeX/mihomo/releases/download/Prerelease-Alpha/version.txt'
const META_ALPHA_URL_PREFIX = `https://github.com/MetaCubeX/mihomo/releases/download/Prerelease-Alpha`
@@ -211,9 +198,7 @@ const META_MAP = {
'linux-loong64': 'mihomo-linux-loong64',
}
// =======================
// Fetch latest versions
// =======================
// Release discovery
async function getLatestAlphaVersion() {
if (!FORCE) {
const cached = await getCachedVersion('META_ALPHA_VERSION')
@@ -280,9 +265,6 @@ async function getLatestReleaseVersion() {
}
}
// =======================
// Validate availability
// =======================
if (!META_MAP[`${platform}-${arch}`]) {
throw new Error(`clash meta unsupported platform "${platform}-${arch}"`)
}
@@ -290,9 +272,6 @@ if (!META_ALPHA_MAP[`${platform}-${arch}`]) {
throw new Error(`clash meta alpha unsupported platform "${platform}-${arch}"`)
}
// =======================
// Build meta objects
// =======================
function clashMetaAlpha() {
const name = META_ALPHA_MAP[`${platform}-${arch}`]
const isWin = platform === 'win32'
@@ -319,9 +298,6 @@ function clashMeta() {
}
}
// =======================
// download helper (增强:status + magic bytes)
// =======================
async function downloadFile(url, outPath) {
const options = {}
const httpProxy =
@@ -338,7 +314,6 @@ async function downloadFile(url, outPath) {
})
if (!response.ok) {
const body = await response.text().catch(() => '')
// 将 body 写到文件以便排查(可通过临时目录查看)
await fsp.mkdir(path.dirname(outPath), { recursive: true })
await fsp.writeFile(outPath, body)
throw new Error(`Failed to download ${url}: status ${response.status}`)
@@ -347,7 +322,6 @@ async function downloadFile(url, outPath) {
const buf = Buffer.from(await response.arrayBuffer())
await fsp.mkdir(path.dirname(outPath), { recursive: true })
// 简单 magic 字节检查
if (url.endsWith('.gz') || url.endsWith('.tgz')) {
if (!(buf[0] === 0x1f && buf[1] === 0x8b)) {
await fsp.writeFile(outPath, buf)
@@ -368,9 +342,6 @@ async function downloadFile(url, outPath) {
log_success(`download finished: ${url}`)
}
// =======================
// resolveSidecar (支持 zip / tgz / gz)
// =======================
async function resolveSidecar(binInfo) {
const { name, targetFile, zipFile, exeFile, downloadURL } = binInfo
const sidecarPath = path.join(SIDECAR_DIR, targetFile)
@@ -397,11 +368,9 @@ async function resolveSidecar(binInfo) {
log_debug(`"${name}" entry: ${entry.entryName}`)
})
zip.extractAllTo(tempDir, true)
// 尝试按 exeFile 重命名,否则找第一个可执行文件
if (fs.existsSync(tempExe)) {
await fsp.rename(tempExe, sidecarPath)
} else {
// 搜索候选
const files = await fsp.readdir(tempDir)
const candidate = files.find(
(f) =>
@@ -419,7 +388,6 @@ async function resolveSidecar(binInfo) {
await extract({ cwd: tempDir, file: tempZip })
const files = await fsp.readdir(tempDir)
log_debug(`"${name}" extracted files:`, files)
// 优先寻找给定 exeFile 或已知前缀
let extracted = files.find(
(f) =>
f === path.basename(exeFile) ||
@@ -432,7 +400,6 @@ async function resolveSidecar(binInfo) {
await fsp.chmod(sidecarPath, 0o755)
log_success(`tgz processed: "${name}"`)
} else {
// .gz
const readStream = fs.createReadStream(tempZip)
const writeStream = fs.createWriteStream(sidecarPath)
await new Promise((resolve, reject) => {
@@ -496,7 +463,7 @@ async function resolveResource(binInfo) {
log_success(`${file} finished`)
}
// SimpleSC.dll (win plugin)
// Windows NSIS plugin
const resolvePlugin = async () => {
const url =
'https://nsis.sourceforge.io/mediawiki/images/e/ef/NSIS_Simple_Service_Plugin_Unicode_1.30.zip'
@@ -524,7 +491,6 @@ const resolvePlugin = async () => {
await fsp.cp(tempDll, pluginPath, { recursive: true, force: true })
log_success(`unzip finished: "SimpleSC"`)
} else {
// 如果 dll 名称不同,尝试找到 dll
const files = await fsp.readdir(tempDir)
const dll = files.find((f) => f.toLowerCase().endsWith('.dll'))
if (dll) {
@@ -542,7 +508,7 @@ const resolvePlugin = async () => {
}
}
// service chmod (保留并使用 glob)
// Service executable permissions
const resolveServicePermission = async () => {
const serviceExecutables = [
'clash-verge-service*',
@@ -578,9 +544,7 @@ const resolveServicePermission = async () => {
}
}
// =======================
// Other resource resolvers (service, mmdb, geosite, geoip, enableLoopback)
// =======================
// Other resources
const SERVICE_BINARIES = [
'clash-verge-service',
'clash-verge-service-install',
@@ -698,9 +662,6 @@ const resolveUnSetDnsScript = () =>
localPath: path.join(cwd, 'scripts/unset_dns.sh'),
})
// =======================
// Tasks
// =======================
const tasks = [
{
name: 'verge-mihomo-alpha',