@putout/operator-filesystem 6.3.0 → 6.4.1

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 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
- arrayExpression,
22
- stringLiteral,
23
- objectProperty,
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,39 @@ module.exports.renameFile = (filePath, name) => {
163
167
  maybeFS.renameFile(oldName, newFilename);
164
168
  };
165
169
 
166
- module.exports.removeFile = (filePath) => {
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
+
197
+ if (!nextParentDir)
198
+ break;
199
+
200
+ removeFile(dirPath);
201
+ dirPath = nextParentDir;
202
+ }
171
203
  };
172
204
 
173
205
  module.exports.moveFile = (filePath, dirPath) => {
@@ -237,22 +269,6 @@ function maybeRemoveFile(dirPath, filename) {
237
269
  fileToOverwrite.remove();
238
270
  }
239
271
 
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
272
  module.exports.createFile = (dirPath, name, content) => {
257
273
  maybeRemoveFile(dirPath, name);
258
274
 
@@ -281,14 +297,15 @@ module.exports.createFile = (dirPath, name, content) => {
281
297
 
282
298
  const getFiles = (dirPath) => getProperty(dirPath, 'files');
283
299
 
284
- module.exports.readDirectory = (dirPath) => {
300
+ module.exports.readDirectory = readDirectory;
301
+ function readDirectory(dirPath) {
285
302
  const fileType = getFileType(dirPath);
286
303
 
287
304
  if (fileType !== 'directory')
288
305
  return [];
289
306
 
290
307
  return getFiles(dirPath).get('value.elements');
291
- };
308
+ }
292
309
 
293
310
  module.exports.createDirectory = createDirectory;
294
311
 
@@ -391,7 +408,9 @@ module.exports.createNestedDirectory = (path, name) => {
391
408
 
392
409
  const n = directories.length;
393
410
 
394
- for (let i = directories.indexOf(lastDirectoryName) + 1; i < n; i++) {
411
+ let i = directories.indexOf(lastDirectoryName) + 1;
412
+
413
+ for (; i < n; i++) {
395
414
  const name = basename(directories[i]);
396
415
  lastDirectoryPath = createDirectory(lastDirectoryPath, name);
397
416
  }
@@ -413,7 +432,9 @@ function getRootDirectory(path) {
413
432
  }
414
433
 
415
434
  return prevPath;
416
- }module.exports.init = maybeFS.init;
435
+ }
436
+
437
+ module.exports.init = maybeFS.init;
417
438
  module.exports.deinit = maybeFS.deinit;
418
439
 
419
440
  module.exports.pause = maybeFS.pause;
@@ -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.0",
3
+ "version": "6.4.1",
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",