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

Merged
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
43 changes: 43 additions & 0 deletions __tests__/fdir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,49 @@ 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) => {
SuperchupuDev marked this conversation as resolved.
Show resolved Hide resolved
mock(mockFsWithSymlinks);

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

// fdir doesn't support this usecase
test(`[${type}] crawl all files and include resolved symlinks with real paths with relative paths on`, async (t) => {
mock(mockFsWithSymlinks);

const api = new fdir()
.withSymlinks()
.withRelativePaths()
.crawl("/some/dir");
const files = await api[type]();
t.expect(files).toHaveLength(3);
t.expect(
files.indexOf(path.join("d", "file-1")) > -1
).toBeTruthy();
t.expect(
files.indexOf(path.join("d", "file-excluded-1")) > -1
).toBeTruthy();
t.expect(
files.indexOf(`${path.sep}file-2`) > -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`.
SuperchupuDev marked this conversation as resolved.
Show resolved Hide resolved

**Parameters:**

Expand Down
2 changes: 1 addition & 1 deletion src/api/functions/join-path.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Options, PathSeparator } from "../../types";

function joinPathWithBasePath(filename: string, directoryPath: string) {
export function joinPathWithBasePath(filename: string, directoryPath: string) {
return directoryPath + filename;
}

Expand Down
7 changes: 5 additions & 2 deletions src/api/walker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve as pathResolve } from "path";
import { basename, dirname, resolve as pathResolve } from "path";
import { cleanPath, convertSlashes } from "../utils";
import { ResultCallback, WalkerState, Options } from "../types";
import * as joinPath from "./functions/join-path";
Expand Down Expand Up @@ -117,14 +117,17 @@ 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 = joinPath.joinPathWithBasePath(entry.name, directoryPath);
this.resolveSymlink!(path, this.state, (stat, 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 {
const filename = basename(resolvedPath);
const directoryPath = this.normalizePath(dirname(resolvedPath));
resolvedPath = this.joinPath(filename, directoryPath);
this.pushFile(resolvedPath, files, this.state.counts, filters);
}
});
Expand Down
Loading