@putout/operator-filesystem 10.3.0 → 10.5.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 +12 -33
- package/lib/filesystem.js +23 -6
- package/lib/get-root-from-ast.js +14 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -37,23 +37,6 @@ const [dirPath] = findFile(ast, 'hello');
|
|
|
37
37
|
const newDirectoryPath = createDirectory(dirPath, 'world');
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
### `readDirectory(directoryPath: DirectoryPath): (FilePath | DirectoryPath)[]`
|
|
41
|
-
|
|
42
|
-
```js
|
|
43
|
-
const {operator} = require('putout');
|
|
44
|
-
const {
|
|
45
|
-
finedFiles,
|
|
46
|
-
findFile,
|
|
47
|
-
readDirectory,
|
|
48
|
-
} = operator;
|
|
49
|
-
|
|
50
|
-
const [dirPath] = findFile(ast, '/bin');
|
|
51
|
-
|
|
52
|
-
readDirectory(dirPath);
|
|
53
|
-
// returns list of files
|
|
54
|
-
[];
|
|
55
|
-
```
|
|
56
|
-
|
|
57
40
|
### `createNestedDirectory(path: FilePath|DirectoryPath, name: string): DirectoryPath`
|
|
58
41
|
|
|
59
42
|
```js
|
|
@@ -135,7 +118,7 @@ const {findFile} = operator;
|
|
|
135
118
|
const coupleFiles = findFile(ast, ['/home/coderaiser', '/home/coderaiser/putout']);
|
|
136
119
|
```
|
|
137
120
|
|
|
138
|
-
### `getFile(directoryPath: DirectoryPath, name: string, options?: Options): FilePath`
|
|
121
|
+
### `getFile(directoryPath: DirectoryPath, name: string | string[], options?: Options): FilePath[]`
|
|
139
122
|
|
|
140
123
|
```ts
|
|
141
124
|
type Options = {
|
|
@@ -149,7 +132,16 @@ Get file named `name` from `directoryPath`
|
|
|
149
132
|
const {operator} = require('putout');
|
|
150
133
|
const {getFile} = operator;
|
|
151
134
|
|
|
152
|
-
const filePath = getFile(root, 'package.json');
|
|
135
|
+
const [filePath] = getFile(root, 'package.json');
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
When you need a couple files use:
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
import {operator} from 'putout';
|
|
142
|
+
|
|
143
|
+
const {getFile} = operator;
|
|
144
|
+
const [index, indexSpec] = getFile(root, ['index.js', 'index.spec.js']);
|
|
153
145
|
```
|
|
154
146
|
|
|
155
147
|
### `getParentDirectory(path: FilePath | DirectoryPath): FilePath | null`
|
|
@@ -171,7 +163,7 @@ dirPath === getParentDirectory(newDirectoryPath);
|
|
|
171
163
|
true;
|
|
172
164
|
```
|
|
173
165
|
|
|
174
|
-
### `getRootDirectory(path: FilePath | DirectoryPath): DrectoryPath`
|
|
166
|
+
### `getRootDirectory(path: FilePath | DirectoryPath | File): DrectoryPath`
|
|
175
167
|
|
|
176
168
|
```js
|
|
177
169
|
const {operator} = require('putout');
|
|
@@ -205,19 +197,6 @@ getFileType(filePath);
|
|
|
205
197
|
'file';
|
|
206
198
|
```
|
|
207
199
|
|
|
208
|
-
### `getFileContent(path: FilePath): [is: boolean, content: string]`
|
|
209
|
-
|
|
210
|
-
Get `content` property if it exists, use [`readFileContent`](#read-file-content) to read file from **Filesystem**.
|
|
211
|
-
|
|
212
|
-
```js
|
|
213
|
-
const {operator} = require('putout');
|
|
214
|
-
const {getFileContent} = operator;
|
|
215
|
-
|
|
216
|
-
getFileContent(filePath);
|
|
217
|
-
// returns
|
|
218
|
-
[true, 'hello world'];
|
|
219
|
-
```
|
|
220
|
-
|
|
221
200
|
### `removeFile(filePath: Path)`
|
|
222
201
|
|
|
223
202
|
```js
|
package/lib/filesystem.js
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
traverseProperties,
|
|
12
12
|
} from '@putout/operate';
|
|
13
13
|
import * as maybeFS from './maybe-fs.js';
|
|
14
|
+
import {getRootFromAst} from './get-root-from-ast.js';
|
|
14
15
|
import {
|
|
15
16
|
createTypeProperty,
|
|
16
17
|
createFilesProperty,
|
|
@@ -27,6 +28,9 @@ const isString = (a) => typeof a === 'string';
|
|
|
27
28
|
const {isArray} = Array;
|
|
28
29
|
const maybeArray = (a) => isArray(a) ? a : [a];
|
|
29
30
|
|
|
31
|
+
const escape = (a) => encodeURIComponent(a).replaceAll('%', '+');
|
|
32
|
+
const unescape = (a) => decodeURIComponent(a.replaceAll('+', '%'));
|
|
33
|
+
|
|
30
34
|
const toBase64 = (content) => {
|
|
31
35
|
const [e, result] = tryCatch(btoa, content);
|
|
32
36
|
|
|
@@ -42,8 +46,12 @@ const fromBase64 = (content) => {
|
|
|
42
46
|
|
|
43
47
|
const [e, decoded] = tryCatch(atob, content);
|
|
44
48
|
|
|
45
|
-
if (!e)
|
|
46
|
-
|
|
49
|
+
if (!e) {
|
|
50
|
+
if (!decoded.includes(' ') && decoded.includes('+'))
|
|
51
|
+
return unescape(decoded);
|
|
52
|
+
|
|
53
|
+
return decoded;
|
|
54
|
+
}
|
|
47
55
|
|
|
48
56
|
return content;
|
|
49
57
|
};
|
|
@@ -301,7 +309,7 @@ export function readDirectory(dirPath) {
|
|
|
301
309
|
}
|
|
302
310
|
|
|
303
311
|
export function createDirectory(dirPath, name) {
|
|
304
|
-
const existed = getFile(dirPath, name);
|
|
312
|
+
const [existed] = getFile(dirPath, name);
|
|
305
313
|
|
|
306
314
|
if (existed) {
|
|
307
315
|
const fileType = getFileType(existed);
|
|
@@ -419,6 +427,9 @@ export const createNestedDirectory = (path, name) => {
|
|
|
419
427
|
};
|
|
420
428
|
|
|
421
429
|
export function getRootDirectory(path) {
|
|
430
|
+
if (path.program)
|
|
431
|
+
return getRootFromAst(path);
|
|
432
|
+
|
|
422
433
|
let currentDirPath = getParentDirectory(path);
|
|
423
434
|
|
|
424
435
|
if (!currentDirPath)
|
|
@@ -434,19 +445,25 @@ export function getRootDirectory(path) {
|
|
|
434
445
|
}
|
|
435
446
|
|
|
436
447
|
export function getFile(directoryPath, name, {type} = {}) {
|
|
448
|
+
const names = maybeArray(name);
|
|
449
|
+
const files = [];
|
|
450
|
+
|
|
437
451
|
for (const currentFile of readDirectory(directoryPath)) {
|
|
438
452
|
const currentName = getFilename(currentFile);
|
|
439
453
|
|
|
440
|
-
if (!
|
|
454
|
+
if (!names.includes(basename(currentName)))
|
|
441
455
|
continue;
|
|
442
456
|
|
|
443
457
|
if (type && type !== getFileType(currentFile))
|
|
444
458
|
continue;
|
|
445
459
|
|
|
446
|
-
|
|
460
|
+
files.push(currentFile);
|
|
461
|
+
|
|
462
|
+
if (names.length === files.length)
|
|
463
|
+
break;
|
|
447
464
|
}
|
|
448
465
|
|
|
449
|
-
return
|
|
466
|
+
return files;
|
|
450
467
|
}
|
|
451
468
|
|
|
452
469
|
export const {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {traverse} from '@putout/traverse';
|
|
2
|
+
import {__filesystem_name} from '@putout/operator-json';
|
|
3
|
+
|
|
4
|
+
export const getRootFromAst = (ast) => {
|
|
5
|
+
let root;
|
|
6
|
+
|
|
7
|
+
traverse(ast, {
|
|
8
|
+
[`${__filesystem_name}(__)`](path) {
|
|
9
|
+
root = path.get('arguments.0');
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
return root;
|
|
14
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-filesystem",
|
|
3
|
-
"version": "10.
|
|
3
|
+
"version": "10.5.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",
|
|
@@ -26,6 +26,8 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@putout/babel": "^5.0.0",
|
|
28
28
|
"@putout/operate": "^15.0.0",
|
|
29
|
+
"@putout/operator-json": "^3.2.0",
|
|
30
|
+
"@putout/traverse": "^16.0.0",
|
|
29
31
|
"fullstore": "^4.0.0",
|
|
30
32
|
"try-catch": "^4.0.0"
|
|
31
33
|
},
|
|
@@ -38,7 +40,6 @@
|
|
|
38
40
|
"devDependencies": {
|
|
39
41
|
"@putout/engine-parser": "^15.0.1",
|
|
40
42
|
"@putout/eslint-flat": "^4.0.0",
|
|
41
|
-
"@putout/operator-json": "^3.0.0",
|
|
42
43
|
"@putout/test": "^15.0.0",
|
|
43
44
|
"c8": "^10.0.0",
|
|
44
45
|
"eslint": "^10.0.0-alpha.0",
|