74 lines
2.2 KiB
JavaScript
74 lines
2.2 KiB
JavaScript
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const oldPath = path.resolve(__dirname, '..', 'out', 'win-unpacked')
|
||
const newPath = path.resolve(__dirname, '..', 'out', 'CN_Tool')
|
||
const retryableErrorCodes = new Set(['EPERM', 'EBUSY', 'ENOTEMPTY', 'UNKNOWN'])
|
||
const maxAttempts = 10
|
||
const retryDelayMs = 1500
|
||
|
||
function sleep(ms) {
|
||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||
}
|
||
|
||
function isRetryable(error) {
|
||
return retryableErrorCodes.has(error?.code)
|
||
}
|
||
|
||
async function removeIfExists(targetPath) {
|
||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||
if (!fs.existsSync(targetPath)) {
|
||
return
|
||
}
|
||
|
||
try {
|
||
fs.rmSync(targetPath, { recursive: true, force: true })
|
||
return
|
||
} catch (error) {
|
||
if (!isRetryable(error) || attempt === maxAttempts) {
|
||
throw error
|
||
}
|
||
|
||
console.warn(
|
||
`[rename-output] 目标目录正在被占用,${retryDelayMs}ms 后重试删除(${attempt}/${maxAttempts}):${targetPath}`
|
||
)
|
||
await sleep(retryDelayMs)
|
||
}
|
||
}
|
||
}
|
||
|
||
async function renameWithRetry(sourcePath, targetPath) {
|
||
// Windows 打包完成后,杀软、索引或资源管理器可能短暂占用目录,重试可避免误判为打包失败。
|
||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||
try {
|
||
fs.renameSync(sourcePath, targetPath)
|
||
console.log('[rename-output] Renamed to CN_Tool')
|
||
return
|
||
} catch (error) {
|
||
if (!isRetryable(error) || attempt === maxAttempts) {
|
||
throw error
|
||
}
|
||
|
||
console.warn(
|
||
`[rename-output] 输出目录暂时无法重命名,${retryDelayMs}ms 后重试(${attempt}/${maxAttempts}):${error.code}`
|
||
)
|
||
await sleep(retryDelayMs)
|
||
}
|
||
}
|
||
}
|
||
|
||
async function main() {
|
||
if (!fs.existsSync(oldPath)) {
|
||
console.warn(`[rename-output] 未找到输出目录,跳过重命名:${oldPath}`)
|
||
return
|
||
}
|
||
|
||
await removeIfExists(newPath)
|
||
await renameWithRetry(oldPath, newPath)
|
||
}
|
||
|
||
main().catch((error) => {
|
||
console.error(`[rename-output] 重命名失败:${error.message}`)
|
||
process.exitCode = 1
|
||
})
|