@putout/operator-filesystem 10.5.1 β 11.0.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 +69 -1
- package/lib/filesystem.js +23 -9
- package/lib/maybe-fs.js +5 -5
- package/package.json +5 -1
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(
|
|
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): Crawled`
|
|
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[]`
|
|
@@ -275,6 +295,54 @@ Since `basename` is used.
|
|
|
275
295
|
|
|
276
296
|
To move file use [`moveFile()`](#movefilefilepath-filepath-dirpath-filepath).
|
|
277
297
|
|
|
298
|
+
### `inject`
|
|
299
|
+
|
|
300
|
+
Inject filesystem API with methods:
|
|
301
|
+
|
|
302
|
+
- β
`renameFile`;
|
|
303
|
+
- β
`copyFile`;
|
|
304
|
+
- β
`removeFile;
|
|
305
|
+
- β
`createDirectory;
|
|
306
|
+
- β
`readFileContent;
|
|
307
|
+
- β
`writeFileContent;
|
|
308
|
+
|
|
309
|
+
To have ability to interact with any kind of filesystem representation.
|
|
310
|
+
|
|
311
|
+
```js
|
|
312
|
+
import {inject} from '@putout/operator-filesystem/maybe';
|
|
313
|
+
import * as filesystemCLI from '@putout/cli-filesystem';
|
|
314
|
+
|
|
315
|
+
inject(filesystemCLI);
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### `eject`
|
|
319
|
+
|
|
320
|
+
```js
|
|
321
|
+
import {eject} from '@putout/operator-filesystem/maybe';
|
|
322
|
+
|
|
323
|
+
eject();
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### `pause`
|
|
327
|
+
|
|
328
|
+
Pause currently injected filesystem API
|
|
329
|
+
|
|
330
|
+
```js
|
|
331
|
+
import {pause} from '@putout/operator-filesystem/maybe';
|
|
332
|
+
|
|
333
|
+
pause();
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### `start`
|
|
337
|
+
|
|
338
|
+
Start currently paused injected filesystem API.
|
|
339
|
+
|
|
340
|
+
```js
|
|
341
|
+
import {start} from '@putout/operator-filesystem/maybe';
|
|
342
|
+
|
|
343
|
+
start();
|
|
344
|
+
```
|
|
345
|
+
|
|
278
346
|
## Example
|
|
279
347
|
|
|
280
348
|
```js
|
package/lib/filesystem.js
CHANGED
|
@@ -86,13 +86,34 @@ function isExcluded({name, base, exclude}) {
|
|
|
86
86
|
return false;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
export
|
|
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 {
|
|
107
|
+
exclude = [],
|
|
108
|
+
crawled = crawlDirectory(node),
|
|
109
|
+
} = parseFindFileOptions(options);
|
|
110
|
+
|
|
90
111
|
checkName(name);
|
|
91
112
|
|
|
92
113
|
const filePaths = [];
|
|
93
114
|
const names = maybeArray(name);
|
|
94
115
|
|
|
95
|
-
for (const filenamePath of
|
|
116
|
+
for (const filenamePath of crawled) {
|
|
96
117
|
const {value} = filenamePath.node.value;
|
|
97
118
|
const base = basename(value);
|
|
98
119
|
|
|
@@ -472,10 +493,3 @@ export function getFile(directoryPath, name, {type} = {}) {
|
|
|
472
493
|
|
|
473
494
|
return files.values();
|
|
474
495
|
}
|
|
475
|
-
|
|
476
|
-
export const {
|
|
477
|
-
init,
|
|
478
|
-
deinit,
|
|
479
|
-
pause,
|
|
480
|
-
start,
|
|
481
|
-
} = maybeFS;
|
package/lib/maybe-fs.js
CHANGED
|
@@ -41,19 +41,19 @@ export const writeFileContent = (name, content) => {
|
|
|
41
41
|
maybeFS.writeFileContent(name, content);
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
export
|
|
44
|
+
export const inject = (fsDriver) => {
|
|
45
45
|
assign(maybeFS, fsDriver);
|
|
46
|
-
}
|
|
46
|
+
};
|
|
47
47
|
|
|
48
48
|
export const pause = () => {
|
|
49
49
|
driverStore(maybeFS);
|
|
50
|
-
|
|
50
|
+
eject();
|
|
51
51
|
};
|
|
52
52
|
|
|
53
53
|
export const start = () => {
|
|
54
|
-
|
|
54
|
+
inject(driverStore());
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
-
export function
|
|
57
|
+
export function eject() {
|
|
58
58
|
assign(maybeFS, defaultFS);
|
|
59
59
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-filesystem",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.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",
|
|
7
7
|
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/operator-filesystem#readme",
|
|
8
8
|
"main": "lib/filesystem.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./lib/filesystem.js",
|
|
11
|
+
"./maybe": "./lib/maybe-fs.js"
|
|
12
|
+
},
|
|
9
13
|
"release": false,
|
|
10
14
|
"tag": false,
|
|
11
15
|
"changelog": false,
|