@putout/operator-filesystem 5.2.0 → 5.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -37,7 +37,7 @@ const [dirPath] = findFile(ast, 'hello');
37
37
  const newDirectoryPath = createDirectory(dirPath, 'world');
38
38
  ```
39
39
 
40
- ### `findFile(directoryPath: DirectoryPath, name: string | string[], exclude?: string[]): (FilePath | DirectoryPath)[]`
40
+ ### `findFile(directoryPath: DirectoryPath, name: string | string[], exclude?: string[] | (FilePath) => boolean): (FilePath | DirectoryPath)[]`
41
41
 
42
42
  ```js
43
43
  const {operator} = require('putout');
@@ -60,8 +60,14 @@ Or `exclude` some files:
60
60
  ```js
61
61
  import {operator} from 'putout';
62
62
 
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
+ });
65
71
  ```
66
72
 
67
73
  And even search for a directory:
package/lib/filesystem.js CHANGED
@@ -11,6 +11,7 @@ const {
11
11
  } = require('@putout/operate');
12
12
 
13
13
  const maybeFS = require('./maybe-fs');
14
+ const isFn = (a) => typeof a === 'function';
14
15
  const isString = (a) => typeof a === 'string';
15
16
  const {isArray} = Array;
16
17
  const maybeArray = (a) => isArray(a) ? a : [a];
@@ -64,7 +65,10 @@ module.exports.getParentDirectory = (filePath) => {
64
65
 
65
66
  module.exports.findFile = findFile;
66
67
 
67
- function isExcluded(name, base, exclude) {
68
+ function isExcluded({path, name, base, exclude}) {
69
+ if (isFn(exclude))
70
+ return exclude(path);
71
+
68
72
  for (const currentExclude of exclude) {
69
73
  if (name === currentExclude || getRegExp(currentExclude).test(base))
70
74
  return true;
@@ -85,10 +89,18 @@ function findFile(node, name, exclude = []) {
85
89
 
86
90
  for (const name of names) {
87
91
  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)
89
101
  continue;
90
102
 
91
- filePaths.push(filenamePath.parentPath);
103
+ filePaths.push(path);
92
104
  }
93
105
  }
94
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "5.2.0",
3
+ "version": "5.4.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout operator adds ability to filesystem referenced variables that was not defined",