@putout/operator-filesystem 2.3.0 → 2.4.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');
@@ -45,7 +45,16 @@ const {findFile} = operator;
45
45
  const coupleFiles = findFile(ast, ['*.js', '*.ts']);
46
46
  ```
47
47
 
48
- ### `getFilename(path: FilePath)`
48
+ And even search for a directory:
49
+
50
+ ```js
51
+ import {operator} from 'putout';
52
+
53
+ const {findFile} = operator;
54
+ const coupleFiles = findFile(ast, ['/home/coderaiser', '/home/coderaiser/putout']);
55
+ ```
56
+
57
+ ### `getFilename(path: FilePath): string`
49
58
 
50
59
  ```js
51
60
  const {operator} = require('putout');
@@ -63,6 +72,15 @@ const {removeFile} = operator;
63
72
  removeFile(filePath);
64
73
  ```
65
74
 
75
+ ### `copyFile(filePath: FilePath, dirPath: FilePath)`
76
+
77
+ ```js
78
+ const {operator} = require('putout');
79
+ const {moveFile} = operator;
80
+
81
+ copyFile(filePath, dirPath);
82
+ ```
83
+
66
84
  ### `moveFile(filePath: FilePath, dirPath: FilePath)`
67
85
 
68
86
  ```js
@@ -72,7 +90,7 @@ const {moveFile} = operator;
72
90
  moveFile(filePath, dirPath);
73
91
  ```
74
92
 
75
- ### `readFileContent(filePath: FilePath)`
93
+ ### `readFileContent(filePath: FilePath): string`
76
94
 
77
95
  ```js
78
96
  const {operator} = require('putout');
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,14 +120,41 @@ module.exports.moveFile = (filePath, dirPath) => {
111
120
  .pop();
112
121
 
113
122
  const newname = `${dirname}/${basename}`;
123
+ const [is] = getFile(dirPathFiles, newname);
114
124
 
115
- setLiteralValue(filenamePath.get('value'), newname);
116
- dirPathFiles.node.value.elements.push(filePath.node);
117
- filePath.remove();
125
+ if (!is) {
126
+ setLiteralValue(filenamePath.get('value'), newname);
127
+ dirPathFiles.node.value.elements.push(filePath.node);
128
+ filePath.remove();
129
+ }
118
130
 
119
131
  maybeFS.renameFile(filename, newname);
120
132
  };
121
133
 
134
+ module.exports.copyFile = (filePath, dirPath) => {
135
+ const dirname = getFilename(dirPath);
136
+ const filename = getFilename(filePath);
137
+ const dirPathFiles = getProperty(dirPath, 'files');
138
+
139
+ const basename = filename
140
+ .split('/')
141
+ .pop();
142
+
143
+ const newFilename = `${dirname}/${basename}`;
144
+
145
+ const copyiedFile = ObjectExpression([
146
+ createType('file'),
147
+ createFilename(newFilename),
148
+ ]);
149
+
150
+ const [is] = getFile(dirPathFiles, newFilename);
151
+
152
+ if (!is)
153
+ dirPathFiles.node.value.elements.push(copyiedFile);
154
+
155
+ maybeFS.copyFile(filename, newFilename);
156
+ };
157
+
122
158
  const createType = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
123
159
  const createFiles = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
124
160
  const createFilename = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
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.0",
3
+ "version": "2.4.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",