52 lines
1.5 KiB
JavaScript
52 lines
1.5 KiB
JavaScript
|
|
#!/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);
|
|||
|
|
});
|