@putout/operator-filesystem 3.4.0 → 3.6.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 +14 -17
- package/lib/filesystem.js +53 -25
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,28 +17,12 @@ npm i putout @putout/operator-filesystem
|
|
|
17
17
|
|
|
18
18
|
```js
|
|
19
19
|
const {operator} = require('putout');
|
|
20
|
-
const {
|
|
21
|
-
createDirectory,
|
|
22
|
-
findFile,
|
|
23
|
-
createFile,
|
|
24
|
-
} = operator;
|
|
20
|
+
const {findFile, createFile} = operator;
|
|
25
21
|
|
|
26
22
|
const [dirPath] = findFile(ast, 'hello');
|
|
27
23
|
const filePath = createFile(dirPath, 'world.txt', 'hello world');
|
|
28
24
|
```
|
|
29
25
|
|
|
30
|
-
```js
|
|
31
|
-
const {operator} = require('putout');
|
|
32
|
-
const {
|
|
33
|
-
createDirectory,
|
|
34
|
-
findFile,
|
|
35
|
-
} = operator;
|
|
36
|
-
|
|
37
|
-
const [dirPath] = findFile(ast, 'hello');
|
|
38
|
-
|
|
39
|
-
const newDirectoryPath = createDirectory(dirPath, 'world');
|
|
40
|
-
```
|
|
41
|
-
|
|
42
26
|
### `createDirectory(directoryPath: FilePath, name: string): FilePath`
|
|
43
27
|
|
|
44
28
|
```js
|
|
@@ -100,6 +84,19 @@ getFileType(filePath);
|
|
|
100
84
|
'file';
|
|
101
85
|
```
|
|
102
86
|
|
|
87
|
+
### `getFileContent(path: FilePath): [is: boolean, content: string]`
|
|
88
|
+
|
|
89
|
+
Get `content` property if it exists, use [`readFileContent`](#read-file-content) to read file from **Filesystem**.
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
const {operator} = require('putout');
|
|
93
|
+
const {getFileContent} = operator;
|
|
94
|
+
|
|
95
|
+
getFileContent(filePath);
|
|
96
|
+
// returns
|
|
97
|
+
[true, 'hello world'];
|
|
98
|
+
```
|
|
99
|
+
|
|
103
100
|
### `removeFile(filePath: Path)`
|
|
104
101
|
|
|
105
102
|
```js
|
package/lib/filesystem.js
CHANGED
|
@@ -15,7 +15,15 @@ const isString = (a) => typeof a === 'string';
|
|
|
15
15
|
const {isArray} = Array;
|
|
16
16
|
const maybeArray = (a) => isArray(a) ? a : [a];
|
|
17
17
|
|
|
18
|
-
const toBase64 = (
|
|
18
|
+
const toBase64 = (content) => {
|
|
19
|
+
const [e, decoded] = tryCatch(btoa, content);
|
|
20
|
+
|
|
21
|
+
if (!e)
|
|
22
|
+
return encodeURI(decoded);
|
|
23
|
+
|
|
24
|
+
return content;
|
|
25
|
+
};
|
|
26
|
+
|
|
19
27
|
const fromBase64 = (content) => {
|
|
20
28
|
const [e, decoded] = tryCatch(atob, content);
|
|
21
29
|
|
|
@@ -93,6 +101,8 @@ function getFileType(filePath) {
|
|
|
93
101
|
return typePath.node.value.value;
|
|
94
102
|
}
|
|
95
103
|
|
|
104
|
+
module.exports.getFileContent = getFileContent;
|
|
105
|
+
|
|
96
106
|
function getFileContent(filePath) {
|
|
97
107
|
const content = getProperty(filePath, 'content');
|
|
98
108
|
|
|
@@ -147,8 +157,8 @@ module.exports.moveFile = (filePath, dirPath) => {
|
|
|
147
157
|
|
|
148
158
|
setLiteralValue(filenamePath.get('value'), newFilename);
|
|
149
159
|
dirPathFiles.node.value.elements.push(filePath.node);
|
|
150
|
-
filePath.remove();
|
|
151
160
|
|
|
161
|
+
filePath.remove();
|
|
152
162
|
maybeFS.renameFile(filename, newFilename);
|
|
153
163
|
};
|
|
154
164
|
|
|
@@ -164,9 +174,9 @@ module.exports.copyFile = (filePath, dirPath) => {
|
|
|
164
174
|
const [hasContent, content] = getFileContent(filePath);
|
|
165
175
|
|
|
166
176
|
const copiedFile = ObjectExpression([
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
hasContent &&
|
|
177
|
+
createTypeProperty('file'),
|
|
178
|
+
createFilenameProperty(newFilename),
|
|
179
|
+
hasContent && createContentProperty(content),
|
|
170
180
|
].filter(Boolean));
|
|
171
181
|
|
|
172
182
|
maybeRemoveFile(dirPath, newFilename);
|
|
@@ -178,6 +188,13 @@ module.exports.copyFile = (filePath, dirPath) => {
|
|
|
178
188
|
};
|
|
179
189
|
|
|
180
190
|
function maybeRemoveFile(dirPath, filename) {
|
|
191
|
+
const type = getFileType(dirPath);
|
|
192
|
+
|
|
193
|
+
if (type !== 'directory') {
|
|
194
|
+
const filename = getFilename(dirPath);
|
|
195
|
+
throw Error(`☝️ Looks like '${filename}' is not a directory, but: '${type}'. Rename to '${filename}/'`);
|
|
196
|
+
}
|
|
197
|
+
|
|
181
198
|
const dirPathFiles = getProperty(dirPath, 'files');
|
|
182
199
|
const [fileToOverwrite] = findFile(dirPathFiles, filename);
|
|
183
200
|
|
|
@@ -187,28 +204,46 @@ function maybeRemoveFile(dirPath, filename) {
|
|
|
187
204
|
fileToOverwrite.remove();
|
|
188
205
|
}
|
|
189
206
|
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
const createContent = (content) => ObjectProperty(StringLiteral('content'), StringLiteral(content));
|
|
207
|
+
const createTypeProperty = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
|
|
208
|
+
|
|
209
|
+
module.exports.createTypeProperty = createTypeProperty;
|
|
194
210
|
|
|
195
|
-
|
|
211
|
+
const createFilesProperty = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
|
|
212
|
+
|
|
213
|
+
module.exports.createFilesProperty = createFilesProperty;
|
|
214
|
+
|
|
215
|
+
const createFilenameProperty = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
|
|
216
|
+
|
|
217
|
+
module.exports.createFilenameProperty = createFilenameProperty;
|
|
218
|
+
|
|
219
|
+
const createContentProperty = (content) => ObjectProperty(StringLiteral('content'), StringLiteral(content));
|
|
220
|
+
|
|
221
|
+
module.exports.createContentProperty = createContentProperty;
|
|
222
|
+
|
|
223
|
+
module.exports.createFile = (dirPath, name, content) => {
|
|
196
224
|
maybeRemoveFile(dirPath, name);
|
|
197
225
|
|
|
198
226
|
const dirPathFiles = getFiles(dirPath);
|
|
199
227
|
const parentFilename = getFilename(dirPath);
|
|
200
228
|
const filename = join(parentFilename, name);
|
|
201
229
|
|
|
202
|
-
const typeProperty =
|
|
203
|
-
const filenameProperty =
|
|
230
|
+
const typeProperty = createTypeProperty('file');
|
|
231
|
+
const filenameProperty = createFilenameProperty(filename);
|
|
204
232
|
|
|
205
|
-
|
|
233
|
+
const properties = [
|
|
234
|
+
typeProperty,
|
|
235
|
+
filenameProperty,
|
|
236
|
+
content && createContentProperty(content),
|
|
237
|
+
].filter(Boolean);
|
|
238
|
+
|
|
239
|
+
dirPathFiles.node.value.elements.push(ObjectExpression(properties));
|
|
206
240
|
|
|
207
241
|
const filePath = dirPathFiles
|
|
208
242
|
.get('value.elements')
|
|
209
243
|
.at(-1);
|
|
210
244
|
|
|
211
|
-
|
|
245
|
+
if (isString(content))
|
|
246
|
+
writeFileContent(filePath, content);
|
|
212
247
|
|
|
213
248
|
return filePath;
|
|
214
249
|
};
|
|
@@ -222,9 +257,9 @@ module.exports.createDirectory = (dirPath, name) => {
|
|
|
222
257
|
const parentFilename = getFilename(dirPath);
|
|
223
258
|
const filename = join(parentFilename, name);
|
|
224
259
|
|
|
225
|
-
const typeProperty =
|
|
226
|
-
const filesProperty =
|
|
227
|
-
const filenameProperty =
|
|
260
|
+
const typeProperty = createTypeProperty('directory');
|
|
261
|
+
const filesProperty = createFilesProperty([]);
|
|
262
|
+
const filenameProperty = createFilenameProperty(filename);
|
|
228
263
|
|
|
229
264
|
dirPathFiles.node.value.elements.push(ObjectExpression([
|
|
230
265
|
typeProperty,
|
|
@@ -239,13 +274,6 @@ module.exports.createDirectory = (dirPath, name) => {
|
|
|
239
274
|
.at(-1);
|
|
240
275
|
};
|
|
241
276
|
|
|
242
|
-
const createContentProperty = (content) => {
|
|
243
|
-
const contentKey = StringLiteral('content');
|
|
244
|
-
const contentValue = StringLiteral(toBase64(content));
|
|
245
|
-
|
|
246
|
-
return ObjectProperty(contentKey, contentValue);
|
|
247
|
-
};
|
|
248
|
-
|
|
249
277
|
module.exports.readFileContent = (filePath) => {
|
|
250
278
|
const fileType = getFileType(filePath);
|
|
251
279
|
|
|
@@ -285,7 +313,7 @@ function writeFileContent(filePath, content) {
|
|
|
285
313
|
return;
|
|
286
314
|
}
|
|
287
315
|
|
|
288
|
-
const property = createContentProperty(content);
|
|
316
|
+
const property = createContentProperty(toBase64(content));
|
|
289
317
|
filePath.node.properties.push(property);
|
|
290
318
|
}
|
|
291
319
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-filesystem",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.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",
|