@putout/operator-filesystem 2.3.1 → 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
@@ -13,7 +13,7 @@ npm i putout @putout/operator-filesystem
13
13
 
14
14
  ## API
15
15
 
16
- ## `createDirectory(directoryPath: FilePath, name: string): FilePath`
16
+ ### `createDirectory(directoryPath: FilePath, name: string): FilePath`
17
17
 
18
18
  ```js
19
19
  const {operator} = require('putout');
@@ -27,7 +27,7 @@ const [dirPath] = findFile(ast, 'hello');
27
27
  const newDirectoryPath = createDirectory(dirPath, 'world');
28
28
  ```
29
29
 
30
- ## `findFile(path: Path, name: string | string[]): FilePath[]`
30
+ ### `findFile(path: Path, name: string | string[]): FilePath[]`
31
31
 
32
32
  ```js
33
33
  const {operator} = require('putout');
@@ -72,6 +72,15 @@ const {removeFile} = operator;
72
72
  removeFile(filePath);
73
73
  ```
74
74
 
75
+ ### `copyFile(filePath: FilePath, dirPath: FilePath)`
76
+
77
+ ```js
78
+ const {operator} = require('putout');
79
+ const {moveFile, copyFile} = operator;
80
+
81
+ copyFile(filePath, dirPath);
82
+ ```
83
+
75
84
  ### `moveFile(filePath: FilePath, dirPath: FilePath)`
76
85
 
77
86
  ```js
package/lib/filesystem.js CHANGED
@@ -100,6 +100,15 @@ module.exports.removeFile = (filePath) => {
100
100
  maybeFS.removeFile(filename);
101
101
  };
102
102
 
103
+ function getFile(dirPathFiles, name) {
104
+ for (const file of dirPathFiles.get('value.elements')) {
105
+ if (name === getFilename(file))
106
+ return [true, file];
107
+ }
108
+
109
+ return [false];
110
+ }
111
+
103
112
  module.exports.moveFile = (filePath, dirPath) => {
104
113
  const dirname = getFilename(dirPath);
105
114
  const filename = getFilename(filePath);
@@ -111,6 +120,10 @@ module.exports.moveFile = (filePath, dirPath) => {
111
120
  .pop();
112
121
 
113
122
  const newname = `${dirname}/${basename}`;
123
+ const [is, fileToOverwrite] = getFile(dirPathFiles, newname);
124
+
125
+ if (is)
126
+ fileToOverwrite.remove();
114
127
 
115
128
  setLiteralValue(filenamePath.get('value'), newname);
116
129
  dirPathFiles.node.value.elements.push(filePath.node);
@@ -119,9 +132,38 @@ module.exports.moveFile = (filePath, dirPath) => {
119
132
  maybeFS.renameFile(filename, newname);
120
133
  };
121
134
 
135
+ module.exports.copyFile = (filePath, dirPath) => {
136
+ const dirname = getFilename(dirPath);
137
+ const filename = getFilename(filePath);
138
+ const dirPathFiles = getProperty(dirPath, 'files');
139
+
140
+ const basename = filename
141
+ .split('/')
142
+ .pop();
143
+
144
+ const newFilename = `${dirname}/${basename}`;
145
+ const [hasContent, content] = getFileContent(filePath);
146
+
147
+ const copiedFile = ObjectExpression([
148
+ createType('file'),
149
+ createFilename(newFilename),
150
+ hasContent && createContent(content),
151
+ ].filter(Boolean));
152
+
153
+ const [is, fileToOverwrite] = getFile(dirPathFiles, newFilename);
154
+
155
+ if (is)
156
+ fileToOverwrite.remove();
157
+
158
+ dirPathFiles.node.value.elements.push(copiedFile);
159
+
160
+ maybeFS.copyFile(filename, newFilename);
161
+ };
162
+
122
163
  const createType = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
123
164
  const createFiles = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
124
165
  const createFilename = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
166
+ const createContent = (content) => ObjectProperty(StringLiteral('content'), StringLiteral(content));
125
167
 
126
168
  module.exports.createDirectory = (dirPath, name) => {
127
169
  const dirPathFiles = getProperty(dirPath, 'files');
package/lib/maybe-fs.js CHANGED
@@ -10,6 +10,7 @@ const defaultFS = {
10
10
  createDirectory: noop,
11
11
  readFileContent: returns(''),
12
12
  writeFileContent: noop,
13
+ copyFile: noop,
13
14
  };
14
15
 
15
16
  const maybeFS = assign({}, defaultFS);
@@ -22,6 +23,10 @@ module.exports.removeFile = (name) => {
22
23
  maybeFS.removeFile(name);
23
24
  };
24
25
 
26
+ module.exports.copyFile = (from, to) => {
27
+ maybeFS.copyFile(from, to);
28
+ };
29
+
25
30
  module.exports.createDirectory = (name) => {
26
31
  maybeFS.createDirectory(name);
27
32
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "2.3.1",
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",