fix:修复报错

This commit is contained in:
Daniel
2026-03-08 21:52:29 +08:00
parent 2dd1117e51
commit b05ebb80fa
2 changed files with 109 additions and 1 deletions

View File

@@ -25,8 +25,35 @@ function copyDir(srcDir, destDir) {
}
}
/** 清空目录内容(不删目录本身),避免 EPERM如宝塔 wwwroot 下 dist 无法 rmSync */
function emptyDir(dir) {
if (!fs.existsSync(dir)) return;
for (const name of fs.readdirSync(dir)) {
const p = path.join(dir, name);
try {
if (fs.statSync(p).isDirectory()) {
fs.rmSync(p, { recursive: true });
} else {
fs.unlinkSync(p);
}
} catch (e) {
if (e.code !== 'EPERM' && e.code !== 'EACCES') throw e;
}
}
}
function main() {
if (fs.existsSync(DIST)) fs.rmSync(DIST, { recursive: true });
if (fs.existsSync(DIST)) {
try {
fs.rmSync(DIST, { recursive: true });
} catch (e) {
if (e.code === 'EPERM' || e.code === 'EACCES') {
emptyDir(DIST);
} else {
throw e;
}
}
}
fs.mkdirSync(DIST, { recursive: true });
copyFile(path.join(ROOT, 'index.html'), path.join(DIST, 'index.html'));