@putout/operator-filesystem 2.6.0 → 2.8.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 +25 -0
- package/lib/filesystem.js +54 -20
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -13,6 +13,31 @@ npm i putout @putout/operator-filesystem
|
|
|
13
13
|
|
|
14
14
|
## API
|
|
15
15
|
|
|
16
|
+
### `creatFile(directoryPath: DirectoryPath, name: string, content?: string): FilePath`
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const {operator} = require('putout');
|
|
20
|
+
const {
|
|
21
|
+
createDirectory,
|
|
22
|
+
findFile,
|
|
23
|
+
} = operator;
|
|
24
|
+
|
|
25
|
+
const [dirPath] = findFile(ast, 'hello');
|
|
26
|
+
const filePath = createFile(dirPath, 'world.txt', 'hello world');
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const {operator} = require('putout');
|
|
31
|
+
const {
|
|
32
|
+
createDirectory,
|
|
33
|
+
findFile,
|
|
34
|
+
} = operator;
|
|
35
|
+
|
|
36
|
+
const [dirPath] = findFile(ast, 'hello');
|
|
37
|
+
|
|
38
|
+
const newDirectoryPath = createDirectory(dirPath, 'world');
|
|
39
|
+
```
|
|
40
|
+
|
|
16
41
|
### `createDirectory(directoryPath: FilePath, name: string): FilePath`
|
|
17
42
|
|
|
18
43
|
```js
|
package/lib/filesystem.js
CHANGED
|
@@ -39,7 +39,9 @@ module.exports.getParentDirectory = (filePath) => {
|
|
|
39
39
|
return parentPath;
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
-
module.exports.findFile =
|
|
42
|
+
module.exports.findFile = findFile;
|
|
43
|
+
|
|
44
|
+
function findFile(node, name) {
|
|
43
45
|
const filePaths = [];
|
|
44
46
|
const names = maybeArray(name);
|
|
45
47
|
|
|
@@ -53,7 +55,7 @@ module.exports.findFile = (node, name) => {
|
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
return filePaths;
|
|
56
|
-
}
|
|
58
|
+
}
|
|
57
59
|
|
|
58
60
|
function getFilenamePath(filePath) {
|
|
59
61
|
const filenamePath = getProperty(filePath, 'filename');
|
|
@@ -103,10 +105,10 @@ module.exports.removeFile = (filePath) => {
|
|
|
103
105
|
function getFile(dirPathFiles, name) {
|
|
104
106
|
for (const file of dirPathFiles.get('value.elements')) {
|
|
105
107
|
if (name === getFilename(file))
|
|
106
|
-
return
|
|
108
|
+
return file;
|
|
107
109
|
}
|
|
108
110
|
|
|
109
|
-
return
|
|
111
|
+
return null;
|
|
110
112
|
}
|
|
111
113
|
|
|
112
114
|
module.exports.moveFile = (filePath, dirPath) => {
|
|
@@ -119,23 +121,20 @@ module.exports.moveFile = (filePath, dirPath) => {
|
|
|
119
121
|
.split('/')
|
|
120
122
|
.pop();
|
|
121
123
|
|
|
122
|
-
const
|
|
123
|
-
const [is, fileToOverwrite] = getFile(dirPathFiles, newname);
|
|
124
|
+
const newFilename = `${dirname}/${basename}`;
|
|
124
125
|
|
|
125
|
-
|
|
126
|
-
fileToOverwrite.remove();
|
|
126
|
+
maybeRemoveFile(dirPath, newFilename);
|
|
127
127
|
|
|
128
|
-
setLiteralValue(filenamePath.get('value'),
|
|
128
|
+
setLiteralValue(filenamePath.get('value'), newFilename);
|
|
129
129
|
dirPathFiles.node.value.elements.push(filePath.node);
|
|
130
130
|
filePath.remove();
|
|
131
131
|
|
|
132
|
-
maybeFS.renameFile(filename,
|
|
132
|
+
maybeFS.renameFile(filename, newFilename);
|
|
133
133
|
};
|
|
134
134
|
|
|
135
135
|
module.exports.copyFile = (filePath, dirPath) => {
|
|
136
136
|
const dirname = getFilename(dirPath);
|
|
137
137
|
const filename = getFilename(filePath);
|
|
138
|
-
const dirPathFiles = getProperty(dirPath, 'files');
|
|
139
138
|
|
|
140
139
|
const basename = filename
|
|
141
140
|
.split('/')
|
|
@@ -150,23 +149,58 @@ module.exports.copyFile = (filePath, dirPath) => {
|
|
|
150
149
|
hasContent && createContent(content),
|
|
151
150
|
].filter(Boolean));
|
|
152
151
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
if (is)
|
|
156
|
-
fileToOverwrite.remove();
|
|
152
|
+
maybeRemoveFile(dirPath, newFilename);
|
|
157
153
|
|
|
154
|
+
const dirPathFiles = getFiles(dirPath);
|
|
158
155
|
dirPathFiles.node.value.elements.push(copiedFile);
|
|
159
156
|
|
|
160
157
|
maybeFS.copyFile(filename, newFilename);
|
|
161
158
|
};
|
|
162
159
|
|
|
160
|
+
function maybeRemoveFile(dirPath, filename) {
|
|
161
|
+
const dirPathFiles = getProperty(dirPath, 'files');
|
|
162
|
+
|
|
163
|
+
const fileToOverwrite = getFile(dirPathFiles, filename);
|
|
164
|
+
|
|
165
|
+
if (!fileToOverwrite)
|
|
166
|
+
return;
|
|
167
|
+
|
|
168
|
+
fileToOverwrite.remove();
|
|
169
|
+
}
|
|
170
|
+
|
|
163
171
|
const createType = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
|
|
164
172
|
const createFiles = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
|
|
165
173
|
const createFilename = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
|
|
166
174
|
const createContent = (content) => ObjectProperty(StringLiteral('content'), StringLiteral(content));
|
|
167
175
|
|
|
176
|
+
module.exports.createFile = (dirPath, name, content) => {
|
|
177
|
+
maybeRemoveFile(dirPath, name);
|
|
178
|
+
|
|
179
|
+
const dirPathFiles = getFiles(dirPath);
|
|
180
|
+
const parentFilename = getFilename(dirPath);
|
|
181
|
+
const filename = `${parentFilename}/${name}`;
|
|
182
|
+
|
|
183
|
+
const typeProperty = createType('file');
|
|
184
|
+
const filenameProperty = createFilename(filename);
|
|
185
|
+
|
|
186
|
+
dirPathFiles.node.value.elements.push(ObjectExpression([typeProperty, filenameProperty]));
|
|
187
|
+
|
|
188
|
+
const filePath = dirPathFiles
|
|
189
|
+
.get('value.elements')
|
|
190
|
+
.at(-1);
|
|
191
|
+
|
|
192
|
+
if (content)
|
|
193
|
+
writeFileContent(filePath, content);
|
|
194
|
+
|
|
195
|
+
return filePath;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
function getFiles(dirPath) {
|
|
199
|
+
return getProperty(dirPath, 'files');
|
|
200
|
+
}
|
|
201
|
+
|
|
168
202
|
module.exports.createDirectory = (dirPath, name) => {
|
|
169
|
-
const dirPathFiles =
|
|
203
|
+
const dirPathFiles = getFiles(dirPath);
|
|
170
204
|
const parentFilename = getFilename(dirPath);
|
|
171
205
|
const filename = `${parentFilename}/${name}`;
|
|
172
206
|
|
|
@@ -215,7 +249,9 @@ module.exports.readFileContent = (filePath) => {
|
|
|
215
249
|
return fileContent;
|
|
216
250
|
};
|
|
217
251
|
|
|
218
|
-
module.exports.writeFileContent =
|
|
252
|
+
module.exports.writeFileContent = writeFileContent;
|
|
253
|
+
|
|
254
|
+
function writeFileContent(filePath, content) {
|
|
219
255
|
const filename = getFilename(filePath);
|
|
220
256
|
|
|
221
257
|
maybeFS.writeFileContent(filename, content);
|
|
@@ -229,7 +265,5 @@ module.exports.writeFileContent = (filePath, content) => {
|
|
|
229
265
|
|
|
230
266
|
const property = createContentProperty(content);
|
|
231
267
|
filePath.node.properties.push(property);
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
module.exports.init = maybeFS.init;
|
|
268
|
+
}module.exports.init = maybeFS.init;
|
|
235
269
|
module.exports.deinit = maybeFS.deinit;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-filesystem",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.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",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"filesystem"
|
|
35
35
|
],
|
|
36
36
|
"devDependencies": {
|
|
37
|
+
"@putout/engine-parser": "^9.6.0",
|
|
37
38
|
"@putout/operator-json": "^1.3.0",
|
|
38
39
|
"@putout/test": "^7.0.0",
|
|
39
40
|
"c8": "^8.0.0",
|