@putout/operator-filesystem 10.5.1 → 10.6.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
@@ -83,6 +83,8 @@ readDirectory(dirPath);
83
83
 
84
84
  ### `findFile(directoryPath: DirectoryPath, name: string | string[], exclude?: string[]): (FilePath | DirectoryPath)[]`
85
85
 
86
+ Traverse filesystem to search one or more files:
87
+
86
88
  ```js
87
89
  const {operator} = require('putout');
88
90
  const {findFile} = operator;
@@ -115,7 +117,25 @@ And even search for a directory:
115
117
  import {operator} from 'putout';
116
118
 
117
119
  const {findFile} = operator;
118
- const coupleFiles = findFile(ast, ['/home/coderaiser', '/home/coderaiser/putout']);
120
+ const coupleFiles = findFile(root, ['/home/coderaiser', '/home/coderaiser/putout']);
121
+ ```
122
+
123
+ Each 🐊**Putout** plugin should use `findFile` independently since AST can change: files renamed, removed etc.
124
+ Anyways inside one plugin while you applying changes related to one plugin you can speed things drastically crawling file system once. It works good if plugin do not mutates the file tree, only file content.
125
+
126
+ ☝️ *`findFile` expensive, when you need to call it often use `crawlDirectory()`*
127
+
128
+ ### `crawlDirectory(directoryPath: directoryPath): CrawledFilesystem`
129
+
130
+ ```js
131
+ import {operator} from 'putout';
132
+
133
+ const {findFile} = operator;
134
+ const crawled = crawlDirectory(rootPath);
135
+
136
+ const [file] = findFile(root, 'hello', {
137
+ crawled,
138
+ });
119
139
  ```
120
140
 
121
141
  ### `getFile(directoryPath: DirectoryPath, name: string | string[], options?: Options): FilePath[]`
package/lib/filesystem.js CHANGED
@@ -86,13 +86,31 @@ function isExcluded({name, base, exclude}) {
86
86
  return false;
87
87
  }
88
88
 
89
- export function findFile(node, name, exclude = []) {
89
+ export const crawlDirectory = (a) => traverseProperties(a, 'filename');
90
+
91
+ function parseFindFileOptions(options) {
92
+ if (!options)
93
+ return {
94
+ excluded: [],
95
+ };
96
+
97
+ if (isArray(options))
98
+ return {
99
+ exclude: options,
100
+ };
101
+
102
+ return options;
103
+ }
104
+
105
+ export function findFile(node, name, options) {
106
+ const {exclude = [], crawled = crawlDirectory(node)} = parseFindFileOptions(options);
107
+
90
108
  checkName(name);
91
109
 
92
110
  const filePaths = [];
93
111
  const names = maybeArray(name);
94
112
 
95
- for (const filenamePath of traverseProperties(node, 'filename')) {
113
+ for (const filenamePath of crawled) {
96
114
  const {value} = filenamePath.node.value;
97
115
  const base = basename(value);
98
116
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "10.5.1",
3
+ "version": "10.6.0",
4
4
  "type": "module",
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",