12cbb934b3
* Add support for .amp.js pages and resolving /page?amp=1 to page.amp.js * Update amp tests * Update example and clean up amp page resolving * Add nested amp test * page => normalizedPage * Add type to page options * Add handling of amp with all pageExtensions and normalize page * Make sure findPageFile only falls back to amp if enabled
27 lines
965 B
TypeScript
27 lines
965 B
TypeScript
import { join } from 'path'
|
|
import {isWriteable} from '../../build/is-writeable'
|
|
|
|
export async function findPageFile(rootDir: string, normalizedPagePath: string, pageExtensions: string[], amp: boolean, ampEnabled: boolean): Promise<string|null> {
|
|
if (ampEnabled) {
|
|
// Add falling back to .amp.js extension
|
|
if (!amp) pageExtensions = pageExtensions.concat(pageExtensions.map((ext) => 'amp.' + ext))
|
|
}
|
|
|
|
for (let extension of pageExtensions) {
|
|
if (amp) extension = 'amp.' + extension
|
|
const relativePagePath = `${normalizedPagePath}.${extension}`
|
|
const pagePath = join(rootDir, relativePagePath)
|
|
|
|
if (await isWriteable(pagePath)) {
|
|
return relativePagePath
|
|
}
|
|
|
|
const relativePagePathWithIndex = join(normalizedPagePath, `index.${extension}`)
|
|
const pagePathWithIndex = join(rootDir, relativePagePathWithIndex)
|
|
if (await isWriteable(pagePathWithIndex)) {
|
|
return relativePagePathWithIndex
|
|
}
|
|
}
|
|
|
|
return null
|
|
}
|