@putout/operator-filesystem 6.3.0 → 6.4.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 +14 -0
- package/lib/filesystem.js +41 -24
- package/lib/property.js +33 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -67,6 +67,20 @@ const {
|
|
|
67
67
|
const newDirectoryPath = createNestedDirectory(ast, '/hello/world');
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
+
### `removeEmptyDirectory(DirectoryPath)`
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
const {operator} = require('putout');
|
|
74
|
+
const {
|
|
75
|
+
removeEmptyDirectory,
|
|
76
|
+
createNestedDirectory,
|
|
77
|
+
} = operator;
|
|
78
|
+
|
|
79
|
+
const newDirectoryPath = createNestedDirectory(ast, '/hello/world');
|
|
80
|
+
|
|
81
|
+
removeEmptyDirectory(newDirectoryPath);
|
|
82
|
+
```
|
|
83
|
+
|
|
70
84
|
### `readDirectory(directoryPath: DirectoryPath): (FilePath | DirectoryPath)[]`
|
|
71
85
|
|
|
72
86
|
```js
|
package/lib/filesystem.js
CHANGED
|
@@ -18,9 +18,13 @@ const {
|
|
|
18
18
|
const maybeFS = require('./maybe-fs');
|
|
19
19
|
|
|
20
20
|
const {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
createTypeProperty,
|
|
22
|
+
createFilesProperty,
|
|
23
|
+
createFilenameProperty,
|
|
24
|
+
createContentProperty,
|
|
25
|
+
} = require('./property');
|
|
26
|
+
|
|
27
|
+
const {
|
|
24
28
|
isProgram,
|
|
25
29
|
objectExpression,
|
|
26
30
|
} = types;
|
|
@@ -163,11 +167,35 @@ module.exports.renameFile = (filePath, name) => {
|
|
|
163
167
|
maybeFS.renameFile(oldName, newFilename);
|
|
164
168
|
};
|
|
165
169
|
|
|
166
|
-
module.exports.removeFile =
|
|
170
|
+
module.exports.removeFile = removeFile;
|
|
171
|
+
function removeFile(filePath) {
|
|
167
172
|
const filename = getFilename(filePath);
|
|
168
173
|
|
|
174
|
+
if (!getParentDirectory(filePath))
|
|
175
|
+
return;
|
|
176
|
+
|
|
169
177
|
filePath.remove();
|
|
170
178
|
maybeFS.removeFile(filename);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
module.exports.removeEmptyDirectory = (dirPath) => {
|
|
182
|
+
const type = getFileType(dirPath);
|
|
183
|
+
|
|
184
|
+
if (type !== 'directory')
|
|
185
|
+
return;
|
|
186
|
+
|
|
187
|
+
let nextParentDir = dirPath;
|
|
188
|
+
|
|
189
|
+
while (!readDirectory(dirPath).length) {
|
|
190
|
+
const name = getFilename(dirPath);
|
|
191
|
+
|
|
192
|
+
if (name === '/')
|
|
193
|
+
break;
|
|
194
|
+
|
|
195
|
+
nextParentDir = getParentDirectory(dirPath);
|
|
196
|
+
removeFile(dirPath);
|
|
197
|
+
dirPath = nextParentDir;
|
|
198
|
+
}
|
|
171
199
|
};
|
|
172
200
|
|
|
173
201
|
module.exports.moveFile = (filePath, dirPath) => {
|
|
@@ -237,22 +265,6 @@ function maybeRemoveFile(dirPath, filename) {
|
|
|
237
265
|
fileToOverwrite.remove();
|
|
238
266
|
}
|
|
239
267
|
|
|
240
|
-
const createTypeProperty = (type) => objectProperty(stringLiteral('type'), stringLiteral(type));
|
|
241
|
-
|
|
242
|
-
module.exports.createTypeProperty = createTypeProperty;
|
|
243
|
-
|
|
244
|
-
const createFilesProperty = (files) => objectProperty(stringLiteral('files'), arrayExpression(files));
|
|
245
|
-
|
|
246
|
-
module.exports.createFilesProperty = createFilesProperty;
|
|
247
|
-
|
|
248
|
-
const createFilenameProperty = (filename) => objectProperty(stringLiteral('filename'), stringLiteral(filename));
|
|
249
|
-
|
|
250
|
-
module.exports.createFilenameProperty = createFilenameProperty;
|
|
251
|
-
|
|
252
|
-
const createContentProperty = (content) => objectProperty(stringLiteral('content'), stringLiteral(content));
|
|
253
|
-
|
|
254
|
-
module.exports.createContentProperty = createContentProperty;
|
|
255
|
-
|
|
256
268
|
module.exports.createFile = (dirPath, name, content) => {
|
|
257
269
|
maybeRemoveFile(dirPath, name);
|
|
258
270
|
|
|
@@ -281,14 +293,15 @@ module.exports.createFile = (dirPath, name, content) => {
|
|
|
281
293
|
|
|
282
294
|
const getFiles = (dirPath) => getProperty(dirPath, 'files');
|
|
283
295
|
|
|
284
|
-
module.exports.readDirectory =
|
|
296
|
+
module.exports.readDirectory = readDirectory;
|
|
297
|
+
function readDirectory(dirPath) {
|
|
285
298
|
const fileType = getFileType(dirPath);
|
|
286
299
|
|
|
287
300
|
if (fileType !== 'directory')
|
|
288
301
|
return [];
|
|
289
302
|
|
|
290
303
|
return getFiles(dirPath).get('value.elements');
|
|
291
|
-
}
|
|
304
|
+
}
|
|
292
305
|
|
|
293
306
|
module.exports.createDirectory = createDirectory;
|
|
294
307
|
|
|
@@ -391,7 +404,9 @@ module.exports.createNestedDirectory = (path, name) => {
|
|
|
391
404
|
|
|
392
405
|
const n = directories.length;
|
|
393
406
|
|
|
394
|
-
|
|
407
|
+
let i = directories.indexOf(lastDirectoryName) + 1;
|
|
408
|
+
|
|
409
|
+
for (; i < n; i++) {
|
|
395
410
|
const name = basename(directories[i]);
|
|
396
411
|
lastDirectoryPath = createDirectory(lastDirectoryPath, name);
|
|
397
412
|
}
|
|
@@ -413,7 +428,9 @@ function getRootDirectory(path) {
|
|
|
413
428
|
}
|
|
414
429
|
|
|
415
430
|
return prevPath;
|
|
416
|
-
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
module.exports.init = maybeFS.init;
|
|
417
434
|
module.exports.deinit = maybeFS.deinit;
|
|
418
435
|
|
|
419
436
|
module.exports.pause = maybeFS.pause;
|
package/lib/property.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('@putout/babel');
|
|
4
|
+
const {
|
|
5
|
+
arrayExpression,
|
|
6
|
+
stringLiteral,
|
|
7
|
+
objectProperty,
|
|
8
|
+
} = types;
|
|
9
|
+
|
|
10
|
+
module.exports.createTypeProperty = (type) => {
|
|
11
|
+
const value = stringLiteral(type);
|
|
12
|
+
return createProperty('type', value);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
module.exports.createFilesProperty = (files) => {
|
|
16
|
+
const value = arrayExpression(files);
|
|
17
|
+
return createProperty('files', value);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports.createFilenameProperty = (filename) => {
|
|
21
|
+
const value = stringLiteral(filename);
|
|
22
|
+
return createProperty('filename', value);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
module.exports.createContentProperty = (content) => {
|
|
26
|
+
const value = stringLiteral(content);
|
|
27
|
+
return createProperty('content', value);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function createProperty(name, value) {
|
|
31
|
+
const key = stringLiteral(name);
|
|
32
|
+
return objectProperty(key, value);
|
|
33
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-filesystem",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.4.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",
|