Skip to content
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
36 changes: 36 additions & 0 deletions __tests__/fdir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ for (const type of apiTypes) {
mock.restore();
});

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

const api = new fdir().withSymlinks({ resolvePaths: false }).crawl("/some/dir");
const files = await api[type]();
t.expect(files).toHaveLength(3);
t.expect(
files.indexOf(resolveSymlinkRoot("/some/dir/dirSymlink/file-1")) > -1
).toBeTruthy();
t.expect(
files.indexOf(resolveSymlinkRoot("/some/dir/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 Expand Up @@ -379,6 +394,27 @@ for (const type of apiTypes) {
}
mock.restore();
});

test("crawl all files (including symlinks without real paths) and throw errors", async (t) => {
mock({
"/other/dir": {},
"/some/dir": {
fileSymlink: mock.symlink({
path: "/other/dir/file-3",
}),
},
});

try {
const api = new fdir().withErrors().withSymlinks({ resolvePaths: false }).crawl("/some/dir");

await api[type]();
} catch (e) {
if (e instanceof Error)
t.expect(e.message.includes("no such file or directory")).toBeTruthy();
}
mock.restore();
});
}

test(`[async] crawl directory & use abort signal to abort`, async (t) => {
Expand Down
7 changes: 4 additions & 3 deletions documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Use this to also add the directories to the output.
const crawler = new fdir().withDirs();
```

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

Use this to follow all symlinks recursively.

Expand All @@ -116,10 +116,10 @@ Use this to follow all symlinks recursively.

```js
// to resolve all symlinked paths to their original path
const crawler = new fdir().withSymlinks(true);
const crawler = new fdir().withSymlinks({ resolvePaths: true });

// to disable path resolution
const crawler = new fdir().withSymlinks(false);
const crawler = new fdir().withSymlinks({ resolvePaths: false });
```

### `withMaxDepth(number)`
Expand Down Expand Up @@ -379,6 +379,7 @@ type Options = {
onlyCounts?: boolean;
filters?: FilterFn[];
resolveSymlinks?: boolean;
useRealPaths?: boolean;
excludeFiles?: boolean;
exclude?: ExcludeFn;
relativePaths?: boolean;
Expand Down
43 changes: 42 additions & 1 deletion src/api/functions/resolve-symlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ const resolveSymlinksAsync: ResolveSymlinkFunction = function(
} = state;
queue.enqueue();

fs.stat(path, (error, stat) => {
Comment thread
SuperchupuDev marked this conversation as resolved.
if (error) {
queue.dequeue(suppressErrors ? null : error, state);
return;
}

callback(stat, path);
queue.dequeue(null, state);
});
};

const resolveSymlinksWithRealPathsAsync: ResolveSymlinkFunction = function(
path,
state,
callback
) {
const {
queue,
options: { suppressErrors },
} = state;
queue.enqueue();

fs.realpath(path, (error, resolvedPath) => {
if (error) {
queue.dequeue(suppressErrors ? null : error, state);
Expand All @@ -36,6 +58,19 @@ const resolveSymlinksSync: ResolveSymlinkFunction = function(
path,
state,
callback
) {
try {
const stat = fs.statSync(path);
Comment thread
SuperchupuDev marked this conversation as resolved.
callback(stat, path);
} catch (e) {
if (!state.options.suppressErrors) throw e;
}
};

const resolveSymlinksWithRealPathsSync: ResolveSymlinkFunction = function(
path,
state,
callback
) {
try {
const resolvedPath = fs.realpathSync(path);
Expand All @@ -52,5 +87,11 @@ export function build(
): ResolveSymlinkFunction | null {
if (!options.resolveSymlinks) return null;

return isSynchronous ? resolveSymlinksSync : resolveSymlinksAsync;
if (options.useRealPaths)
return isSynchronous
? resolveSymlinksWithRealPathsSync
: resolveSymlinksWithRealPathsAsync;
return isSynchronous
? resolveSymlinksSync
: resolveSymlinksAsync;
}
3 changes: 2 additions & 1 deletion src/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ export class Builder<TReturnType extends Output = PathsOutput> {
return this;
}

withSymlinks() {
withSymlinks({ resolvePaths = true } = {}) {
this.options.resolveSymlinks = true;
this.options.useRealPaths = resolvePaths;
return this.withFullPaths();
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type Options = {
onlyCounts?: boolean;
filters: FilterPredicate[];
resolveSymlinks?: boolean;
useRealPaths?: boolean;
excludeFiles?: boolean;
exclude?: ExcludePredicate;
relativePaths?: boolean;
Expand Down