@putout/operator-filesystem 10.4.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 -20
- package/lib/filesystem.js +14 -4
- 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');
|
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,
|
|
@@ -308,7 +309,7 @@ export function readDirectory(dirPath) {
|
|
|
308
309
|
}
|
|
309
310
|
|
|
310
311
|
export function createDirectory(dirPath, name) {
|
|
311
|
-
const existed = getFile(dirPath, name);
|
|
312
|
+
const [existed] = getFile(dirPath, name);
|
|
312
313
|
|
|
313
314
|
if (existed) {
|
|
314
315
|
const fileType = getFileType(existed);
|
|
@@ -426,6 +427,9 @@ export const createNestedDirectory = (path, name) => {
|
|
|
426
427
|
};
|
|
427
428
|
|
|
428
429
|
export function getRootDirectory(path) {
|
|
430
|
+
if (path.program)
|
|
431
|
+
return getRootFromAst(path);
|
|
432
|
+
|
|
429
433
|
let currentDirPath = getParentDirectory(path);
|
|
430
434
|
|
|
431
435
|
if (!currentDirPath)
|
|
@@ -441,19 +445,25 @@ export function getRootDirectory(path) {
|
|
|
441
445
|
}
|
|
442
446
|
|
|
443
447
|
export function getFile(directoryPath, name, {type} = {}) {
|
|
448
|
+
const names = maybeArray(name);
|
|
449
|
+
const files = [];
|
|
450
|
+
|
|
444
451
|
for (const currentFile of readDirectory(directoryPath)) {
|
|
445
452
|
const currentName = getFilename(currentFile);
|
|
446
453
|
|
|
447
|
-
if (!
|
|
454
|
+
if (!names.includes(basename(currentName)))
|
|
448
455
|
continue;
|
|
449
456
|
|
|
450
457
|
if (type && type !== getFileType(currentFile))
|
|
451
458
|
continue;
|
|
452
459
|
|
|
453
|
-
|
|
460
|
+
files.push(currentFile);
|
|
461
|
+
|
|
462
|
+
if (names.length === files.length)
|
|
463
|
+
break;
|
|
454
464
|
}
|
|
455
465
|
|
|
456
|
-
return
|
|
466
|
+
return files;
|
|
457
467
|
}
|
|
458
468
|
|
|
459
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",
|