@putout/operator-filesystem 1.6.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 +24 -0
- package/lib/filesystem.js +31 -2
- package/lib/maybe-fs.js +11 -0
- package/package.json +1 -1
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,29 @@ 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 {writeFileContent} = operator;
|
|
82
|
+
|
|
83
|
+
writeFileContent(filePath, 'hello');
|
|
84
|
+
readFileContent(filePath);
|
|
85
|
+
// returns
|
|
86
|
+
'hello';
|
|
87
|
+
```
|
|
88
|
+
|
|
65
89
|
### `renameFile(filePath: Path, name: string)`
|
|
66
90
|
|
|
67
91
|
```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,14 @@ function getFilename(filePath) {
|
|
|
59
58
|
return value;
|
|
60
59
|
}
|
|
61
60
|
|
|
61
|
+
function getFileContent(filePath) {
|
|
62
|
+
const content = getProperty(filePath, 'content');
|
|
63
|
+
return [
|
|
64
|
+
Boolean(content),
|
|
65
|
+
content?.node.value.value,
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
|
|
62
69
|
module.exports.getFilename = getFilename;
|
|
63
70
|
|
|
64
71
|
module.exports.renameFile = (filePath, name) => {
|
|
@@ -126,5 +133,27 @@ module.exports.createDirectory = (dirPath, name) => {
|
|
|
126
133
|
.at(-1);
|
|
127
134
|
};
|
|
128
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
|
+
|
|
129
158
|
module.exports.init = maybeFS.init;
|
|
130
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.
|
|
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",
|