import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { SITE_ORIGIN, DEFAULT_PATH } from '../src/config.js';
import { REGIONS_DATA } from '../src/data/regions.js';
import { TARGETS, SUBJECTS } from '../src/data/subjects.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const publicDir = path.resolve(__dirname, '../public');
function generateUrlXml(urls, today) {
return `
${urls
.map(
(u) => `
${u.loc}
${today}
${u.changefreq}
${u.priority}
`
)
.join('\n')}
`;
}
function generate() {
const today = new Date().toISOString().split('T')[0];
const sitemapFiles = [];
let totalIndexedUrls = 0;
// 1. 공통 및 메인 대표 페이지 사이트맵 (sitemap-main.xml - 지역 사이트맵과 중복되지 않는 고유 URL만 수록)
const mainUrls = [
{ loc: `${SITE_ORIGIN}`, priority: '1.0', changefreq: 'daily' },
{ loc: `${SITE_ORIGIN}/regions`, priority: '0.9', changefreq: 'daily' }
];
const mainXml = generateUrlXml(mainUrls, today);
fs.writeFileSync(path.join(publicDir, 'sitemap-main.xml'), mainXml, 'utf-8');
sitemapFiles.push('sitemap-main.xml');
totalIndexedUrls += mainUrls.length;
// 2. 17개 시·도별 개별 사이트맵 (sitemap-${sidoSlug}.xml)
REGIONS_DATA.forEach((sido) => {
const sidoUrls = [];
sido.sigunguList.forEach((sigungu) => {
sigungu.dongList.forEach((dong) => {
TARGETS.forEach((target) => {
SUBJECTS.forEach((sub) => {
sidoUrls.push({
loc: `${SITE_ORIGIN}/${sido.sidoSlug}/${sigungu.sigunguSlug}/${dong.dongSlug}/${target.slug}/${sub.slug}`,
priority: '0.8',
changefreq: 'weekly'
});
});
});
});
});
const fileName = `sitemap-${sido.sidoSlug}.xml`;
const xml = generateUrlXml(sidoUrls, today);
fs.writeFileSync(path.join(publicDir, fileName), xml, 'utf-8');
sitemapFiles.push(fileName);
totalIndexedUrls += sidoUrls.length;
});
// 3. 루트 사이트맵 인덱스 (sitemap.xml - 구글/네이버 단일 제출용 표준)
const indexXml = `
${sitemapFiles
.map(
(file) => `
${SITE_ORIGIN}/${file}
${today}
`
)
.join('\n')}
`;
fs.writeFileSync(path.join(publicDir, 'sitemap.xml'), indexXml, 'utf-8');
// 4. robots.txt (루트 사이트맵 인덱스 및 시·도별 분할 사이트맵 전체 명시)
const robots = [
'User-agent: *',
'Allow: /',
'Disallow: /admin',
'Disallow: /regions/admin',
'',
`Sitemap: ${SITE_ORIGIN}/sitemap.xml`,
...sitemapFiles.map((file) => `Sitemap: ${SITE_ORIGIN}/${file}`),
''
].join('\n');
fs.writeFileSync(path.join(publicDir, 'robots.txt'), robots, 'utf-8');
console.log(`[OK] 사이트맵 인덱스(sitemap.xml) 및 ${sitemapFiles.length}개 시·도 분할 사이트맵 생성 완료!`);
console.log(`[OK] 총 인덱싱 등록 URL: ${totalIndexedUrls.toLocaleString()}개 (${SITE_ORIGIN})`);
}
generate();