fix(utils/filepath): filepath supports Windows (#1642)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent d1ff3d8 · Browse files · 3 files·+23 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+23 added−4 removed
- tests:+13 added−0 removed
- other:+10 added−4 removed
Provenance
- Commit
48dc87d- Tree
17f3fed- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
d1ff3d8
3 files changed+23 added−4 removed
modified:deno_dist/utils/filepath.ts+5 added-2 removed
4 unmodified lines567899101112135 unmodified lines19202122232324252627282930315 unmodified lines4 unmodified lines}
export const getFilePath = (options: FilePathOptions): string | undefined => { let filename = options.filename if (/(?:^|\/)\.\.(?:$|\/)/.test(filename)) return if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) return
let root = options.root || '' const defaultDocument = options.defaultDocument || 'index.html'
5 unmodified lines filename = filename.concat('/' + defaultDocument) }
// /foo.html => foo.html filename = filename.replace(/^\.?\//, '') filename = filename.replace(/^\.?[\/\\]/, '')
// foo\bar.txt => foo/bar.txt filename = filename.replace(/\\/, '/')
// assets/ => assets root = root.replace(/\/$/, '')
// ./assets/foo.html => assets/foo.html5 unmodified linesmodified:src/utils/filepath.test.ts+13 added
23 unmodified lines2425262728293031323334353637383940414223 unmodified lines expect(getFilePath({ filename: './foo..bar.txt' })).toBe('foo..bar.txt') expect(getFilePath({ filename: './..foo/bar.txt' })).toBe('..foo/bar.txt') expect(getFilePath({ filename: './foo../bar.txt' })).toBe('foo../bar.txt') expect(getFilePath({ filename: './..foo../bar.txt' })).toBe('..foo../bar.txt')
expect(getFilePath({ filename: slashToBackslash('/../foo') })).toBeUndefined() expect(getFilePath({ filename: slashToBackslash('./../foo') })).toBeUndefined() expect(getFilePath({ filename: slashToBackslash('foo..bar.txt') })).toBe('foo..bar.txt') expect(getFilePath({ filename: slashToBackslash('/foo..bar.txt') })).toBe('foo..bar.txt') expect(getFilePath({ filename: slashToBackslash('./foo..bar.txt') })).toBe('foo..bar.txt') expect(getFilePath({ filename: slashToBackslash('./..foo/bar.txt') })).toBe('..foo/bar.txt') expect(getFilePath({ filename: slashToBackslash('./foo../bar.txt') })).toBe('foo../bar.txt') expect(getFilePath({ filename: slashToBackslash('./..foo../bar.txt') })).toBe('..foo../bar.txt') })})
function slashToBackslash(filename: string) { return filename.split('/').join('\\')}modified:src/utils/filepath.ts+5 added-2 removed
4 unmodified lines567899101112135 unmodified lines19202122232324252627282930315 unmodified lines4 unmodified lines}
export const getFilePath = (options: FilePathOptions): string | undefined => { let filename = options.filename if (/(?:^|\/)\.\.(?:$|\/)/.test(filename)) return if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) return
let root = options.root || '' const defaultDocument = options.defaultDocument || 'index.html'
5 unmodified lines filename = filename.concat('/' + defaultDocument) }
// /foo.html => foo.html filename = filename.replace(/^\.?\//, '') filename = filename.replace(/^\.?[\/\\]/, '')
// foo\bar.txt => foo/bar.txt filename = filename.replace(/\\/, '/')
// assets/ => assets root = root.replace(/\/$/, '')
// ./assets/foo.html => assets/foo.html5 unmodified lines