@putout/operator-filesystem 6.0.3 → 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 +16 -0
- package/lib/filesystem.js +26 -17
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -37,6 +37,22 @@ 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
|
+
readDirectory(dirPath);
|
|
52
|
+
// returns list of files
|
|
53
|
+
[];
|
|
54
|
+
```
|
|
55
|
+
|
|
40
56
|
### `findFile(directoryPath: DirectoryPath, name: string | string[], exclude?: string[]): (FilePath | DirectoryPath)[]`
|
|
41
57
|
|
|
42
58
|
```js
|
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,
|
|
@@ -12,6 +12,14 @@ const {
|
|
|
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, '\\.')
|
|
@@ -169,7 +169,7 @@ module.exports.moveFile = (filePath, dirPath) => {
|
|
|
169
169
|
|
|
170
170
|
const dirname = getFilename(dirPath);
|
|
171
171
|
const filename = getFilename(filePath);
|
|
172
|
-
const dirPathFiles =
|
|
172
|
+
const dirPathFiles = getFiles(dirPath);
|
|
173
173
|
const filenamePath = getProperty(filePath, 'filename');
|
|
174
174
|
|
|
175
175
|
const basename = filename
|
|
@@ -198,7 +198,7 @@ module.exports.copyFile = (filePath, dirPath) => {
|
|
|
198
198
|
const newFilename = join(dirname, basename);
|
|
199
199
|
const [hasContent, content] = getFileContent(filePath);
|
|
200
200
|
|
|
201
|
-
const copiedFile =
|
|
201
|
+
const copiedFile = objectExpression([
|
|
202
202
|
createTypeProperty('file'),
|
|
203
203
|
createFilenameProperty(newFilename),
|
|
204
204
|
hasContent && createContentProperty(content),
|
|
@@ -230,19 +230,19 @@ function maybeRemoveFile(dirPath, filename) {
|
|
|
230
230
|
fileToOverwrite.remove();
|
|
231
231
|
}
|
|
232
232
|
|
|
233
|
-
const createTypeProperty = (type) =>
|
|
233
|
+
const createTypeProperty = (type) => objectProperty(stringLiteral('type'), stringLiteral(type));
|
|
234
234
|
|
|
235
235
|
module.exports.createTypeProperty = createTypeProperty;
|
|
236
236
|
|
|
237
|
-
const createFilesProperty = (files) =>
|
|
237
|
+
const createFilesProperty = (files) => objectProperty(stringLiteral('files'), arrayExpression(files));
|
|
238
238
|
|
|
239
239
|
module.exports.createFilesProperty = createFilesProperty;
|
|
240
240
|
|
|
241
|
-
const createFilenameProperty = (filename) =>
|
|
241
|
+
const createFilenameProperty = (filename) => objectProperty(stringLiteral('filename'), stringLiteral(filename));
|
|
242
242
|
|
|
243
243
|
module.exports.createFilenameProperty = createFilenameProperty;
|
|
244
244
|
|
|
245
|
-
const createContentProperty = (content) =>
|
|
245
|
+
const createContentProperty = (content) => objectProperty(stringLiteral('content'), stringLiteral(content));
|
|
246
246
|
|
|
247
247
|
module.exports.createContentProperty = createContentProperty;
|
|
248
248
|
|
|
@@ -262,7 +262,7 @@ module.exports.createFile = (dirPath, name, content) => {
|
|
|
262
262
|
content && createContentProperty(content),
|
|
263
263
|
].filter(Boolean);
|
|
264
264
|
|
|
265
|
-
dirPathFiles.node.value.elements.push(
|
|
265
|
+
dirPathFiles.node.value.elements.push(objectExpression(properties));
|
|
266
266
|
|
|
267
267
|
const filePath = dirPathFiles.get('value.elements').at(-1);
|
|
268
268
|
|
|
@@ -274,6 +274,15 @@ module.exports.createFile = (dirPath, name, content) => {
|
|
|
274
274
|
|
|
275
275
|
const getFiles = (dirPath) => getProperty(dirPath, 'files');
|
|
276
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
|
+
|
|
277
286
|
module.exports.createDirectory = (dirPath, name) => {
|
|
278
287
|
const dirPathFiles = getFiles(dirPath);
|
|
279
288
|
const parentFilename = getFilename(dirPath);
|
|
@@ -283,7 +292,7 @@ module.exports.createDirectory = (dirPath, name) => {
|
|
|
283
292
|
const filesProperty = createFilesProperty([]);
|
|
284
293
|
const filenameProperty = createFilenameProperty(filename);
|
|
285
294
|
|
|
286
|
-
dirPathFiles.node.value.elements.push(
|
|
295
|
+
dirPathFiles.node.value.elements.push(objectExpression([
|
|
287
296
|
typeProperty,
|
|
288
297
|
filenameProperty,
|
|
289
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",
|
|
@@ -39,12 +39,11 @@
|
|
|
39
39
|
"@putout/engine-parser": "^12.0.0",
|
|
40
40
|
"@putout/eslint-flat": "^2.0.0",
|
|
41
41
|
"@putout/operator-json": "^2.0.0",
|
|
42
|
-
"@putout/test": "^
|
|
42
|
+
"@putout/test": "^12.0.0",
|
|
43
43
|
"c8": "^10.0.0",
|
|
44
44
|
"eslint": "^9.0.0",
|
|
45
45
|
"eslint-plugin-n": "^17.0.0",
|
|
46
|
-
"eslint-plugin-putout": "^
|
|
47
|
-
"lerna": "^6.0.1",
|
|
46
|
+
"eslint-plugin-putout": "^25.0.1",
|
|
48
47
|
"madrun": "^10.0.0",
|
|
49
48
|
"montag": "^1.2.1",
|
|
50
49
|
"nodemon": "^3.0.1",
|