@putout/operator-filesystem 1.5.0 → 1.7.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
@@ -17,17 +17,21 @@ npm i putout @putout/operator-filesystem
17
17
 
18
18
  ```js
19
19
  const {operator} = require('putout');
20
- const {createDirectory} = operator;
20
+ const {
21
+ createDirectory,
22
+ findFile,
23
+ } = operator;
21
24
 
22
25
  const [dirPath] = findFile(ast, 'hello');
23
- createDirectory(dirPath, 'world');// returns directoryPath
26
+
27
+ createDirectory(dirPath, 'world'); // returns directoryPath
24
28
  ```
25
29
 
26
30
  ## `findFile(path: Path, name: string)`
27
31
 
28
32
  ```js
29
33
  const {operator} = require('putout');
30
- const {finedFiles} = operator;
34
+ const {finedFiles, findFile} = operator;
31
35
 
32
36
  const [filePath] = findFile(ast, 'hello');
33
37
  ```
@@ -59,6 +63,29 @@ const {moveFile} = operator;
59
63
  moveFile(filePath, dirPath);
60
64
  ```
61
65
 
66
+ ### `readFileContent(filePath: Path)`
67
+
68
+ ```js
69
+ const {operator} = require('putout');
70
+ const {readFileContent} = operator;
71
+
72
+ readFileContent(filePath);
73
+ // returns
74
+ 'hello';
75
+ ```
76
+
77
+ ### `writeFileContent(filePath: Path, content: string)`
78
+
79
+ ```js
80
+ const {operator} = require('putout');
81
+ const {writeFileContent} = operator;
82
+
83
+ writeFileContent(filePath, 'hello');
84
+ readFileContent(filePath);
85
+ // returns
86
+ 'hello';
87
+ ```
88
+
62
89
  ### `renameFile(filePath: Path, name: string)`
63
90
 
64
91
  ```js
@@ -69,7 +96,7 @@ const {
69
96
  operator,
70
97
  } = require('putout');
71
98
 
72
- const {renameFile} = operator;
99
+ const {renameFile, findFile} = operator;
73
100
 
74
101
  const ast = parse(montag`
75
102
  putout_processor_filesystem({
package/lib/filesystem.js CHANGED
@@ -14,6 +14,7 @@ const {
14
14
  ArrayExpression,
15
15
  StringLiteral,
16
16
  ObjectProperty,
17
+ isProgram,
17
18
  } = types;
18
19
 
19
20
  const getRegExp = (wildcard) => {
@@ -25,15 +26,23 @@ const getRegExp = (wildcard) => {
25
26
  return RegExp(`${escaped}$`);
26
27
  };
27
28
 
29
+ module.exports.getParentDirectory = (filePath) => {
30
+ const {parentPath} = filePath.parentPath.parentPath;
31
+
32
+ if (isProgram(parentPath))
33
+ return null;
34
+
35
+ return parentPath;
36
+ };
37
+
28
38
  module.exports.findFile = (node, name) => {
29
39
  const filePaths = [];
30
40
 
31
41
  for (const filenamePath of traverseProperties(node, 'filename')) {
32
42
  const {value} = filenamePath.node.value;
33
43
 
34
- if (value === name || getRegExp(name).test(value)) {
44
+ if (value === name || getRegExp(name).test(value))
35
45
  filePaths.push(filenamePath.parentPath);
36
- }
37
46
  }
38
47
 
39
48
  return filePaths;
@@ -49,6 +58,14 @@ function getFilename(filePath) {
49
58
  return value;
50
59
  }
51
60
 
61
+ function getFileContent(filePath) {
62
+ const content = getProperty(filePath, 'content');
63
+ return [
64
+ Boolean(content),
65
+ content?.node.value.value,
66
+ ];
67
+ }
68
+
52
69
  module.exports.getFilename = getFilename;
53
70
 
54
71
  module.exports.renameFile = (filePath, name) => {
@@ -116,5 +133,27 @@ module.exports.createDirectory = (dirPath, name) => {
116
133
  .at(-1);
117
134
  };
118
135
 
136
+ module.exports.readFileContent = (filePath) => {
137
+ const [hasContent, content] = getFileContent(filePath);
138
+
139
+ if (hasContent)
140
+ return content;
141
+
142
+ const filename = getFilename(filePath);
143
+ const fileContent = maybeFS.readFileContent(filename);
144
+
145
+ filePath.node.properties.push(ObjectProperty(StringLiteral('content'), StringLiteral(fileContent)));
146
+
147
+ return fileContent;
148
+ };
149
+
150
+ module.exports.writeFileContent = (filePath, content) => {
151
+ const filename = getFilename(filePath);
152
+
153
+ maybeFS.writeFileContent(filename, content);
154
+
155
+ filePath.node.properties.push(ObjectProperty(StringLiteral('content'), StringLiteral(content)));
156
+ };
157
+
119
158
  module.exports.init = maybeFS.init;
120
159
  module.exports.deinit = maybeFS.deinit;
package/lib/maybe-fs.js CHANGED
@@ -2,11 +2,14 @@
2
2
 
3
3
  const {assign} = Object;
4
4
  const noop = () => {};
5
+ const returns = (a) => () => a;
5
6
 
6
7
  const defaultFS = {
7
8
  renameFile: noop,
8
9
  removeFile: noop,
9
10
  createDirectory: noop,
11
+ readFileContent: returns(''),
12
+ writeFileContent: noop,
10
13
  };
11
14
 
12
15
  const maybeFS = assign({}, defaultFS);
@@ -23,6 +26,14 @@ module.exports.createDirectory = (name) => {
23
26
  maybeFS.createDirectory(name);
24
27
  };
25
28
 
29
+ module.exports.readFileContent = (name) => {
30
+ return maybeFS.readFileContent(name);
31
+ };
32
+
33
+ module.exports.writeFileContent = (name) => {
34
+ maybeFS.writeFileContent(name);
35
+ };
36
+
26
37
  module.exports.init = (fsDriver) => {
27
38
  assign(maybeFS, fsDriver);
28
39
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "1.5.0",
3
+ "version": "1.7.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",