-
-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { MapFS } from './map-fs' | ||
|
||
describe('MapFS', () => { | ||
const fs = new MapFS({}) | ||
it('should resolve relative file paths', () => { | ||
expect(fs.resolve('foo/bar', 'coo', '')).toEqual('foo/bar/coo') | ||
}) | ||
it('should resolve to parent', () => { | ||
expect(fs.resolve('foo/bar', '../coo', '')).toEqual('foo/coo') | ||
}) | ||
it('should resolve to root', () => { | ||
expect(fs.resolve('foo/bar', '../../coo', '')).toEqual('coo') | ||
}) | ||
it('should resolve exceeding root', () => { | ||
expect(fs.resolve('foo/bar', '../../../coo', '')).toEqual('coo') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { isNil } from '../util' | ||
|
||
export class MapFS { | ||
constructor (private mapping: {[key: string]: string}) {} | ||
|
||
public sep = '/' | ||
|
||
async exists (filepath: string) { | ||
return this.existsSync(filepath) | ||
} | ||
|
||
existsSync (filepath: string) { | ||
return !isNil(this.mapping[filepath]) | ||
} | ||
|
||
async readFile (filepath: string) { | ||
return this.readFileSync(filepath) | ||
} | ||
|
||
readFileSync (filepath: string) { | ||
const content = this.mapping[filepath] | ||
if (isNil(content)) throw new Error(`ENOENT: ${filepath}`) | ||
return content | ||
} | ||
|
||
dirname (filepath: string) { | ||
const segments = filepath.split(this.sep) | ||
segments.pop() | ||
return segments.join(this.sep) | ||
} | ||
|
||
resolve (dir: string, file: string, ext: string) { | ||
file += ext | ||
if (dir === '.') return file | ||
const segments = dir.split(this.sep) | ||
for (const segment of file.split(this.sep)) { | ||
if (segment === '.' || segment === '') continue | ||
else if (segment === '..') segments.pop() | ||
else segments.push(segment) | ||
} | ||
return segments.join(this.sep) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters