import fs from 'fs'; import path from 'path'; export default function resolveFilePath( urlPath: string[] | undefined, basePath: string, missingExtname: string[] = [], missingFilename: string[] = [] ): string | null { // If urlPath is undefined, set it to an empty array const segments = urlPath || [] // Construct the base file path let filePath: string = path.join(basePath, ...segments) let find:boolean = false do { if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) { break } for (const extend of missingExtname) { const newPath = filePath + extend; if (fs.existsSync(newPath) && fs.statSync(newPath).isFile()) { filePath = newPath find = true break } } if (find) break if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) { for (const extend of missingFilename) { const newPath = path.join(filePath, extend); if (fs.existsSync(newPath) && fs.statSync(newPath).isFile()) { filePath = newPath find = true break } } } if (find) break return null } while(0) if (!filePath.startsWith(basePath)) { return null } return filePath }