mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2026-02-10 19:50:28 +08:00
fix: handle file copy errors gracefully in init
This commit is contained in:
parent
9d5d2bb73d
commit
3d9507b10c
@ -178,28 +178,23 @@ async function initFiles(): Promise<void> {
|
|||||||
const sourcePath = path.join(resourcesFilesDir(), file)
|
const sourcePath = path.join(resourcesFilesDir(), file)
|
||||||
if (!existsSync(sourcePath)) return
|
if (!existsSync(sourcePath)) return
|
||||||
|
|
||||||
const targets = [
|
const targets = [path.join(mihomoWorkDir(), file), path.join(mihomoTestDir(), file)]
|
||||||
path.join(mihomoWorkDir(), file),
|
|
||||||
path.join(mihomoTestDir(), file)
|
|
||||||
]
|
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
targets.map(async (targetPath) => {
|
targets.map(async (targetPath) => {
|
||||||
const shouldCopy = !existsSync(targetPath) || (await isSourceNewer(sourcePath, targetPath))
|
const shouldCopy = !existsSync(targetPath) || (await isSourceNewer(sourcePath, targetPath))
|
||||||
if (shouldCopy) {
|
if (!shouldCopy) return
|
||||||
// 先删除目标,force: true 会忽略 ENOENT,避免 cp 内部 unlink 的问题
|
|
||||||
try {
|
try {
|
||||||
await rm(targetPath, { recursive: true, force: true })
|
await cp(sourcePath, targetPath, { recursive: true, force: true })
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
// EPERM: 文件被占用,跳过本次复制
|
const code = (error as NodeJS.ErrnoException).code
|
||||||
if ((error as NodeJS.ErrnoException).code === 'EPERM') {
|
if (code === 'EPERM' || code === 'EBUSY') {
|
||||||
await initLogger.warn(`Skipping ${file}: file is in use`)
|
await initLogger.warn(`Skipping ${file}: file is in use`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
await cp(sourcePath, targetPath, { recursive: true })
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user