@putout/operator-filesystem 1.6.0 → 2.0.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
@@ -23,6 +23,7 @@ const {
23
23
  } = operator;
24
24
 
25
25
  const [dirPath] = findFile(ast, 'hello');
26
+
26
27
  createDirectory(dirPath, 'world'); // returns directoryPath
27
28
  ```
28
29
 
@@ -62,6 +63,32 @@ const {moveFile} = operator;
62
63
  moveFile(filePath, dirPath);
63
64
  ```
64
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 {
82
+ writeFileContent,
83
+ readFileContent,
84
+ } = operator;
85
+
86
+ writeFileContent(filePath, 'hello');
87
+ readFileContent(filePath);
88
+ // returns
89
+ 'hello';
90
+ ```
91
+
65
92
  ### `renameFile(filePath: Path, name: string)`
66
93
 
67
94
  ```js
package/lib/filesystem.js CHANGED
@@ -41,9 +41,8 @@ module.exports.findFile = (node, name) => {
41
41
  for (const filenamePath of traverseProperties(node, 'filename')) {
42
42
  const {value} = filenamePath.node.value;
43
43
 
44
- if (value === name || getRegExp(name).test(value)) {
44
+ if (value === name || getRegExp(name).test(value))
45
45
  filePaths.push(filenamePath.parentPath);
46
- }
47
46
  }
48
47
 
49
48
  return filePaths;
@@ -59,6 +58,15 @@ function getFilename(filePath) {
59
58
  return value;
60
59
  }
61
60
 
61
+ function getFileContent(filePath) {
62
+ const content = getProperty(filePath, 'content');
63
+
64
+ return [
65
+ Boolean(content),
66
+ content?.node.value.value,
67
+ ];
68
+ }
69
+
62
70
  module.exports.getFilename = getFilename;
63
71
 
64
72
  module.exports.renameFile = (filePath, name) => {
@@ -126,5 +134,27 @@ module.exports.createDirectory = (dirPath, name) => {
126
134
  .at(-1);
127
135
  };
128
136
 
137
+ module.exports.readFileContent = (filePath) => {
138
+ const [hasContent, content] = getFileContent(filePath);
139
+
140
+ if (hasContent)
141
+ return content;
142
+
143
+ const filename = getFilename(filePath);
144
+ const fileContent = maybeFS.readFileContent(filename);
145
+
146
+ filePath.node.properties.push(ObjectProperty(StringLiteral('content'), StringLiteral(fileContent)));
147
+
148
+ return fileContent;
149
+ };
150
+
151
+ module.exports.writeFileContent = (filePath, content) => {
152
+ const filename = getFilename(filePath);
153
+
154
+ maybeFS.writeFileContent(filename, content);
155
+
156
+ filePath.node.properties.push(ObjectProperty(StringLiteral('content'), StringLiteral(content)));
157
+ };
158
+
129
159
  module.exports.init = maybeFS.init;
130
160
  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.6.0",
3
+ "version": "2.0.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",
@@ -46,7 +46,7 @@
46
46
  "try-catch": "^3.0.0"
47
47
  },
48
48
  "peerDependencies": {
49
- "putout": ">=32"
49
+ "putout": ">=33"
50
50
  },
51
51
  "license": "MIT",
52
52
  "engines": {