fix: sync updated geo files to profile work dir in lite mode

This commit is contained in:
xmk23333 2026-01-15 18:05:38 +08:00
parent 45fd8e6870
commit ccaabb7b1a

View File

@ -1,4 +1,4 @@
import { copyFile, mkdir, writeFile, readFile } from 'fs/promises' import { copyFile, mkdir, writeFile, readFile, stat } from 'fs/promises'
import vm from 'vm' import vm from 'vm'
import { existsSync, writeFileSync } from 'fs' import { existsSync, writeFileSync } from 'fs'
import path from 'path' import path from 'path'
@ -160,10 +160,23 @@ async function prepareProfileWorkDir(current: string | undefined): Promise<void>
if (!existsSync(mihomoProfileWorkDir(current))) { if (!existsSync(mihomoProfileWorkDir(current))) {
await mkdir(mihomoProfileWorkDir(current), { recursive: true }) await mkdir(mihomoProfileWorkDir(current), { recursive: true })
} }
const isSourceNewer = async (sourcePath: string, targetPath: string): Promise<boolean> => {
try {
const [sourceStats, targetStats] = await Promise.all([stat(sourcePath), stat(targetPath)])
return sourceStats.mtime > targetStats.mtime
} catch {
return true
}
}
const copy = async (file: string): Promise<void> => { const copy = async (file: string): Promise<void> => {
const targetPath = path.join(mihomoProfileWorkDir(current), file) const targetPath = path.join(mihomoProfileWorkDir(current), file)
const sourcePath = path.join(mihomoWorkDir(), file) const sourcePath = path.join(mihomoWorkDir(), file)
if (!existsSync(targetPath) && existsSync(sourcePath)) { if (!existsSync(sourcePath)) return
// 复制条件:目标不存在 或 源文件更新
const shouldCopy = !existsSync(targetPath) || (await isSourceNewer(sourcePath, targetPath))
if (shouldCopy) {
await copyFile(sourcePath, targetPath) await copyFile(sourcePath, targetPath)
} }
} }