Files
soul-yongping/scripts/clean-standalone.js

52 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* 构建前清理 .next/standalone避免 Next.js build 时 EBUSY目录被占用
* 若曾运行 pnpm start请先 Ctrl+C 停掉再 build
*/
const fs = require('fs');
const path = require('path');
const rootDir = path.join(__dirname, '..');
const standaloneDir = path.join(rootDir, '.next', 'standalone');
const RETRIES = 5;
const DELAY_MS = 2000;
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
async function main() {
if (!fs.existsSync(standaloneDir)) {
return;
}
for (let attempt = 1; attempt <= RETRIES; attempt++) {
try {
fs.rmSync(standaloneDir, { recursive: true, force: true, maxRetries: 3 });
console.log('[clean-standalone] 已删除 .next/standalone');
return;
} catch (e) {
if (e.code === 'EBUSY' || e.code === 'EPERM' || e.code === 'ENOTEMPTY') {
if (attempt < RETRIES) {
console.warn('[clean-standalone] 目录被占用,%ds 后重试 (%d/%d)...', DELAY_MS / 1000, attempt, RETRIES);
await sleep(DELAY_MS);
} else {
console.error('[clean-standalone] 无法删除 .next/standalone目录被占用');
console.error(' 请先关闭pnpm start、或占用该目录的其它程序如资源管理器、杀毒');
console.error(' 然后重新执行pnpm build');
process.exit(1);
}
} else {
throw e;
}
}
}
}
main().catch((e) => {
console.error(e);
process.exit(1);
});