@putout/operator-filesystem 1.0.1 → 1.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 CHANGED
@@ -13,16 +13,34 @@ npm i putout @putout/operator-filesystem
13
13
 
14
14
  ## API
15
15
 
16
- ## findFiles(path: Path, name: string)
16
+ ## `findFile(path: Path, name: string)`
17
17
 
18
18
  ```js
19
19
  const {operator} = require('putout');
20
20
  const {finedFiles} = operator;
21
21
 
22
- const [filePath] = findFiles(ast, 'hello');
22
+ const [filePath] = findFile(ast, 'hello');
23
23
  ```
24
24
 
25
- ### renameFile(filePath: Path, name: string)
25
+ ### `getFilename(path: Path)`
26
+
27
+ ```js
28
+ const {operator} = require('putout');
29
+ const {getFilename} = operator;
30
+
31
+ const name = getFilename(filePath);
32
+ ```
33
+
34
+ ### `removeFile(filePath: Path)`
35
+
36
+ ```js
37
+ const {operator} = require('putout');
38
+ const {removeFile} = operator;
39
+
40
+ removeFile(filePath);
41
+ ```
42
+
43
+ ### `renameFile(filePath: Path, name: string)`
26
44
 
27
45
  ```js
28
46
  const montag = require('montag');
@@ -42,7 +60,7 @@ const ast = parse(montag`
42
60
  });
43
61
  `);
44
62
 
45
- const [filePath] = findFiles(ast, 'hello');
63
+ const [filePath] = findFile(ast, 'hello');
46
64
 
47
65
  renameFile(filePath, 'world');
48
66
 
package/lib/filesystem.js CHANGED
@@ -8,7 +8,7 @@ const {
8
8
 
9
9
  const maybeFS = require('./maybe-fs');
10
10
 
11
- module.exports.findFiles = (node, name) => {
11
+ module.exports.findFile = (node, name) => {
12
12
  const filePaths = [];
13
13
 
14
14
  for (const filenamePath of traverseProperties(node, 'filename')) {
@@ -22,11 +22,22 @@ module.exports.findFiles = (node, name) => {
22
22
  return filePaths;
23
23
  };
24
24
 
25
- module.exports.renameFile = (filePath, name) => {
25
+ function getFilenamePath(filePath) {
26
26
  const filenamePath = getProperty(filePath, 'filename');
27
- const valuePath = filenamePath.get('value');
28
- const {value: oldName} = filenamePath.node.value;
29
27
 
28
+ return filenamePath.get('value');
29
+ }
30
+
31
+ function getFilename(filePath) {
32
+ const {value} = getFilenamePath(filePath).node;
33
+ return value;
34
+ }
35
+
36
+ module.exports.getFilename = getFilename;
37
+
38
+ module.exports.renameFile = (filePath, name) => {
39
+ const oldName = getFilename(filePath);
40
+ const valuePath = getFilenamePath(filePath);
30
41
  const baseName = oldName
31
42
  .split('/')
32
43
  .pop();
@@ -37,5 +48,12 @@ module.exports.renameFile = (filePath, name) => {
37
48
  maybeFS.renameFile(oldName, newName);
38
49
  };
39
50
 
51
+ module.exports.removeFile = (filePath) => {
52
+ const filename = getFilename(filePath);
53
+
54
+ filePath.remove();
55
+ maybeFS.removeFile(filename);
56
+ };
57
+
40
58
  module.exports.init = maybeFS.init;
41
59
  module.exports.deinit = maybeFS.deinit;
package/lib/maybe-fs.js CHANGED
@@ -5,6 +5,7 @@ const noop = () => {};
5
5
 
6
6
  const defaultFS = {
7
7
  renameFile: noop,
8
+ removeFile: noop,
8
9
  };
9
10
 
10
11
  const maybeFS = assign({}, defaultFS);
@@ -13,6 +14,10 @@ module.exports.renameFile = (oldName, newName) => {
13
14
  maybeFS.renameFile(oldName, newName);
14
15
  };
15
16
 
17
+ module.exports.removeFile = (name) => {
18
+ maybeFS.removeFile(name);
19
+ };
20
+
16
21
  module.exports.init = (fsDriver) => {
17
22
  assign(maybeFS, fsDriver);
18
23
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "1.0.1",
3
+ "version": "1.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",