@putout/operator-filesystem 6.0.2 → 6.1.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 +17 -6
- package/lib/filesystem.js +28 -23
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -37,7 +37,23 @@ const [dirPath] = findFile(ast, 'hello');
|
|
|
37
37
|
const newDirectoryPath = createDirectory(dirPath, 'world');
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
### `
|
|
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
|
+
readDirectory(dirPath);
|
|
52
|
+
// returns list of files
|
|
53
|
+
[];
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### `findFile(directoryPath: DirectoryPath, name: string | string[], exclude?: string[]): (FilePath | DirectoryPath)[]`
|
|
41
57
|
|
|
42
58
|
```js
|
|
43
59
|
const {operator} = require('putout');
|
|
@@ -63,11 +79,6 @@ import {operator} from 'putout';
|
|
|
63
79
|
const {findFile, getFilename} = operator;
|
|
64
80
|
|
|
65
81
|
const coupleFilesWithExcludeArray = findFile(ast, '*.ts', ['*.d.ts']);
|
|
66
|
-
|
|
67
|
-
const coupleFilesWithExcludeFn = findFile(ast, '*.ts', (filePath) => {
|
|
68
|
-
const name = getFilename(filePath);
|
|
69
|
-
return !name.endsWith('*.d.ts');
|
|
70
|
-
});
|
|
71
82
|
```
|
|
72
83
|
|
|
73
84
|
And even search for a directory:
|
package/lib/filesystem.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {join, basename} = require('node:path');
|
|
4
|
-
const tryCatch = require('try-catch');
|
|
5
4
|
const {types} = require('@putout/babel');
|
|
5
|
+
const tryCatch = require('try-catch');
|
|
6
6
|
|
|
7
7
|
const {
|
|
8
8
|
setLiteralValue,
|
|
@@ -11,7 +11,15 @@ const {
|
|
|
11
11
|
} = require('@putout/operate');
|
|
12
12
|
|
|
13
13
|
const maybeFS = require('./maybe-fs');
|
|
14
|
-
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
arrayExpression,
|
|
17
|
+
stringLiteral,
|
|
18
|
+
objectProperty,
|
|
19
|
+
isProgram,
|
|
20
|
+
objectExpression,
|
|
21
|
+
} = types;
|
|
22
|
+
|
|
15
23
|
const isString = (a) => typeof a === 'string';
|
|
16
24
|
const {isArray} = Array;
|
|
17
25
|
const maybeArray = (a) => isArray(a) ? a : [a];
|
|
@@ -34,14 +42,6 @@ const fromBase64 = (content) => {
|
|
|
34
42
|
return content;
|
|
35
43
|
};
|
|
36
44
|
|
|
37
|
-
const {
|
|
38
|
-
ObjectExpression,
|
|
39
|
-
ArrayExpression,
|
|
40
|
-
StringLiteral,
|
|
41
|
-
ObjectProperty,
|
|
42
|
-
isProgram,
|
|
43
|
-
} = types;
|
|
44
|
-
|
|
45
45
|
const getRegExp = (wildcard) => {
|
|
46
46
|
const escaped = wildcard
|
|
47
47
|
.replace(/\./g, '\\.')
|
|
@@ -65,10 +65,7 @@ module.exports.getParentDirectory = (filePath) => {
|
|
|
65
65
|
|
|
66
66
|
module.exports.findFile = findFile;
|
|
67
67
|
|
|
68
|
-
function isExcluded({
|
|
69
|
-
if (isFn(exclude))
|
|
70
|
-
return exclude(path);
|
|
71
|
-
|
|
68
|
+
function isExcluded({name, base, exclude}) {
|
|
72
69
|
for (const currentExclude of exclude) {
|
|
73
70
|
if (name === currentExclude || getRegExp(currentExclude).test(base))
|
|
74
71
|
return true;
|
|
@@ -91,7 +88,6 @@ function findFile(node, name, exclude = []) {
|
|
|
91
88
|
if (value === name || getRegExp(name).test(base)) {
|
|
92
89
|
const path = filenamePath.parentPath;
|
|
93
90
|
const excluded = isExcluded({
|
|
94
|
-
path,
|
|
95
91
|
name,
|
|
96
92
|
base,
|
|
97
93
|
exclude,
|
|
@@ -173,7 +169,7 @@ module.exports.moveFile = (filePath, dirPath) => {
|
|
|
173
169
|
|
|
174
170
|
const dirname = getFilename(dirPath);
|
|
175
171
|
const filename = getFilename(filePath);
|
|
176
|
-
const dirPathFiles =
|
|
172
|
+
const dirPathFiles = getFiles(dirPath);
|
|
177
173
|
const filenamePath = getProperty(filePath, 'filename');
|
|
178
174
|
|
|
179
175
|
const basename = filename
|
|
@@ -202,7 +198,7 @@ module.exports.copyFile = (filePath, dirPath) => {
|
|
|
202
198
|
const newFilename = join(dirname, basename);
|
|
203
199
|
const [hasContent, content] = getFileContent(filePath);
|
|
204
200
|
|
|
205
|
-
const copiedFile =
|
|
201
|
+
const copiedFile = objectExpression([
|
|
206
202
|
createTypeProperty('file'),
|
|
207
203
|
createFilenameProperty(newFilename),
|
|
208
204
|
hasContent && createContentProperty(content),
|
|
@@ -234,19 +230,19 @@ function maybeRemoveFile(dirPath, filename) {
|
|
|
234
230
|
fileToOverwrite.remove();
|
|
235
231
|
}
|
|
236
232
|
|
|
237
|
-
const createTypeProperty = (type) =>
|
|
233
|
+
const createTypeProperty = (type) => objectProperty(stringLiteral('type'), stringLiteral(type));
|
|
238
234
|
|
|
239
235
|
module.exports.createTypeProperty = createTypeProperty;
|
|
240
236
|
|
|
241
|
-
const createFilesProperty = (files) =>
|
|
237
|
+
const createFilesProperty = (files) => objectProperty(stringLiteral('files'), arrayExpression(files));
|
|
242
238
|
|
|
243
239
|
module.exports.createFilesProperty = createFilesProperty;
|
|
244
240
|
|
|
245
|
-
const createFilenameProperty = (filename) =>
|
|
241
|
+
const createFilenameProperty = (filename) => objectProperty(stringLiteral('filename'), stringLiteral(filename));
|
|
246
242
|
|
|
247
243
|
module.exports.createFilenameProperty = createFilenameProperty;
|
|
248
244
|
|
|
249
|
-
const createContentProperty = (content) =>
|
|
245
|
+
const createContentProperty = (content) => objectProperty(stringLiteral('content'), stringLiteral(content));
|
|
250
246
|
|
|
251
247
|
module.exports.createContentProperty = createContentProperty;
|
|
252
248
|
|
|
@@ -266,7 +262,7 @@ module.exports.createFile = (dirPath, name, content) => {
|
|
|
266
262
|
content && createContentProperty(content),
|
|
267
263
|
].filter(Boolean);
|
|
268
264
|
|
|
269
|
-
dirPathFiles.node.value.elements.push(
|
|
265
|
+
dirPathFiles.node.value.elements.push(objectExpression(properties));
|
|
270
266
|
|
|
271
267
|
const filePath = dirPathFiles.get('value.elements').at(-1);
|
|
272
268
|
|
|
@@ -278,6 +274,15 @@ module.exports.createFile = (dirPath, name, content) => {
|
|
|
278
274
|
|
|
279
275
|
const getFiles = (dirPath) => getProperty(dirPath, 'files');
|
|
280
276
|
|
|
277
|
+
module.exports.readDirectory = (dirPath) => {
|
|
278
|
+
const fileType = getFileType(dirPath);
|
|
279
|
+
|
|
280
|
+
if (fileType !== 'directory')
|
|
281
|
+
return [];
|
|
282
|
+
|
|
283
|
+
return getFiles(dirPath).get('value.elements');
|
|
284
|
+
};
|
|
285
|
+
|
|
281
286
|
module.exports.createDirectory = (dirPath, name) => {
|
|
282
287
|
const dirPathFiles = getFiles(dirPath);
|
|
283
288
|
const parentFilename = getFilename(dirPath);
|
|
@@ -287,7 +292,7 @@ module.exports.createDirectory = (dirPath, name) => {
|
|
|
287
292
|
const filesProperty = createFilesProperty([]);
|
|
288
293
|
const filenameProperty = createFilenameProperty(filename);
|
|
289
294
|
|
|
290
|
-
dirPathFiles.node.value.elements.push(
|
|
295
|
+
dirPathFiles.node.value.elements.push(objectExpression([
|
|
291
296
|
typeProperty,
|
|
292
297
|
filenameProperty,
|
|
293
298
|
filesProperty,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-filesystem",
|
|
3
|
-
"version": "6.0
|
|
3
|
+
"version": "6.1.0",
|
|
4
4
|
"type": "commonjs",
|
|
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",
|
|
@@ -37,13 +37,13 @@
|
|
|
37
37
|
],
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@putout/engine-parser": "^12.0.0",
|
|
40
|
+
"@putout/eslint-flat": "^2.0.0",
|
|
40
41
|
"@putout/operator-json": "^2.0.0",
|
|
41
|
-
"@putout/test": "^
|
|
42
|
+
"@putout/test": "^12.0.0",
|
|
42
43
|
"c8": "^10.0.0",
|
|
43
44
|
"eslint": "^9.0.0",
|
|
44
45
|
"eslint-plugin-n": "^17.0.0",
|
|
45
|
-
"eslint-plugin-putout": "^
|
|
46
|
-
"lerna": "^6.0.1",
|
|
46
|
+
"eslint-plugin-putout": "^25.0.1",
|
|
47
47
|
"madrun": "^10.0.0",
|
|
48
48
|
"montag": "^1.2.1",
|
|
49
49
|
"nodemon": "^3.0.1",
|