@putout/operator-filesystem 1.1.0 → 1.3.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 CHANGED
@@ -40,6 +40,15 @@ const {removeFile} = operator;
40
40
  removeFile(filePath);
41
41
  ```
42
42
 
43
+ ### `moveFile(filePath: Path, dirPath: Path)`
44
+
45
+ ```js
46
+ const {operator} = require('putout');
47
+ const {moveFile} = operator;
48
+
49
+ moveFile(filePath, dirPath);
50
+ ```
51
+
43
52
  ### `renameFile(filePath: Path, name: string)`
44
53
 
45
54
  ```js
package/lib/filesystem.js CHANGED
@@ -8,13 +8,22 @@ const {
8
8
 
9
9
  const maybeFS = require('./maybe-fs');
10
10
 
11
+ const getRegExp = (wildcard) => {
12
+ const escaped = wildcard
13
+ .replace(/\./g, '\\.')
14
+ .replace(/\*/g, '.*')
15
+ .replace('?', '.?');
16
+
17
+ return RegExp(`${escaped}$`);
18
+ };
19
+
11
20
  module.exports.findFile = (node, name) => {
12
21
  const filePaths = [];
13
22
 
14
23
  for (const filenamePath of traverseProperties(node, 'filename')) {
15
24
  const {value} = filenamePath.node.value;
16
25
 
17
- if (value === name || value.endsWith(`/${name}`)) {
26
+ if (value === name || getRegExp(name).test(value)) {
18
27
  filePaths.push(filenamePath.parentPath);
19
28
  }
20
29
  }
@@ -24,7 +33,6 @@ module.exports.findFile = (node, name) => {
24
33
 
25
34
  function getFilenamePath(filePath) {
26
35
  const filenamePath = getProperty(filePath, 'filename');
27
-
28
36
  return filenamePath.get('value');
29
37
  }
30
38
 
@@ -55,5 +63,20 @@ module.exports.removeFile = (filePath) => {
55
63
  maybeFS.removeFile(filename);
56
64
  };
57
65
 
66
+ module.exports.moveFile = (filePath, dirPath) => {
67
+ const dirname = getFilename(dirPath);
68
+ const filename = getFilename(filePath);
69
+ const dirPathFiles = getProperty(dirPath, 'files');
70
+
71
+ dirPathFiles.node.value.elements.push(filePath.node);
72
+ filePath.remove();
73
+
74
+ const basename = filename
75
+ .split('/')
76
+ .pop();
77
+
78
+ maybeFS.renameFile(filename, `${dirname}/${basename}`);
79
+ };
80
+
58
81
  module.exports.init = maybeFS.init;
59
82
  module.exports.deinit = maybeFS.deinit;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "1.1.0",
3
+ "version": "1.3.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",