Skip to content

Commit 9e9c03c

Browse files
committed
feature: @putout/operator-filesystem: findFile: exclude: function
1 parent 8ece460 commit 9e9c03c

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

packages/operator-filesystem/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const [dirPath] = findFile(ast, 'hello');
3737
const newDirectoryPath = createDirectory(dirPath, 'world');
3838
```
3939

40-
### `findFile(directoryPath: DirectoryPath, name: string | string[], exclude?: string[]): (FilePath | DirectoryPath)[]`
40+
### `findFile(directoryPath: DirectoryPath, name: string | string[], exclude?: string[] | (FilePath) => boolean): (FilePath | DirectoryPath)[]`
4141

4242
```js
4343
const {operator} = require('putout');
@@ -60,8 +60,14 @@ Or `exclude` some files:
6060
```js
6161
import {operator} from 'putout';
6262

63-
const {findFile} = operator;
64-
const coupleFiles = findFile(ast, '*.ts', ['*.d.ts']);
63+
const {findFile, getFilename} = operator;
64+
65+
const coupleFilesWithExcludeArray = findFile(ast, '*.ts', ['*.d.ts']);
66+
67+
const coupleFilesWithExcludeFn = findFile(ast, '*.ts', (filePath) => {
68+
const name = getFilename(filePath);
69+
return !name.endsWith('*.d.ts');
70+
});
6571
```
6672

6773
And even search for a directory:

packages/operator-filesystem/lib/filesystem.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
} = require('@putout/operate');
1212

1313
const maybeFS = require('./maybe-fs');
14+
const isFn = (a) => typeof a === 'function';
1415
const isString = (a) => typeof a === 'string';
1516
const {isArray} = Array;
1617
const maybeArray = (a) => isArray(a) ? a : [a];
@@ -64,7 +65,10 @@ module.exports.getParentDirectory = (filePath) => {
6465

6566
module.exports.findFile = findFile;
6667

67-
function isExcluded(name, base, exclude) {
68+
function isExcluded({path, name, base, exclude}) {
69+
if (isFn(exclude))
70+
return exclude(path);
71+
6872
for (const currentExclude of exclude) {
6973
if (name === currentExclude || getRegExp(currentExclude).test(base))
7074
return true;
@@ -85,10 +89,18 @@ function findFile(node, name, exclude = []) {
8589

8690
for (const name of names) {
8791
if (value === name || getRegExp(name).test(base)) {
88-
if (isExcluded(name, base, exclude))
92+
const path = filenamePath.parentPath;
93+
const excluded = isExcluded({
94+
path,
95+
name,
96+
base,
97+
exclude,
98+
});
99+
100+
if (excluded)
89101
continue;
90102

91-
filePaths.push(filenamePath.parentPath);
103+
filePaths.push(path);
92104
}
93105
}
94106
}

packages/operator-filesystem/lib/filesystem.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,39 @@ test('putout: operator: filesystem: findFile: exclude', (t) => {
179179
t.end();
180180
});
181181

182+
test('putout: operator: filesystem: findFile: exclude: function', (t) => {
183+
const ast = parseFilesystem([
184+
'/',
185+
'/hello/',
186+
['/hello/world.js', 'const a = 5'],
187+
['/hello/hello.js', 'const a = <A></A>'],
188+
]);
189+
190+
const exclude = (path) => {
191+
const source = readFileContent(path);
192+
return source.includes('<');
193+
};
194+
195+
findFile(ast, '*.js', exclude).map(removeFile);
196+
197+
const expected = {
198+
type: 'directory',
199+
filename: '/',
200+
files: [{
201+
type: 'directory',
202+
filename: '/hello',
203+
files: [{
204+
type: 'file',
205+
filename: '/hello/hello.js',
206+
content: 'const a = <A></A>',
207+
}],
208+
}],
209+
};
210+
211+
t.equalFilesystems(ast, expected);
212+
t.end();
213+
});
214+
182215
test('putout: operator: filesystem: findFile: no names', (t) => {
183216
const ast = parse(montag`
184217
${FS}({

0 commit comments

Comments
 (0)