Skip to content

Commit

Permalink
feat: remove useRealPaths method for backward compat
Browse files Browse the repository at this point in the history
`fdir` will resolve all symlinked files to their original paths as it
did in the previous versions. To disable this behavior, `withSymlinks`
now accepts a new optional parameter `resolvePaths`
which can be set to `false`.
  • Loading branch information
thecodrr committed Oct 18, 2022
1 parent 847079b commit cdb201f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 33 deletions.
7 changes: 2 additions & 5 deletions __tests__/fdir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe.each(["withPromise", "sync"])("fdir %s", (type) => {
}),
},
});
const api = new fdir().withSymlinks().crawl("/some/dir");
const api = new fdir().withSymlinks(false).crawl("/some/dir");
const files = await api[type]();
expect(files.length).toBe(2);
expect(files.indexOf("/some/dir/fileSymlink/")).toBeGreaterThan(-1);
Expand All @@ -192,10 +192,7 @@ describe.each(["withPromise", "sync"])("fdir %s", (type) => {
}),
},
});
const api = new fdir()
.withSymlinks()
.withRealPaths()
.crawl("/some/dir");
const api = new fdir().withSymlinks(true).crawl("/some/dir");
const files = await api[type]();
expect(files.length).toBe(2);
expect(files.indexOf("/sym/linked/file-1")).toBeGreaterThan(-1);
Expand Down
26 changes: 10 additions & 16 deletions documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,24 @@ Use this to also add the directories to the output.
const crawler = new fdir().withDirs();
```

### `withSymlinks`
### `withSymlinks(boolean)`

By default `fdir` does not follow symlinks but if such a functionality is required, you can use this flag. Do note that the returned paths are not real paths i.e., they are not relative to the original directory but the symlink itself. You can change this behavior by using `withRealPaths`.
Use this to follow all symlinks recursively.

> NOTE: This will affect crawling performance.
**Usage**

```js
const crawler = new fdir().withSymlinks();
```

### `withRealPaths`

This changes the default behavior of `withSymlinks` to return real paths instead of the symlinked paths.
**Parameters:**

Do note that using `withRealPaths` will be slower since `fdir` will call `realpath` in addition to `stat`. 2 calls instead of 1 so only use this if absolutely required.
- `resolvePaths: boolean` — By default, `fdir` returns original paths to files irrespective of whether they are inside a symlinked directory or not. If you want the paths to be relative to the symlink, set this flag to `false`. (Default is `true`).

**This is intended to be used with `withSymlinks`**
> NOTE: This will affect crawling performance.
**Usage**

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

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

### `withMaxDepth(number)`
Expand Down
8 changes: 2 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,9 @@ declare module "fdir" {

/**
* Recursively follow all symlinks
* @param {boolean} resolvePaths By default, `fdir` returns original paths to files irrespective of whether they are inside a symlinked directory or not. If you want the paths to be relative to the symlink, set this flag to `false`. (Default is `true`).
*/
withSymlinks(): Builder;

/**
* Return real paths for symlinked files & directories
*/
withRealPaths(): Builder;
withSymlinks(resolvePaths?: boolean): Builder;

/**
* Return paths relative to the root directory
Expand Down
9 changes: 3 additions & 6 deletions src/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Builder.prototype.crawlWithOptions = function(path, options) {
options.excludeFn = options.exclude;
options.filters = options.filters || [];
options.relativePath = options.relativePaths ? path : undefined;
options.useRealPaths = options.useRealPaths || true;

if (options.excludeFiles) {
options.includeDirs = true;
Expand Down Expand Up @@ -67,17 +68,13 @@ Builder.prototype.withErrors = function() {
return this;
};

Builder.prototype.withSymlinks = function() {
Builder.prototype.withSymlinks = function(resolvePaths = true) {
this.resolveSymlinks = true;
this.useRealPaths = resolvePaths;
this.withFullPaths();
return this;
};

Builder.prototype.withRealPaths = function() {
this.useRealPaths = true;
return this;
};

Builder.prototype.group = function() {
this.groupVar = true;
return this;
Expand Down

0 comments on commit cdb201f

Please sign in to comment.