-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
page-map.ts
38 lines (34 loc) · 1.26 KB
/
page-map.ts
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
import { PageMapItem } from './types'
import { parseFileName, existsSync } from './utils'
import path from 'path'
import filterRouteLocale from './filter-route-locale'
export function findPagesDir(dir = process.cwd()): string {
// prioritize ./pages over ./src/pages
if (existsSync(path.join(dir, 'pages'))) return 'pages'
if (existsSync(path.join(dir, 'src/pages'))) return 'src/pages'
throw new Error(
"> Couldn't find a `pages` directory. Please create one under the project root"
)
}
export function getPageMap(
currentResourcePath: string,
pageMap: PageMapItem[],
fileMap: Record<string, PageMapItem>,
defaultLocale: string
): [PageMapItem[], string, string] {
const activeRouteLocale = parseFileName(currentResourcePath).locale
const pageItem = fileMap[currentResourcePath]
const metaPath = path.dirname(currentResourcePath)
const metaExtension = activeRouteLocale ? `${activeRouteLocale}.json` : `json`
const pageMeta =
fileMap[`${metaPath}/meta.${metaExtension}`]?.meta?.[pageItem.name]
const title =
(typeof pageMeta === 'string' ? pageMeta : pageMeta?.title) || pageItem.name
return [
activeRouteLocale
? filterRouteLocale(pageMap, activeRouteLocale, defaultLocale)
: pageMap,
pageItem.route,
title
]
}