filepath.ts
This file
Path
src/utils/filepath.ts
60 lines · 1.3 KiB · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 82bdc4d point at it.
4.9.0Yusuke Wadabefore adoption82bdc4d
lines 1–60why, one step back1/**2 * @module3 * FilePath utility.4 */56type FilePathOptions = {7 filename: string8 root?: string9 defaultDocument?: string10}1112export const getFilePath = (options: FilePathOptions): string | undefined => {13 let filename = options.filename14 const defaultDocument = options.defaultDocument || 'index.html'1516 if (filename.endsWith('/')) {17 // /top/ => /top/index.html18 filename = filename.concat(defaultDocument)19 } else if (!filename.match(/\.[a-zA-Z0-9_-]+$/)) {20 // /top => /top/index.html21 filename = filename.concat('/' + defaultDocument)22 }2324 const path = getFilePathWithoutDefaultDocument({25 root: options.root,26 filename,27 })2829 return path30}3132export const getFilePathWithoutDefaultDocument = (33 options: Omit<FilePathOptions, 'defaultDocument'>34): string | undefined => {35 let root = options.root || ''36 let filename = options.filename3738 if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) {39 return40 }4142 // /foo.html => foo.html43 filename = filename.replace(/^\.?[\/\\]/, '')4445 // foo\bar.txt => foo/bar.txt46 filename = filename.replace(/\\/, '/')4748 // assets/ => assets49 root = root.replace(/\/$/, '')5051 // ./assets/foo.html => assets/foo.html52 let path = root ? root + '/' + filename : filename53 path = path.replace(/^\.?\//, '')5455 if (root[0] !== '/' && path[0] === '/') {56 return57 }5859 return path60}