Skip to content

Commit

Permalink
fix: support symlinks without real paths with relative paths enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Sep 26, 2024
1 parent 6ba7d22 commit 1e278f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
19 changes: 19 additions & 0 deletions __tests__/fdir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,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 && !excludeSymlinks) {
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

0 comments on commit 1e278f9

Please sign in to comment.