@putout/operator-filesystem 10.5.0 → 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
 
@@ -446,24 +464,31 @@ export function getRootDirectory(path) {
446
464
 
447
465
  export function getFile(directoryPath, name, {type} = {}) {
448
466
  const names = maybeArray(name);
449
- const files = [];
467
+ const files = new Map();
468
+ let count = 0;
469
+
470
+ for (const name of names)
471
+ files.set(name, null);
450
472
 
451
473
  for (const currentFile of readDirectory(directoryPath)) {
452
474
  const currentName = getFilename(currentFile);
475
+ const base = basename(currentName);
453
476
 
454
- if (!names.includes(basename(currentName)))
477
+ if (!names.includes(base))
455
478
  continue;
456
479
 
457
480
  if (type && type !== getFileType(currentFile))
458
481
  continue;
459
482
 
460
- files.push(currentFile);
483
+ files.set(base, currentFile);
484
+
485
+ ++count;
461
486
 
462
- if (names.length === files.length)
487
+ if (names.length === count)
463
488
  break;
464
489
  }
465
490
 
466
- return files;
491
+ return files.values();
467
492
  }
468
493
 
469
494
  export const {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "10.5.0",
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",