-
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// 한국 표준시 (KST) 기준 날짜 (YYYY-MM-DD)
|
||||
function getKstDate(d = new Date()) {
|
||||
return d.toLocaleDateString('sv-SE', { timeZone: 'Asia/Seoul' });
|
||||
}
|
||||
|
||||
// Write-if-changed: 내용이 동일하면 파일 수정을 건너뛰어 mtime 유지
|
||||
function writeIfChanged(filePath, newContent) {
|
||||
if (fs.existsSync(filePath)) {
|
||||
const existing = fs.readFileSync(filePath, 'utf8');
|
||||
if (existing === newContent) {
|
||||
console.log(` [Skip] No change in ${path.basename(filePath)} (mtime preserved)`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(filePath, newContent, 'utf8');
|
||||
console.log(` [Write] Updated ${path.basename(filePath)} with new content`);
|
||||
return true;
|
||||
}
|
||||
|
||||
const projects = ['seoul', 'gyeonggi', 'incheon', 'daegu', 'busan', 'gwangju', 'ulsan', 'site1'];
|
||||
|
||||
// 오늘 KST 날짜
|
||||
const todayKst = getKstDate();
|
||||
|
||||
projects.forEach(p => {
|
||||
const sitemapPath = path.join('c:/Users/kkgu8/OneDrive/Desktop/kyounggu/educo/example/web', p, 'public', 'sitemap.xml');
|
||||
if (!fs.existsSync(sitemapPath)) return;
|
||||
|
||||
const xmlContent = fs.readFileSync(sitemapPath, 'utf8');
|
||||
|
||||
// <url> 블록 파싱 및 <lastmod> 주입 (기존 lastmod가 없으면 추가, 있으면 유지)
|
||||
// url 내부에 lastmod가 없을 경우 </url> 바로 앞에 <lastmod>YYYY-MM-DD</lastmod> 추가
|
||||
const updatedXml = xmlContent.replace(/<url>([\s\S]*?)<\/url>/g, (match, inner) => {
|
||||
if (inner.includes('<lastmod>')) {
|
||||
return match; // 이미 lastmod가 있으면 기존 유지
|
||||
}
|
||||
// changefreq나 priority 앞에 lastmod 삽입
|
||||
const lastmodTag = `\n <lastmod>${todayKst}</lastmod>`;
|
||||
if (inner.includes('<changefreq>')) {
|
||||
return `<url>${inner.replace('<changefreq>', `${lastmodTag}\n <changefreq>`)}</url>`;
|
||||
}
|
||||
return `<url>${inner}${lastmodTag}\n </url>`;
|
||||
});
|
||||
|
||||
console.log(`Checking sitemap for ${p}...`);
|
||||
writeIfChanged(sitemapPath, updatedXml);
|
||||
});
|
||||
Reference in New Issue
Block a user