Skip to content

Commit

Permalink
feat: rewrite root paths
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Nov 29, 2017
1 parent 651e6e2 commit bae6451
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.idea
node_modules
/.idea
.nyc_output
coverage
/.nyc_output/
/coverage/
package-lock.json
yarn.lock
/lib/
21 changes: 21 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('rewrite(fs, rewrites)', () => {
const lfs = link(vol, ['/lol', '/foo']);
expect(lfs.readFileSync('/lol', 'utf8')).toBe('bar');
});

it('Each path step should be rewritten completely', () => {
const vol = Volume.fromJSON({'/foo/bar': 'hello'});
const lfs = link(vol, ['/lol', '/fo']);
Expand All @@ -19,6 +20,7 @@ describe('rewrite(fs, rewrites)', () => {
expect(err.code).toBe('ENOENT');
}
});

it('Invalid rewrite routes argument throws', () => {
const vol = Volume.fromJSON({'/foo/bar': 'hello'});
try {
Expand All @@ -28,6 +30,7 @@ describe('rewrite(fs, rewrites)', () => {
expect(err.message === 'not_this').toBe(false);
}
});

it('Invalid path argument gets proxied', () => {
const vol = Volume.fromJSON({'/foo/bar': 'hello'});
try {
Expand All @@ -38,4 +41,22 @@ describe('rewrite(fs, rewrites)', () => {
expect(err.code).toBe('EBADF');
}
});

it('rewrites multi-step paths', () => {
const vol = Volume.fromJSON({
'/1/2/3/4': 'foo'
});
const lfs = link(vol, ['/lol', '/1/2/3']);

expect(lfs.readFileSync('/lol/4', 'utf8')).toBe('foo');
});

it('rewrites root path', () => {
const vol = Volume.fromJSON({
'/1/2/3/4': 'foo'
});
const lfs = link(vol, ['/', '/1/2/3']);

expect(lfs.readFileSync('/4', 'utf8')).toBe('foo');
});
});
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {resolve} from 'path';
import {resolve, sep} from 'path';


export const props = [
Expand Down Expand Up @@ -127,10 +127,18 @@ export function link(fs, rewrites: string[] | string[][]): any {
let filename = resolve(String(path));
for(const [from, to] of rews) {
if(filename.indexOf(from) === 0) {
// filename = filename.replace(from, to);
const regex = new RegExp('^(' + from.replace(/\\/g, '\\\\') + ')(\\\\|\/|$)');
filename = filename.replace(regex, (match, p1, p2, off, str) => to + p2);
}
const rootRegex = /(?:^[a-zA-Z]:\\$)|(?:^\/$)/; // C:\ vs /
const isRoot = from.match(rootRegex);
const baseRegex = '^(' + from.replace(/\\/g, '\\\\') + ')';

if(isRoot) {
const regex = new RegExp(baseRegex);
filename = filename.replace(regex, () => to + sep);
} else {
const regex = new RegExp(baseRegex + '(\\\\|\/|$)');
filename = filename.replace(regex, (match, p1, p2) => to + p2);
}
}
}

args[0] = filename;
Expand Down

0 comments on commit bae6451

Please sign in to comment.