@putout/operator-filesystem 1.4.0 → 1.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 +15 -2
- package/lib/filesystem.js +44 -0
- package/lib/maybe-fs.js +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,11 +13,24 @@ npm i putout @putout/operator-filesystem
|
|
|
13
13
|
|
|
14
14
|
## API
|
|
15
15
|
|
|
16
|
+
## `createDirectory(directoryPath: Path, name: string)`
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
const {operator} = require('putout');
|
|
20
|
+
const {
|
|
21
|
+
createDirectory,
|
|
22
|
+
findFile,
|
|
23
|
+
} = operator;
|
|
24
|
+
|
|
25
|
+
const [dirPath] = findFile(ast, 'hello');
|
|
26
|
+
createDirectory(dirPath, 'world'); // returns directoryPath
|
|
27
|
+
```
|
|
28
|
+
|
|
16
29
|
## `findFile(path: Path, name: string)`
|
|
17
30
|
|
|
18
31
|
```js
|
|
19
32
|
const {operator} = require('putout');
|
|
20
|
-
const {finedFiles} = operator;
|
|
33
|
+
const {finedFiles, findFile} = operator;
|
|
21
34
|
|
|
22
35
|
const [filePath] = findFile(ast, 'hello');
|
|
23
36
|
```
|
|
@@ -59,7 +72,7 @@ const {
|
|
|
59
72
|
operator,
|
|
60
73
|
} = require('putout');
|
|
61
74
|
|
|
62
|
-
const {renameFile} = operator;
|
|
75
|
+
const {renameFile, findFile} = operator;
|
|
63
76
|
|
|
64
77
|
const ast = parse(montag`
|
|
65
78
|
putout_processor_filesystem({
|
package/lib/filesystem.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const {types} = require('putout');
|
|
3
4
|
const {
|
|
4
5
|
setLiteralValue,
|
|
5
6
|
getProperty,
|
|
@@ -8,6 +9,14 @@ const {
|
|
|
8
9
|
|
|
9
10
|
const maybeFS = require('./maybe-fs');
|
|
10
11
|
|
|
12
|
+
const {
|
|
13
|
+
ObjectExpression,
|
|
14
|
+
ArrayExpression,
|
|
15
|
+
StringLiteral,
|
|
16
|
+
ObjectProperty,
|
|
17
|
+
isProgram,
|
|
18
|
+
} = types;
|
|
19
|
+
|
|
11
20
|
const getRegExp = (wildcard) => {
|
|
12
21
|
const escaped = wildcard
|
|
13
22
|
.replace(/\./g, '\\.')
|
|
@@ -17,6 +26,15 @@ const getRegExp = (wildcard) => {
|
|
|
17
26
|
return RegExp(`${escaped}$`);
|
|
18
27
|
};
|
|
19
28
|
|
|
29
|
+
module.exports.getParentDirectory = (filePath) => {
|
|
30
|
+
const {parentPath} = filePath.parentPath.parentPath;
|
|
31
|
+
|
|
32
|
+
if (isProgram(parentPath))
|
|
33
|
+
return null;
|
|
34
|
+
|
|
35
|
+
return parentPath;
|
|
36
|
+
};
|
|
37
|
+
|
|
20
38
|
module.exports.findFile = (node, name) => {
|
|
21
39
|
const filePaths = [];
|
|
22
40
|
|
|
@@ -82,5 +100,31 @@ module.exports.moveFile = (filePath, dirPath) => {
|
|
|
82
100
|
maybeFS.renameFile(filename, newname);
|
|
83
101
|
};
|
|
84
102
|
|
|
103
|
+
const createType = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
|
|
104
|
+
const createFiles = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
|
|
105
|
+
const createFilename = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
|
|
106
|
+
|
|
107
|
+
module.exports.createDirectory = (dirPath, name) => {
|
|
108
|
+
const dirPathFiles = getProperty(dirPath, 'files');
|
|
109
|
+
const parentFilename = getFilename(dirPath);
|
|
110
|
+
const filename = `${parentFilename}/${name}`;
|
|
111
|
+
|
|
112
|
+
const typeProperty = createType('directory');
|
|
113
|
+
const filesProperty = createFiles([]);
|
|
114
|
+
const filenameProperty = createFilename(filename);
|
|
115
|
+
|
|
116
|
+
dirPathFiles.node.value.elements.push(ObjectExpression([
|
|
117
|
+
typeProperty,
|
|
118
|
+
filenameProperty,
|
|
119
|
+
filesProperty,
|
|
120
|
+
]));
|
|
121
|
+
|
|
122
|
+
maybeFS.createDirectory(filename);
|
|
123
|
+
|
|
124
|
+
return dirPathFiles
|
|
125
|
+
.get('value.elements')
|
|
126
|
+
.at(-1);
|
|
127
|
+
};
|
|
128
|
+
|
|
85
129
|
module.exports.init = maybeFS.init;
|
|
86
130
|
module.exports.deinit = maybeFS.deinit;
|
package/lib/maybe-fs.js
CHANGED
|
@@ -6,6 +6,7 @@ const noop = () => {};
|
|
|
6
6
|
const defaultFS = {
|
|
7
7
|
renameFile: noop,
|
|
8
8
|
removeFile: noop,
|
|
9
|
+
createDirectory: noop,
|
|
9
10
|
};
|
|
10
11
|
|
|
11
12
|
const maybeFS = assign({}, defaultFS);
|
|
@@ -18,6 +19,10 @@ module.exports.removeFile = (name) => {
|
|
|
18
19
|
maybeFS.removeFile(name);
|
|
19
20
|
};
|
|
20
21
|
|
|
22
|
+
module.exports.createDirectory = (name) => {
|
|
23
|
+
maybeFS.createDirectory(name);
|
|
24
|
+
};
|
|
25
|
+
|
|
21
26
|
module.exports.init = (fsDriver) => {
|
|
22
27
|
assign(maybeFS, fsDriver);
|
|
23
28
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-filesystem",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.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",
|