@putout/operator-filesystem 5.1.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];
@@ -51,6 +52,9 @@ const getRegExp = (wildcard) => {
51
52
  };
52
53
 
53
54
  module.exports.getParentDirectory = (filePath) => {
55
+ if (!filePath.parentPath)
56
+ return null;
57
+
54
58
  const {parentPath} = filePath.parentPath.parentPath;
55
59
 
56
60
  if (isProgram(parentPath))
@@ -61,7 +65,10 @@ module.exports.getParentDirectory = (filePath) => {
61
65
 
62
66
  module.exports.findFile = findFile;
63
67
 
64
- function isExcluded(name, base, exclude) {
68
+ function isExcluded({path, name, base, exclude}) {
69
+ if (isFn(exclude))
70
+ return exclude(path);
71
+
65
72
  for (const currentExclude of exclude) {
66
73
  if (name === currentExclude || getRegExp(currentExclude).test(base))
67
74
  return true;
@@ -82,10 +89,18 @@ function findFile(node, name, exclude = []) {
82
89
 
83
90
  for (const name of names) {
84
91
  if (value === name || getRegExp(name).test(base)) {
85
- 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)
86
101
  continue;
87
102
 
88
- filePaths.push(filenamePath.parentPath);
103
+ filePaths.push(path);
89
104
  }
90
105
  }
91
106
  }
@@ -253,9 +268,7 @@ module.exports.createFile = (dirPath, name, content) => {
253
268
 
254
269
  dirPathFiles.node.value.elements.push(ObjectExpression(properties));
255
270
 
256
- const filePath = dirPathFiles
257
- .get('value.elements')
258
- .at(-1);
271
+ const filePath = dirPathFiles.get('value.elements').at(-1);
259
272
 
260
273
  if (isString(content))
261
274
  writeFileContent(filePath, content);
@@ -263,9 +276,7 @@ module.exports.createFile = (dirPath, name, content) => {
263
276
  return filePath;
264
277
  };
265
278
 
266
- function getFiles(dirPath) {
267
- return getProperty(dirPath, 'files');
268
- }
279
+ const getFiles = (dirPath) => getProperty(dirPath, 'files');
269
280
 
270
281
  module.exports.createDirectory = (dirPath, name) => {
271
282
  const dirPathFiles = getFiles(dirPath);
@@ -284,9 +295,7 @@ module.exports.createDirectory = (dirPath, name) => {
284
295
 
285
296
  maybeFS.createDirectory(filename);
286
297
 
287
- return dirPathFiles
288
- .get('value.elements')
289
- .at(-1);
298
+ return dirPathFiles.get('value.elements').at(-1);
290
299
  };
291
300
 
292
301
  module.exports.readFileContent = (filePath) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "5.1.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",
@@ -24,7 +24,7 @@
24
24
  "report": "madrun report"
25
25
  },
26
26
  "dependencies": {
27
- "@putout/babel": "^2.0.0",
27
+ "@putout/babel": "^3.0.0",
28
28
  "@putout/operate": "^12.0.0",
29
29
  "fullstore": "^3.0.0",
30
30
  "try-catch": "^3.0.1"