@putout/operator-filesystem 2.3.1 → 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 +11 -2
- package/lib/filesystem.js +39 -3
- package/lib/maybe-fs.js +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm i putout @putout/operator-filesystem
|
|
|
13
13
|
|
|
14
14
|
## API
|
|
15
15
|
|
|
16
|
-
|
|
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
|
-
|
|
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} = 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,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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
+
"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",
|