Files
teacherpick/scripts/post-build.js
T
2026-11-10 10:12:09 +09:00

76 lines
3.1 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.
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
const dist = path.resolve('dist');
const adminDir = path.join(dist, 'admin');
const regionsDir = path.join(dist, 'regions');
if (!fs.existsSync(adminDir)) fs.mkdirSync(adminDir, { recursive: true });
if (!fs.existsSync(regionsDir)) fs.mkdirSync(regionsDir, { recursive: true });
if (!fs.existsSync(path.join(adminDir, 'index.html'))) {
fs.copyFileSync(path.join(dist, 'index.html'), path.join(adminDir, 'index.html'));
}
if (!fs.existsSync(path.join(regionsDir, 'index.html'))) {
fs.copyFileSync(path.join(dist, 'index.html'), path.join(regionsDir, 'index.html'));
}
if (!fs.existsSync(path.join(dist, '404.html'))) {
fs.copyFileSync(path.join(dist, 'index.html'), path.join(dist, '404.html'));
}
if (fs.existsSync('public/sitemap.xml')) {
fs.copyFileSync('public/sitemap.xml', path.join(dist, 'sitemap.xml'));
}
if (fs.existsSync('public/robots.txt')) {
fs.copyFileSync('public/robots.txt', path.join(dist, 'robots.txt'));
}
// Cloudflare Pages 표준 SPA/SSR 폴백 라우팅 (_redirects): 모든 지역 경로 200 OK 응답 보장
fs.writeFileSync(path.join(dist, '_redirects'), '/* /index.html 200\n', 'utf-8');
// ---------------------------------------------------------------------------
// Cloudflare 대시보드 무한 업로드(Hanging) 원인 파일 완전 제거:
// 1. 0바이트 파일 (.gitkeep 등)
// 2. 숨김 파일 (.htaccess, .gitkeep, .DS_Store 등 점(.)으로 시작하는 파일)
// 3. 대시보드 드래그앤드롭에서 파서 멈춤을 유발하는 _worker.js, _routes.json, _template.html
// 4. 빈 폴더 (video 등)
// ---------------------------------------------------------------------------
const blockedRootFiles = ['_worker.js', '_routes.json', '_template.html', '.htaccess'];
for (const name of blockedRootFiles) {
const p = path.join(dist, name);
if (fs.existsSync(p)) {
try { fs.unlinkSync(p); } catch {}
}
}
function cleanProblematicFiles(dirPath) {
if (!fs.existsSync(dirPath)) return;
for (const entry of fs.readdirSync(dirPath, { withFileTypes: true })) {
const fullPath = path.join(dirPath, entry.name);
if (entry.isDirectory()) {
cleanProblematicFiles(fullPath);
// 하위 정리 후 빈 폴더면 삭제 (Cloudflare 빈 폴더 업로드 멈춤 방지)
if (fs.existsSync(fullPath) && fs.readdirSync(fullPath).length === 0) {
try { fs.rmdirSync(fullPath); } catch {}
}
} else {
const size = fs.statSync(fullPath).size;
if (entry.name.startsWith('.') || size === 0) {
try { fs.unlinkSync(fullPath); } catch {}
}
}
}
}
cleanProblematicFiles(dist);
console.log('✅ [Post-Build] 무한 업로드 유발 파일(0바이트 .gitkeep, .htaccess, _worker.js) 제거 및 _redirects 설정 완료!');
try {
console.log('📦 [Zip] Cloudflare 직접 업로드용 dist.zip 자동 압축 중...');
execSync('tar -acf dist.zip -C dist .', { stdio: 'inherit' });
console.log('🎉 [Success] 최신 배포용 dist.zip (루트 압축) 생성이 완료되었습니다!');
} catch (e) {
console.log('ℹ️ tar 압축 대신 PowerShell 수동 압축 가능:', e.message);
}