Skip to content

Commit d4ca4b3

Browse files
committed
feat: add script to update sitemap with git modification dates
1 parent 0e43440 commit d4ca4b3

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"changeset:version": "cp CHANGELOG.md src/CHANGELOG.md && changeset version && cp -vf src/CHANGELOG.md .",
2424
"knip": "knip --include files",
2525
"update-contributors": "node scripts/update-contributors.js",
26+
"update-sitemap": "node scripts/update-sitemap-dates.js",
2627
"evals": "dotenvx run -f packages/evals/.env.development packages/evals/.env.local -- docker compose -f packages/evals/docker-compose.yml --profile server --profile runner up --build --scale runner=0",
2728
"link-workspace-packages": "node scripts/link-packages.js",
2829
"unlink-workspace-packages": "node scripts/link-packages.js --unlink"

scripts/update-sitemap-dates.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require("fs")
4+
const path = require("path")
5+
const { execSync } = require("child_process")
6+
7+
/**
8+
* Script to update sitemap.ts with actual git modification dates
9+
* Run this script whenever you want to update the sitemap with current git history
10+
*/
11+
12+
const SITEMAP_PATH = path.join(__dirname, "../apps/web-roo-code/src/app/sitemap.ts")
13+
14+
// Map of sitemap URLs to their corresponding page files
15+
const URL_TO_FILE_MAP = {
16+
"/": "apps/web-roo-code/src/app/page.tsx",
17+
"/enterprise": "apps/web-roo-code/src/app/enterprise/page.tsx",
18+
"/evals": "apps/web-roo-code/src/app/evals/page.tsx",
19+
"/privacy": "apps/web-roo-code/src/app/privacy/page.tsx",
20+
"/terms": "apps/web-roo-code/src/app/terms/page.tsx",
21+
}
22+
23+
/**
24+
* Get the last modification date of a file from git history
25+
* @param {string} filePath - Path to the file relative to repo root
26+
* @returns {string} ISO date string
27+
*/
28+
function getLastModifiedDate(filePath) {
29+
try {
30+
// Get the last commit date for the file
31+
const gitCommand = `git log -1 --format="%ai" -- "${filePath}"`
32+
const result = execSync(gitCommand, {
33+
cwd: path.join(__dirname, "../"), // Go to repo root
34+
encoding: "utf8",
35+
}).trim()
36+
37+
if (!result) {
38+
console.warn(`No git history found for ${filePath}, using current date`)
39+
return new Date().toISOString()
40+
}
41+
42+
// Convert git date to ISO string
43+
return new Date(result).toISOString()
44+
} catch (error) {
45+
console.error(`Error getting git date for ${filePath}:`, error.message)
46+
return new Date().toISOString()
47+
}
48+
}
49+
50+
/**
51+
* Update the sitemap.ts file with new dates
52+
*/
53+
function updateSitemap() {
54+
console.log("🔍 Reading current sitemap...")
55+
56+
// Read the current sitemap file
57+
const sitemapContent = fs.readFileSync(SITEMAP_PATH, "utf8")
58+
59+
console.log("📅 Getting git modification dates...")
60+
61+
// Get modification dates for each URL
62+
const urlDates = {}
63+
for (const [url, filePath] of Object.entries(URL_TO_FILE_MAP)) {
64+
const lastModified = getLastModifiedDate(filePath)
65+
urlDates[url] = lastModified
66+
console.log(` ${url}: ${lastModified}`)
67+
}
68+
69+
console.log("✏️ Updating sitemap content...")
70+
71+
// Update the sitemap content
72+
let updatedContent = sitemapContent
73+
74+
// Replace each lastModified: new Date() with the actual git date
75+
for (const [url, isoDate] of Object.entries(urlDates)) {
76+
// Create a regex to match the specific URL entry and its lastModified line
77+
const urlPattern = new RegExp(
78+
`(\\{[^}]*url:\\s*\`\\$\\{baseUrl\\}${url.replace("/", "\\/")}\`[^}]*lastModified:\\s*)new Date\\([^)]*\\)`,
79+
"g",
80+
)
81+
82+
updatedContent = updatedContent.replace(urlPattern, `$1new Date("${isoDate}")`)
83+
}
84+
85+
// Write the updated content back to the file
86+
fs.writeFileSync(SITEMAP_PATH, updatedContent, "utf8")
87+
88+
console.log("✅ Sitemap updated successfully!")
89+
console.log(`📁 Updated file: ${SITEMAP_PATH}`)
90+
}
91+
92+
// Run the script
93+
if (require.main === module) {
94+
try {
95+
updateSitemap()
96+
} catch (error) {
97+
console.error("❌ Error updating sitemap:", error.message)
98+
process.exit(1)
99+
}
100+
}
101+
102+
module.exports = { updateSitemap, getLastModifiedDate }

0 commit comments

Comments
 (0)