diff --git a/__tests__/fdir.test.ts b/__tests__/fdir.test.ts index a16586f..0224c79 100644 --- a/__tests__/fdir.test.ts +++ b/__tests__/fdir.test.ts @@ -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) => { + 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() diff --git a/documentation.md b/documentation.md index 602c938..8183efd 100644 --- a/documentation.md +++ b/documentation.md @@ -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:** diff --git a/src/api/functions/join-path.ts b/src/api/functions/join-path.ts index e8c599b..daba715 100644 --- a/src/api/functions/join-path.ts +++ b/src/api/functions/join-path.ts @@ -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; } diff --git a/src/api/walker.ts b/src/api/walker.ts index b2c5f07..ffa02d5 100644 --- a/src/api/walker.ts +++ b/src/api/walker.ts @@ -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"; @@ -117,7 +117,7 @@ export class Walker { 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); @@ -125,6 +125,9 @@ export class Walker { 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); } });