@putout/operator-filesystem 2.7.0 → 2.9.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 +50 -25
- package/package.json +1 -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');
|
|
@@ -100,15 +102,6 @@ module.exports.removeFile = (filePath) => {
|
|
|
100
102
|
maybeFS.removeFile(filename);
|
|
101
103
|
};
|
|
102
104
|
|
|
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
|
-
|
|
112
105
|
module.exports.moveFile = (filePath, dirPath) => {
|
|
113
106
|
const dirname = getFilename(dirPath);
|
|
114
107
|
const filename = getFilename(filePath);
|
|
@@ -119,23 +112,20 @@ module.exports.moveFile = (filePath, dirPath) => {
|
|
|
119
112
|
.split('/')
|
|
120
113
|
.pop();
|
|
121
114
|
|
|
122
|
-
const
|
|
123
|
-
const [is, fileToOverwrite] = getFile(dirPathFiles, newname);
|
|
115
|
+
const newFilename = `${dirname}/${basename}`;
|
|
124
116
|
|
|
125
|
-
|
|
126
|
-
fileToOverwrite.remove();
|
|
117
|
+
maybeRemoveFile(dirPath, newFilename);
|
|
127
118
|
|
|
128
|
-
setLiteralValue(filenamePath.get('value'),
|
|
119
|
+
setLiteralValue(filenamePath.get('value'), newFilename);
|
|
129
120
|
dirPathFiles.node.value.elements.push(filePath.node);
|
|
130
121
|
filePath.remove();
|
|
131
122
|
|
|
132
|
-
maybeFS.renameFile(filename,
|
|
123
|
+
maybeFS.renameFile(filename, newFilename);
|
|
133
124
|
};
|
|
134
125
|
|
|
135
126
|
module.exports.copyFile = (filePath, dirPath) => {
|
|
136
127
|
const dirname = getFilename(dirPath);
|
|
137
128
|
const filename = getFilename(filePath);
|
|
138
|
-
const dirPathFiles = getProperty(dirPath, 'files');
|
|
139
129
|
|
|
140
130
|
const basename = filename
|
|
141
131
|
.split('/')
|
|
@@ -150,23 +140,56 @@ module.exports.copyFile = (filePath, dirPath) => {
|
|
|
150
140
|
hasContent && createContent(content),
|
|
151
141
|
].filter(Boolean));
|
|
152
142
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
if (is)
|
|
156
|
-
fileToOverwrite.remove();
|
|
143
|
+
maybeRemoveFile(dirPath, newFilename);
|
|
157
144
|
|
|
145
|
+
const dirPathFiles = getFiles(dirPath);
|
|
158
146
|
dirPathFiles.node.value.elements.push(copiedFile);
|
|
159
147
|
|
|
160
148
|
maybeFS.copyFile(filename, newFilename);
|
|
161
149
|
};
|
|
162
150
|
|
|
151
|
+
function maybeRemoveFile(dirPath, filename) {
|
|
152
|
+
const dirPathFiles = getProperty(dirPath, 'files');
|
|
153
|
+
const [fileToOverwrite] = findFile(dirPathFiles, filename);
|
|
154
|
+
|
|
155
|
+
if (!fileToOverwrite)
|
|
156
|
+
return;
|
|
157
|
+
|
|
158
|
+
fileToOverwrite.remove();
|
|
159
|
+
}
|
|
160
|
+
|
|
163
161
|
const createType = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
|
|
164
162
|
const createFiles = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
|
|
165
163
|
const createFilename = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
|
|
166
164
|
const createContent = (content) => ObjectProperty(StringLiteral('content'), StringLiteral(content));
|
|
167
165
|
|
|
166
|
+
module.exports.createFile = (dirPath, name, content = '') => {
|
|
167
|
+
maybeRemoveFile(dirPath, name);
|
|
168
|
+
|
|
169
|
+
const dirPathFiles = getFiles(dirPath);
|
|
170
|
+
const parentFilename = getFilename(dirPath);
|
|
171
|
+
const filename = `${parentFilename}/${name}`;
|
|
172
|
+
|
|
173
|
+
const typeProperty = createType('file');
|
|
174
|
+
const filenameProperty = createFilename(filename);
|
|
175
|
+
|
|
176
|
+
dirPathFiles.node.value.elements.push(ObjectExpression([typeProperty, filenameProperty, createContent(content)]));
|
|
177
|
+
|
|
178
|
+
const filePath = dirPathFiles
|
|
179
|
+
.get('value.elements')
|
|
180
|
+
.at(-1);
|
|
181
|
+
|
|
182
|
+
writeFileContent(filePath, content);
|
|
183
|
+
|
|
184
|
+
return filePath;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
function getFiles(dirPath) {
|
|
188
|
+
return getProperty(dirPath, 'files');
|
|
189
|
+
}
|
|
190
|
+
|
|
168
191
|
module.exports.createDirectory = (dirPath, name) => {
|
|
169
|
-
const dirPathFiles =
|
|
192
|
+
const dirPathFiles = getFiles(dirPath);
|
|
170
193
|
const parentFilename = getFilename(dirPath);
|
|
171
194
|
const filename = `${parentFilename}/${name}`;
|
|
172
195
|
|
|
@@ -215,7 +238,9 @@ module.exports.readFileContent = (filePath) => {
|
|
|
215
238
|
return fileContent;
|
|
216
239
|
};
|
|
217
240
|
|
|
218
|
-
module.exports.writeFileContent =
|
|
241
|
+
module.exports.writeFileContent = writeFileContent;
|
|
242
|
+
|
|
243
|
+
function writeFileContent(filePath, content) {
|
|
219
244
|
const filename = getFilename(filePath);
|
|
220
245
|
|
|
221
246
|
maybeFS.writeFileContent(filename, content);
|
|
@@ -229,7 +254,7 @@ module.exports.writeFileContent = (filePath, content) => {
|
|
|
229
254
|
|
|
230
255
|
const property = createContentProperty(content);
|
|
231
256
|
filePath.node.properties.push(property);
|
|
232
|
-
}
|
|
257
|
+
}
|
|
233
258
|
|
|
234
259
|
module.exports.init = maybeFS.init;
|
|
235
260
|
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.9.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",
|