@putout/operator-filesystem 2.4.0 → 2.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
@@ -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));
152
+
153
+ const [is, fileToOverwrite] = getFile(dirPathFiles, newFilename);
149
154
 
150
- const [is] = getFile(dirPathFiles, newFilename);
155
+ if (is)
156
+ fileToOverwrite.remove();
151
157
 
152
- if (!is)
153
- dirPathFiles.node.value.elements.push(copyiedFile);
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');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "2.4.0",
3
+ "version": "2.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",