@putout/operator-filesystem 3.3.0 → 3.5.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 +13 -0
- package/lib/filesystem.js +58 -33
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -100,6 +100,19 @@ getFileType(filePath);
|
|
|
100
100
|
'file';
|
|
101
101
|
```
|
|
102
102
|
|
|
103
|
+
### `getFileContent(path: FilePath): [is: boolean, content: string]`
|
|
104
|
+
|
|
105
|
+
Get `content` property if it exists, use [`readFileContent`](#read-file-content) to read file from **Filesystem**.
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
const {operator} = require('putout');
|
|
109
|
+
const {getFileContent} = operator;
|
|
110
|
+
|
|
111
|
+
getFileContent(filePath);
|
|
112
|
+
// returns
|
|
113
|
+
[true, 'hello world'];
|
|
114
|
+
```
|
|
115
|
+
|
|
103
116
|
### `removeFile(filePath: Path)`
|
|
104
117
|
|
|
105
118
|
```js
|
package/lib/filesystem.js
CHANGED
|
@@ -15,6 +15,24 @@ const isString = (a) => typeof a === 'string';
|
|
|
15
15
|
const {isArray} = Array;
|
|
16
16
|
const maybeArray = (a) => isArray(a) ? a : [a];
|
|
17
17
|
|
|
18
|
+
const toBase64 = (content) => {
|
|
19
|
+
const [e, decoded] = tryCatch(btoa, content);
|
|
20
|
+
|
|
21
|
+
if (!e)
|
|
22
|
+
return encodeURI(decoded);
|
|
23
|
+
|
|
24
|
+
return content;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const fromBase64 = (content) => {
|
|
28
|
+
const [e, decoded] = tryCatch(atob, content);
|
|
29
|
+
|
|
30
|
+
if (!e)
|
|
31
|
+
return decodeURI(decoded);
|
|
32
|
+
|
|
33
|
+
return content;
|
|
34
|
+
};
|
|
35
|
+
|
|
18
36
|
const {
|
|
19
37
|
ObjectExpression,
|
|
20
38
|
ArrayExpression,
|
|
@@ -83,6 +101,8 @@ function getFileType(filePath) {
|
|
|
83
101
|
return typePath.node.value.value;
|
|
84
102
|
}
|
|
85
103
|
|
|
104
|
+
module.exports.getFileContent = getFileContent;
|
|
105
|
+
|
|
86
106
|
function getFileContent(filePath) {
|
|
87
107
|
const content = getProperty(filePath, 'content');
|
|
88
108
|
|
|
@@ -137,8 +157,8 @@ module.exports.moveFile = (filePath, dirPath) => {
|
|
|
137
157
|
|
|
138
158
|
setLiteralValue(filenamePath.get('value'), newFilename);
|
|
139
159
|
dirPathFiles.node.value.elements.push(filePath.node);
|
|
140
|
-
filePath.remove();
|
|
141
160
|
|
|
161
|
+
filePath.remove();
|
|
142
162
|
maybeFS.renameFile(filename, newFilename);
|
|
143
163
|
};
|
|
144
164
|
|
|
@@ -154,9 +174,9 @@ module.exports.copyFile = (filePath, dirPath) => {
|
|
|
154
174
|
const [hasContent, content] = getFileContent(filePath);
|
|
155
175
|
|
|
156
176
|
const copiedFile = ObjectExpression([
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
hasContent &&
|
|
177
|
+
createTypeProperty('file'),
|
|
178
|
+
createFilenameProperty(newFilename),
|
|
179
|
+
hasContent && createContentProperty(content),
|
|
160
180
|
].filter(Boolean));
|
|
161
181
|
|
|
162
182
|
maybeRemoveFile(dirPath, newFilename);
|
|
@@ -177,28 +197,46 @@ function maybeRemoveFile(dirPath, filename) {
|
|
|
177
197
|
fileToOverwrite.remove();
|
|
178
198
|
}
|
|
179
199
|
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
200
|
+
const createTypeProperty = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
|
|
201
|
+
|
|
202
|
+
module.exports.createTypeProperty = createTypeProperty;
|
|
203
|
+
|
|
204
|
+
const createFilesProperty = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
|
|
205
|
+
|
|
206
|
+
module.exports.createFilesProperty = createFilesProperty;
|
|
184
207
|
|
|
185
|
-
|
|
208
|
+
const createFilenameProperty = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
|
|
209
|
+
|
|
210
|
+
module.exports.createFilenameProperty = createFilenameProperty;
|
|
211
|
+
|
|
212
|
+
const createContentProperty = (content) => ObjectProperty(StringLiteral('content'), StringLiteral(content));
|
|
213
|
+
|
|
214
|
+
module.exports.createContentProperty = createContentProperty;
|
|
215
|
+
|
|
216
|
+
module.exports.createFile = (dirPath, name, content) => {
|
|
186
217
|
maybeRemoveFile(dirPath, name);
|
|
187
218
|
|
|
188
219
|
const dirPathFiles = getFiles(dirPath);
|
|
189
220
|
const parentFilename = getFilename(dirPath);
|
|
190
221
|
const filename = join(parentFilename, name);
|
|
191
222
|
|
|
192
|
-
const typeProperty =
|
|
193
|
-
const filenameProperty =
|
|
223
|
+
const typeProperty = createTypeProperty('file');
|
|
224
|
+
const filenameProperty = createFilenameProperty(filename);
|
|
225
|
+
|
|
226
|
+
const properties = [
|
|
227
|
+
typeProperty,
|
|
228
|
+
filenameProperty,
|
|
229
|
+
content && createContentProperty(content),
|
|
230
|
+
].filter(Boolean);
|
|
194
231
|
|
|
195
|
-
dirPathFiles.node.value.elements.push(ObjectExpression(
|
|
232
|
+
dirPathFiles.node.value.elements.push(ObjectExpression(properties));
|
|
196
233
|
|
|
197
234
|
const filePath = dirPathFiles
|
|
198
235
|
.get('value.elements')
|
|
199
236
|
.at(-1);
|
|
200
237
|
|
|
201
|
-
|
|
238
|
+
if (isString(content))
|
|
239
|
+
writeFileContent(filePath, content);
|
|
202
240
|
|
|
203
241
|
return filePath;
|
|
204
242
|
};
|
|
@@ -212,9 +250,9 @@ module.exports.createDirectory = (dirPath, name) => {
|
|
|
212
250
|
const parentFilename = getFilename(dirPath);
|
|
213
251
|
const filename = join(parentFilename, name);
|
|
214
252
|
|
|
215
|
-
const typeProperty =
|
|
216
|
-
const filesProperty =
|
|
217
|
-
const filenameProperty =
|
|
253
|
+
const typeProperty = createTypeProperty('directory');
|
|
254
|
+
const filesProperty = createFilesProperty([]);
|
|
255
|
+
const filenameProperty = createFilenameProperty(filename);
|
|
218
256
|
|
|
219
257
|
dirPathFiles.node.value.elements.push(ObjectExpression([
|
|
220
258
|
typeProperty,
|
|
@@ -229,13 +267,6 @@ module.exports.createDirectory = (dirPath, name) => {
|
|
|
229
267
|
.at(-1);
|
|
230
268
|
};
|
|
231
269
|
|
|
232
|
-
const createContentProperty = (content) => {
|
|
233
|
-
const contentKey = StringLiteral('content');
|
|
234
|
-
const contentValue = StringLiteral(btoa(encodeURI(content)));
|
|
235
|
-
|
|
236
|
-
return ObjectProperty(contentKey, contentValue);
|
|
237
|
-
};
|
|
238
|
-
|
|
239
270
|
module.exports.readFileContent = (filePath) => {
|
|
240
271
|
const fileType = getFileType(filePath);
|
|
241
272
|
|
|
@@ -244,14 +275,8 @@ module.exports.readFileContent = (filePath) => {
|
|
|
244
275
|
|
|
245
276
|
const [hasContent, content] = getFileContent(filePath);
|
|
246
277
|
|
|
247
|
-
if (hasContent)
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (!e)
|
|
251
|
-
return decodeURI(decoded);
|
|
252
|
-
|
|
253
|
-
return content;
|
|
254
|
-
}
|
|
278
|
+
if (hasContent)
|
|
279
|
+
return fromBase64(content);
|
|
255
280
|
|
|
256
281
|
const filename = getFilename(filePath);
|
|
257
282
|
const fileContent = maybeFS.readFileContent(filename);
|
|
@@ -277,11 +302,11 @@ function writeFileContent(filePath, content) {
|
|
|
277
302
|
const contentPath = getProperty(filePath, 'content');
|
|
278
303
|
|
|
279
304
|
if (contentPath) {
|
|
280
|
-
setLiteralValue(contentPath.node.value,
|
|
305
|
+
setLiteralValue(contentPath.node.value, toBase64(content));
|
|
281
306
|
return;
|
|
282
307
|
}
|
|
283
308
|
|
|
284
|
-
const property = createContentProperty(content);
|
|
309
|
+
const property = createContentProperty(toBase64(content));
|
|
285
310
|
filePath.node.properties.push(property);
|
|
286
311
|
}
|
|
287
312
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-filesystem",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.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",
|