2020-05-26 14:50:28 +02:00
|
|
|
import { join, sep as pathSeparator, normalize } from 'path'
|
2020-11-06 03:33:14 +01:00
|
|
|
import chalk from 'chalk'
|
2019-09-11 20:26:10 +02:00
|
|
|
import { warn } from '../../build/output/log'
|
2020-06-06 17:40:06 +02:00
|
|
|
import { promises } from 'fs'
|
2021-06-30 13:44:40 +02:00
|
|
|
import { denormalizePagePath } from '../normalize-page-path'
|
2021-05-10 10:39:18 +02:00
|
|
|
import { fileExists } from '../../lib/file-exists'
|
2020-05-26 14:50:28 +02:00
|
|
|
|
|
|
|
async function isTrueCasePagePath(pagePath: string, pagesDir: string) {
|
|
|
|
const pageSegments = normalize(pagePath).split(pathSeparator).filter(Boolean)
|
|
|
|
|
|
|
|
const segmentExistsPromises = pageSegments.map(async (segment, i) => {
|
|
|
|
const segmentParentDir = join(pagesDir, ...pageSegments.slice(0, i))
|
2020-06-06 17:40:06 +02:00
|
|
|
const parentDirEntries = await promises.readdir(segmentParentDir)
|
2020-05-26 14:50:28 +02:00
|
|
|
return parentDirEntries.includes(segment)
|
|
|
|
})
|
|
|
|
|
|
|
|
return (await Promise.all(segmentExistsPromises)).every(Boolean)
|
|
|
|
}
|
2019-02-24 22:08:35 +01:00
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
export async function findPageFile(
|
|
|
|
rootDir: string,
|
|
|
|
normalizedPagePath: string,
|
|
|
|
pageExtensions: string[]
|
|
|
|
): Promise<string | null> {
|
2020-06-04 19:32:45 +02:00
|
|
|
const foundPagePaths: string[] = []
|
|
|
|
|
|
|
|
const page = denormalizePagePath(normalizedPagePath)
|
2019-09-11 20:26:10 +02:00
|
|
|
|
2019-04-11 20:59:26 +02:00
|
|
|
for (const extension of pageExtensions) {
|
2020-06-04 19:32:45 +02:00
|
|
|
if (!normalizedPagePath.endsWith('/index')) {
|
|
|
|
const relativePagePath = `${page}.${extension}`
|
|
|
|
const pagePath = join(rootDir, relativePagePath)
|
2019-02-24 22:08:35 +01:00
|
|
|
|
2021-05-10 10:39:18 +02:00
|
|
|
if (await fileExists(pagePath)) {
|
2020-04-07 19:24:53 +02:00
|
|
|
foundPagePaths.push(relativePagePath)
|
|
|
|
}
|
2019-02-24 22:08:35 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 19:32:45 +02:00
|
|
|
const relativePagePathWithIndex = join(page, `index.${extension}`)
|
2019-02-24 22:08:35 +01:00
|
|
|
const pagePathWithIndex = join(rootDir, relativePagePathWithIndex)
|
2021-05-10 10:39:18 +02:00
|
|
|
if (await fileExists(pagePathWithIndex)) {
|
2019-09-11 20:26:10 +02:00
|
|
|
foundPagePaths.push(relativePagePathWithIndex)
|
2019-02-24 22:08:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 20:26:10 +02:00
|
|
|
if (foundPagePaths.length < 1) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:50:28 +02:00
|
|
|
if (!(await isTrueCasePagePath(foundPagePaths[0], rootDir))) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2019-09-11 20:26:10 +02:00
|
|
|
if (foundPagePaths.length > 1) {
|
|
|
|
warn(
|
|
|
|
`Duplicate page detected. ${chalk.cyan(
|
|
|
|
join('pages', foundPagePaths[0])
|
|
|
|
)} and ${chalk.cyan(
|
|
|
|
join('pages', foundPagePaths[1])
|
|
|
|
)} both resolve to ${chalk.cyan(normalizedPagePath)}.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return foundPagePaths[0]
|
2019-02-24 22:08:35 +01:00
|
|
|
}
|