@putout/operator-filesystem 2.4.0 → 2.6.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
@@ -76,7 +76,7 @@ removeFile(filePath);
76
76
 
77
77
  ```js
78
78
  const {operator} = require('putout');
79
- const {moveFile} = operator;
79
+ const {moveFile, copyFile} = operator;
80
80
 
81
81
  copyFile(filePath, dirPath);
82
82
  ```
package/lib/filesystem.js CHANGED
@@ -120,13 +120,14 @@ module.exports.moveFile = (filePath, dirPath) => {
120
120
  .pop();
121
121
 
122
122
  const newname = `${dirname}/${basename}`;
123
- const [is] = getFile(dirPathFiles, newname);
123
+ const [is, fileToOverwrite] = getFile(dirPathFiles, newname);
124
124
 
125
- if (!is) {
126
- setLiteralValue(filenamePath.get('value'), newname);
127
- dirPathFiles.node.value.elements.push(filePath.node);
128
- filePath.remove();
129
- }
125
+ if (is)
126
+ fileToOverwrite.remove();
127
+
128
+ setLiteralValue(filenamePath.get('value'), newname);
129
+ dirPathFiles.node.value.elements.push(filePath.node);
130
+ filePath.remove();
130
131
 
131
132
  maybeFS.renameFile(filename, newname);
132
133
  };
@@ -141,16 +142,20 @@ module.exports.copyFile = (filePath, dirPath) => {
141
142
  .pop();
142
143
 
143
144
  const newFilename = `${dirname}/${basename}`;
145
+ const [hasContent, content] = getFileContent(filePath);
144
146
 
145
- const copyiedFile = ObjectExpression([
147
+ const copiedFile = ObjectExpression([
146
148
  createType('file'),
147
149
  createFilename(newFilename),
148
- ]);
150
+ hasContent && createContent(content),
151
+ ].filter(Boolean));
149
152
 
150
- const [is] = getFile(dirPathFiles, newFilename);
153
+ const [is, fileToOverwrite] = getFile(dirPathFiles, newFilename);
151
154
 
152
- if (!is)
153
- dirPathFiles.node.value.elements.push(copyiedFile);
155
+ if (is)
156
+ fileToOverwrite.remove();
157
+
158
+ dirPathFiles.node.value.elements.push(copiedFile);
154
159
 
155
160
  maybeFS.copyFile(filename, newFilename);
156
161
  };
@@ -158,6 +163,7 @@ module.exports.copyFile = (filePath, dirPath) => {
158
163
  const createType = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
159
164
  const createFiles = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
160
165
  const createFilename = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
166
+ const createContent = (content) => ObjectProperty(StringLiteral('content'), StringLiteral(content));
161
167
 
162
168
  module.exports.createDirectory = (dirPath, name) => {
163
169
  const dirPathFiles = getProperty(dirPath, 'files');
@@ -183,7 +189,7 @@ module.exports.createDirectory = (dirPath, name) => {
183
189
 
184
190
  const createContentProperty = (content) => {
185
191
  const contentKey = StringLiteral('content');
186
- const contentValue = StringLiteral(btoa(content));
192
+ const contentValue = StringLiteral(btoa(encodeURI(content)));
187
193
 
188
194
  return ObjectProperty(contentKey, contentValue);
189
195
  };
@@ -192,8 +198,12 @@ module.exports.readFileContent = (filePath) => {
192
198
  const [hasContent, content] = getFileContent(filePath);
193
199
 
194
200
  if (hasContent) {
195
- const [, decoded] = tryCatch(atob, content);
196
- return decoded || content;
201
+ const [e, decoded] = tryCatch(atob, content);
202
+
203
+ if (!e)
204
+ return decodeURI(decoded);
205
+
206
+ return content;
197
207
  }
198
208
 
199
209
  const filename = getFilename(filePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "2.4.0",
3
+ "version": "2.6.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",