Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support symlinks without real paths with relative paths enabled #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions __tests__/fdir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,25 @@ for (const type of apiTypes) {
mock.restore();
});

test(`[${type}] crawl all files and include resolved symlinks without real paths with relative paths on`, async (t) => {
mock(mockFsWithSymlinks);

const api = new fdir()
.withSymlinks({ resolvePaths: false })
.withRelativePaths()
.withPathSeparator("/")
.crawl("/some/dir");
const files = await api[type]();
t.expect(files).toHaveLength(3);
t.expect(
files.indexOf("dirSymlink/file-1") > -1
).toBeTruthy();
t.expect(
files.indexOf("dirSymlink/file-excluded-1") > -1
).toBeTruthy();
mock.restore();
});

test("crawl all files and include resolved symlinks with exclusions", async (t) => {
mock(mockFsWithSymlinks);
const api = new fdir()
Expand Down
2 changes: 1 addition & 1 deletion documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const crawler = new fdir().withDirs();

### `withSymlinks({ resolvePaths: boolean })`

Use this to follow all symlinks recursively.
Use this to follow all symlinks recursively. Not available with relative paths on unless the parameter is set to `false`.

**Parameters:**

Expand Down
12 changes: 9 additions & 3 deletions src/api/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,20 @@ export class Walker<TOutput extends Output> {
if (exclude && exclude(entry.name, path)) continue;
this.walkDirectory(this.state, path, depth - 1, this.walk);
} else if (entry.isSymbolicLink() && resolveSymlinks) {
let path = this.joinPath(entry.name, directoryPath);
let path = directoryPath + entry.name;
this.resolveSymlink!(path, this.state, (stat, resolvedPath) => {
resolvedPath = this.normalizePath(resolvedPath);

if (stat.isDirectory()) {
resolvedPath = this.normalizePath(resolvedPath);
if (exclude && exclude(entry.name, resolvedPath)) return;

this.walkDirectory(this.state, resolvedPath, depth - 1, this.walk);
} else {
if (!this.state.options.useRealPaths && this.state.options.relativePaths) {
resolvedPath = resolvedPath.substring(this.root.length, resolvedPath.length - 1);
} else {
// resolvedPath has a trailing slash due to the normalizePath call
resolvedPath = resolvedPath.substring(0, resolvedPath.length - 1)
}
this.pushFile(resolvedPath, files, this.state.counts, filters);
}
});
Expand Down
Loading