@putout/operator-filesystem 3.4.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 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,7 +15,15 @@ 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 = (a) => btoa(encodeURI(a));
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
+
19
27
  const fromBase64 = (content) => {
20
28
  const [e, decoded] = tryCatch(atob, content);
21
29
 
@@ -93,6 +101,8 @@ function getFileType(filePath) {
93
101
  return typePath.node.value.value;
94
102
  }
95
103
 
104
+ module.exports.getFileContent = getFileContent;
105
+
96
106
  function getFileContent(filePath) {
97
107
  const content = getProperty(filePath, 'content');
98
108
 
@@ -147,8 +157,8 @@ module.exports.moveFile = (filePath, dirPath) => {
147
157
 
148
158
  setLiteralValue(filenamePath.get('value'), newFilename);
149
159
  dirPathFiles.node.value.elements.push(filePath.node);
150
- filePath.remove();
151
160
 
161
+ filePath.remove();
152
162
  maybeFS.renameFile(filename, newFilename);
153
163
  };
154
164
 
@@ -164,9 +174,9 @@ module.exports.copyFile = (filePath, dirPath) => {
164
174
  const [hasContent, content] = getFileContent(filePath);
165
175
 
166
176
  const copiedFile = ObjectExpression([
167
- createType('file'),
168
- createFilename(newFilename),
169
- hasContent && createContent(content),
177
+ createTypeProperty('file'),
178
+ createFilenameProperty(newFilename),
179
+ hasContent && createContentProperty(content),
170
180
  ].filter(Boolean));
171
181
 
172
182
  maybeRemoveFile(dirPath, newFilename);
@@ -187,28 +197,46 @@ function maybeRemoveFile(dirPath, filename) {
187
197
  fileToOverwrite.remove();
188
198
  }
189
199
 
190
- const createType = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
191
- const createFiles = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
192
- const createFilename = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
193
- const createContent = (content) => ObjectProperty(StringLiteral('content'), StringLiteral(content));
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));
194
205
 
195
- module.exports.createFile = (dirPath, name, content = '') => {
206
+ module.exports.createFilesProperty = createFilesProperty;
207
+
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) => {
196
217
  maybeRemoveFile(dirPath, name);
197
218
 
198
219
  const dirPathFiles = getFiles(dirPath);
199
220
  const parentFilename = getFilename(dirPath);
200
221
  const filename = join(parentFilename, name);
201
222
 
202
- const typeProperty = createType('file');
203
- const filenameProperty = createFilename(filename);
223
+ const typeProperty = createTypeProperty('file');
224
+ const filenameProperty = createFilenameProperty(filename);
204
225
 
205
- dirPathFiles.node.value.elements.push(ObjectExpression([typeProperty, filenameProperty, createContent(content)]));
226
+ const properties = [
227
+ typeProperty,
228
+ filenameProperty,
229
+ content && createContentProperty(content),
230
+ ].filter(Boolean);
231
+
232
+ dirPathFiles.node.value.elements.push(ObjectExpression(properties));
206
233
 
207
234
  const filePath = dirPathFiles
208
235
  .get('value.elements')
209
236
  .at(-1);
210
237
 
211
- writeFileContent(filePath, content);
238
+ if (isString(content))
239
+ writeFileContent(filePath, content);
212
240
 
213
241
  return filePath;
214
242
  };
@@ -222,9 +250,9 @@ module.exports.createDirectory = (dirPath, name) => {
222
250
  const parentFilename = getFilename(dirPath);
223
251
  const filename = join(parentFilename, name);
224
252
 
225
- const typeProperty = createType('directory');
226
- const filesProperty = createFiles([]);
227
- const filenameProperty = createFilename(filename);
253
+ const typeProperty = createTypeProperty('directory');
254
+ const filesProperty = createFilesProperty([]);
255
+ const filenameProperty = createFilenameProperty(filename);
228
256
 
229
257
  dirPathFiles.node.value.elements.push(ObjectExpression([
230
258
  typeProperty,
@@ -239,13 +267,6 @@ module.exports.createDirectory = (dirPath, name) => {
239
267
  .at(-1);
240
268
  };
241
269
 
242
- const createContentProperty = (content) => {
243
- const contentKey = StringLiteral('content');
244
- const contentValue = StringLiteral(toBase64(content));
245
-
246
- return ObjectProperty(contentKey, contentValue);
247
- };
248
-
249
270
  module.exports.readFileContent = (filePath) => {
250
271
  const fileType = getFileType(filePath);
251
272
 
@@ -285,7 +306,7 @@ function writeFileContent(filePath, content) {
285
306
  return;
286
307
  }
287
308
 
288
- const property = createContentProperty(content);
309
+ const property = createContentProperty(toBase64(content));
289
310
  filePath.node.properties.push(property);
290
311
  }
291
312
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "3.4.0",
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",