-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-pages.mjs
83 lines (63 loc) · 1.9 KB
/
index-pages.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// import { Dirent } from 'fs'
import { readdir, writeFile } from 'fs/promises'
import path from 'path'
import { fileURLToPath } from 'url'
// Must be synced manually with same param in 'src/utils/index-pages.mjs'
// Investigate better syncing solution later
const INDEX_PATH_PARAM_SUFFIX = '-index'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const ignoreList = [
// sitemaps
/^sitemap\.xml.*/,
// _nextjs templates
// .hidden_files
// [nextjs_dynamic_pages]
// 404 and 500 error pages
/^([_.[]|404|500).*$/,
]
// Allow list will override ignore list
const indexPageRegex = new RegExp(
`^\\[\\[\\.{3}.*?${INDEX_PATH_PARAM_SUFFIX}\\]\\]`
)
const pageFilter = (file) => {
if (file?.isDirectory()) {
return true
}
let allow = true
for (const regx of ignoreList) {
if (file.name.match(regx)) {
allow = false
}
}
return allow && file.name.match(/\.(ts|tsx|js|jsx|md|mdoc)$/)
}
const rootDir = __dirname
const PAGES_PATH = '/pages'
async function crawlPages(filePath = '/') {
const fullPath = path.join(rootDir, PAGES_PATH, filePath)
const files = await readdir(fullPath, { withFileTypes: true })
const filteredFiles = files
.map((file) => {
if (file.name.match(indexPageRegex)) {
file.name = 'index.tsx'
}
return file
})
.filter(pageFilter)
const pages = []
for (const file of filteredFiles) {
if (file.isDirectory()) {
pages.push(...(await crawlPages(path.join(filePath, file.name))))
} else {
let pathname = file.name.split('.').slice(0, -1).join('.')
pathname = path.join(filePath, pathname.replace(/(^|\/)index$/g, ''))
pages.push({ path: pathname })
}
}
return pages
}
const pagesObj = await crawlPages()
const pages = JSON.stringify(pagesObj, null, ' ')
writeFile(path.join(rootDir, 'src/generated/pages.json'), pages)
export default {}