Skip to content

Commit

Permalink
feat(*): Ability to skip expanding of symlinked directories
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed Jan 12, 2018
1 parent 658a46f commit 9cec4a8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ Return only files.

Return only directories.

#### followSymlinkedDirectories

* Type: `boolean`
* Default: `true`

Follow symlinked directories when expanding `**` patterns.

#### transform

* Type: `Function`
Expand Down Expand Up @@ -214,7 +221,7 @@ Not fully, because `fast-glob` not implements all options of `node-glob`. See ta
| `matchBase` ||
| `nodir` | [`onlyFiles`](https://github.com/mrmlnc/fast-glob#onlyfiles) |
| `ignore` | [`ignore`](https://github.com/mrmlnc/fast-glob#ignore) |
| `follow` | by default |
| `follow` | [`followSymlinkedDirectories`](https://github.com/mrmlnc/fast-glob#followsymlinkeddirectories) |
| `realpath` ||
| `absolute` ||

Expand Down
2 changes: 2 additions & 0 deletions src/managers/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Managers → Options', () => {
stats: false,
onlyFiles: true,
onlyDirectories: false,
followSymlinkedDirectories: true,
transform: null
};

Expand All @@ -30,6 +31,7 @@ describe('Managers → Options', () => {
stats: true,
onlyFiles: true,
onlyDirectories: false,
followSymlinkedDirectories: true,
transform: null
};

Expand Down
5 changes: 5 additions & 0 deletions src/managers/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface IOptions<T = EntryItem> {
* Return only directories.
*/
onlyDirectories: boolean;
/**
* Follow symlinked directories when expanding `**` patterns.
*/
followSymlinkedDirectories: boolean;
/**
* Allows you to transform a path or `fs.Stats` object before sending to the array.
*/
Expand All @@ -51,6 +55,7 @@ export function prepare(options?: IPartialOptions): IOptions {
stats: false,
onlyFiles: true,
onlyDirectories: false,
followSymlinkedDirectories: true,
transform: null
}, options);
}
20 changes: 20 additions & 0 deletions src/providers/reader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,26 @@ describe('Providers → Reader', () => {
assert.ok(!actual);
});

it('should returns true for symlinked directory when the «followSymlinkedDirectories» option is enabled', () => {
const reader = getReader();

const entry = getDirectoryEntry(false /** dot */, true /** isSymbolicLink */);

const actual = reader.deep(entry, []);

assert.ok(actual);
});

it('should returns false for symlinked directory when the «followSymlinkedDirectories» option is disabled', () => {
const reader = getReader({ followSymlinkedDirectories: false });

const entry = getDirectoryEntry(false /** dot */, true /** isSymbolicLink */);

const actual = reader.deep(entry, []);

assert.ok(!actual);
});

it('should returns false if specified a limit of depth for «deep» option ', () => {
const reader = getReader({ deep: 2 });

Expand Down
12 changes: 12 additions & 0 deletions src/providers/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export default abstract class Reader {
}
}

// Skip reading if the directory is symlink and we don't want expand symlinks
if (this.filterBySymlinkType(entry)) {
return false;
}

// Skip reading if the directory name starting with a period and is not expected
if (!this.options.dot && this.isDotDirectory(entry)) {
return false;
Expand Down Expand Up @@ -108,4 +113,11 @@ export default abstract class Reader {
private filterByDirectoryType(entry: IEntry): boolean {
return this.options.onlyDirectories && !entry.isDirectory();
}

/**
* Returns true for symlinked directories if the «followSymlinks» option is enabled.
*/
private filterBySymlinkType(entry: IEntry): boolean {
return !this.options.followSymlinkedDirectories && entry.isSymbolicLink();
}
}

0 comments on commit 9cec4a8

Please # to comment.