Skip to content

Commit d6a4f99

Browse files
committed
feat: enhance sitemap update script to dynamically discover page files and handle missing entries
1 parent d4ca4b3 commit d6a4f99

File tree

1 file changed

+78
-10
lines changed

1 file changed

+78
-10
lines changed

scripts/update-sitemap-dates.js

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,58 @@ const { execSync } = require("child_process")
1010
*/
1111

1212
const SITEMAP_PATH = path.join(__dirname, "../apps/web-roo-code/src/app/sitemap.ts")
13+
const APP_DIR = path.join(__dirname, "../apps/web-roo-code/src/app")
1314

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",
15+
/**
16+
* Recursively find all page.tsx files in the app directory
17+
* @param {string} dir - Directory to search
18+
* @param {string} basePath - Base path for building relative paths
19+
* @returns {Array<{url: string, filePath: string}>} Array of URL to file mappings
20+
*/
21+
function findPageFiles(dir, basePath = "") {
22+
const pages = []
23+
const entries = fs.readdirSync(dir, { withFileTypes: true })
24+
25+
for (const entry of entries) {
26+
const fullPath = path.join(dir, entry.name)
27+
28+
if (entry.isDirectory()) {
29+
// Skip certain directories that shouldn't contain pages
30+
if (entry.name.startsWith(".") || entry.name === "api") {
31+
continue
32+
}
33+
34+
// Recursively search subdirectories
35+
const subPages = findPageFiles(fullPath, path.join(basePath, entry.name))
36+
pages.push(...subPages)
37+
} else if (entry.name === "page.tsx") {
38+
// Convert file path to URL following Next.js app router conventions
39+
const url = basePath === "" ? "/" : `/${basePath}`
40+
const relativeFilePath = path.relative(path.join(__dirname, "../"), fullPath)
41+
42+
pages.push({
43+
url,
44+
filePath: relativeFilePath,
45+
})
46+
}
47+
}
48+
49+
return pages
50+
}
51+
52+
/**
53+
* Get URL to file mapping by scanning the app directory
54+
* @returns {Object} Map of URLs to their corresponding page files
55+
*/
56+
function getUrlToFileMap() {
57+
const pages = findPageFiles(APP_DIR)
58+
const urlToFileMap = {}
59+
60+
for (const page of pages) {
61+
urlToFileMap[page.url] = page.filePath
62+
}
63+
64+
return urlToFileMap
2165
}
2266

2367
/**
@@ -51,7 +95,16 @@ function getLastModifiedDate(filePath) {
5195
* Update the sitemap.ts file with new dates
5296
*/
5397
function updateSitemap() {
54-
console.log("🔍 Reading current sitemap...")
98+
console.log("🔍 Discovering page files...")
99+
100+
// Dynamically discover page files
101+
const urlToFileMap = getUrlToFileMap()
102+
console.log(`Found ${Object.keys(urlToFileMap).length} page files:`)
103+
for (const [url, filePath] of Object.entries(urlToFileMap)) {
104+
console.log(` ${url}${filePath}`)
105+
}
106+
107+
console.log("\n🔍 Reading current sitemap...")
55108

56109
// Read the current sitemap file
57110
const sitemapContent = fs.readFileSync(SITEMAP_PATH, "utf8")
@@ -60,7 +113,7 @@ function updateSitemap() {
60113

61114
// Get modification dates for each URL
62115
const urlDates = {}
63-
for (const [url, filePath] of Object.entries(URL_TO_FILE_MAP)) {
116+
for (const [url, filePath] of Object.entries(urlToFileMap)) {
64117
const lastModified = getLastModifiedDate(filePath)
65118
urlDates[url] = lastModified
66119
console.log(` ${url}: ${lastModified}`)
@@ -78,6 +131,21 @@ function updateSitemap() {
78131
`(\\{[^}]*url:\\s*\`\\$\\{baseUrl\\}${url.replace("/", "\\/")}\`[^}]*lastModified:\\s*)new Date\\([^)]*\\)`,
79132
"g",
80133
)
134+
const isFound = updatedContent.match(urlPattern)
135+
136+
if (!isFound) {
137+
console.error(`\n⚠️ No matching entry found for ${url}, skipping update`)
138+
console.log(
139+
`📝 To add this missing entry to the sitemap, add the following to apps/web-roo-code/src/app/sitemap.ts:`,
140+
)
141+
console.log(`\n{`)
142+
console.log(`\turl: \`\${baseUrl}${url}\`,`)
143+
console.log(`\tlastModified: new Date("${isoDate}"),`)
144+
console.log(`\tchangeFrequency: "monthly",`)
145+
console.log(`\tpriority: 0.8,`)
146+
console.log(`},\n`)
147+
continue
148+
}
81149

82150
updatedContent = updatedContent.replace(urlPattern, `$1new Date("${isoDate}")`)
83151
}
@@ -99,4 +167,4 @@ if (require.main === module) {
99167
}
100168
}
101169

102-
module.exports = { updateSitemap, getLastModifiedDate }
170+
module.exports = { updateSitemap, getLastModifiedDate, findPageFiles, getUrlToFileMap }

0 commit comments

Comments
 (0)