@putout/operator-filesystem 5.0.0 → 5.2.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[]): (FilePath | DirectoryPath)[]`
40
+ ### `findFile(directoryPath: DirectoryPath, name: string | string[], exclude?: string[]): (FilePath | DirectoryPath)[]`
41
41
 
42
42
  ```js
43
43
  const {operator} = require('putout');
@@ -55,6 +55,15 @@ const {findFile} = operator;
55
55
  const coupleFiles = findFile(ast, ['*.js', '*.ts']);
56
56
  ```
57
57
 
58
+ Or `exclude` some files:
59
+
60
+ ```js
61
+ import {operator} from 'putout';
62
+
63
+ const {findFile} = operator;
64
+ const coupleFiles = findFile(ast, '*.ts', ['*.d.ts']);
65
+ ```
66
+
58
67
  And even search for a directory:
59
68
 
60
69
  ```js
package/lib/filesystem.js CHANGED
@@ -51,6 +51,9 @@ const getRegExp = (wildcard) => {
51
51
  };
52
52
 
53
53
  module.exports.getParentDirectory = (filePath) => {
54
+ if (!filePath.parentPath)
55
+ return null;
56
+
54
57
  const {parentPath} = filePath.parentPath.parentPath;
55
58
 
56
59
  if (isProgram(parentPath))
@@ -61,7 +64,16 @@ module.exports.getParentDirectory = (filePath) => {
61
64
 
62
65
  module.exports.findFile = findFile;
63
66
 
64
- function findFile(node, name) {
67
+ function isExcluded(name, base, exclude) {
68
+ for (const currentExclude of exclude) {
69
+ if (name === currentExclude || getRegExp(currentExclude).test(base))
70
+ return true;
71
+ }
72
+
73
+ return false;
74
+ }
75
+
76
+ function findFile(node, name, exclude = []) {
65
77
  checkName(name);
66
78
 
67
79
  const filePaths = [];
@@ -72,8 +84,12 @@ function findFile(node, name) {
72
84
  const base = basename(value);
73
85
 
74
86
  for (const name of names) {
75
- if (value === name || getRegExp(name).test(base))
87
+ if (value === name || getRegExp(name).test(base)) {
88
+ if (isExcluded(name, base, exclude))
89
+ continue;
90
+
76
91
  filePaths.push(filenamePath.parentPath);
92
+ }
77
93
  }
78
94
  }
79
95
 
@@ -240,9 +256,7 @@ module.exports.createFile = (dirPath, name, content) => {
240
256
 
241
257
  dirPathFiles.node.value.elements.push(ObjectExpression(properties));
242
258
 
243
- const filePath = dirPathFiles
244
- .get('value.elements')
245
- .at(-1);
259
+ const filePath = dirPathFiles.get('value.elements').at(-1);
246
260
 
247
261
  if (isString(content))
248
262
  writeFileContent(filePath, content);
@@ -250,9 +264,7 @@ module.exports.createFile = (dirPath, name, content) => {
250
264
  return filePath;
251
265
  };
252
266
 
253
- function getFiles(dirPath) {
254
- return getProperty(dirPath, 'files');
255
- }
267
+ const getFiles = (dirPath) => getProperty(dirPath, 'files');
256
268
 
257
269
  module.exports.createDirectory = (dirPath, name) => {
258
270
  const dirPathFiles = getFiles(dirPath);
@@ -271,9 +283,7 @@ module.exports.createDirectory = (dirPath, name) => {
271
283
 
272
284
  maybeFS.createDirectory(filename);
273
285
 
274
- return dirPathFiles
275
- .get('value.elements')
276
- .at(-1);
286
+ return dirPathFiles.get('value.elements').at(-1);
277
287
  };
278
288
 
279
289
  module.exports.readFileContent = (filePath) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "5.0.0",
3
+ "version": "5.2.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"