初始化
This commit is contained in:
73
build/rename-output.js
Normal file
73
build/rename-output.js
Normal file
@@ -0,0 +1,73 @@
|
||||
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
|
||||
})
|
||||
Reference in New Issue
Block a user