@putout/operator-filesystem 1.0.1 → 1.2.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 +22 -4
- package/lib/filesystem.js +32 -5
- package/lib/maybe-fs.js +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,16 +13,34 @@ npm i putout @putout/operator-filesystem
|
|
|
13
13
|
|
|
14
14
|
## API
|
|
15
15
|
|
|
16
|
-
##
|
|
16
|
+
## `findFile(path: Path, name: string)`
|
|
17
17
|
|
|
18
18
|
```js
|
|
19
19
|
const {operator} = require('putout');
|
|
20
20
|
const {finedFiles} = operator;
|
|
21
21
|
|
|
22
|
-
const [filePath] =
|
|
22
|
+
const [filePath] = findFile(ast, 'hello');
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
###
|
|
25
|
+
### `getFilename(path: Path)`
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const {operator} = require('putout');
|
|
29
|
+
const {getFilename} = operator;
|
|
30
|
+
|
|
31
|
+
const name = getFilename(filePath);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### `removeFile(filePath: Path)`
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
const {operator} = require('putout');
|
|
38
|
+
const {removeFile} = operator;
|
|
39
|
+
|
|
40
|
+
removeFile(filePath);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### `renameFile(filePath: Path, name: string)`
|
|
26
44
|
|
|
27
45
|
```js
|
|
28
46
|
const montag = require('montag');
|
|
@@ -42,7 +60,7 @@ const ast = parse(montag`
|
|
|
42
60
|
});
|
|
43
61
|
`);
|
|
44
62
|
|
|
45
|
-
const [filePath] =
|
|
63
|
+
const [filePath] = findFile(ast, 'hello');
|
|
46
64
|
|
|
47
65
|
renameFile(filePath, 'world');
|
|
48
66
|
|
package/lib/filesystem.js
CHANGED
|
@@ -8,13 +8,22 @@ const {
|
|
|
8
8
|
|
|
9
9
|
const maybeFS = require('./maybe-fs');
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
const getRegExp = (wildcard) => {
|
|
12
|
+
const escaped = wildcard
|
|
13
|
+
.replace(/\./g, '\\.')
|
|
14
|
+
.replace(/\*/g, '.*')
|
|
15
|
+
.replace('?', '.?');
|
|
16
|
+
|
|
17
|
+
return RegExp(`${escaped}$`);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports.findFile = (node, name) => {
|
|
12
21
|
const filePaths = [];
|
|
13
22
|
|
|
14
23
|
for (const filenamePath of traverseProperties(node, 'filename')) {
|
|
15
24
|
const {value} = filenamePath.node.value;
|
|
16
25
|
|
|
17
|
-
if (value === name ||
|
|
26
|
+
if (value === name || getRegExp(name).test(value)) {
|
|
18
27
|
filePaths.push(filenamePath.parentPath);
|
|
19
28
|
}
|
|
20
29
|
}
|
|
@@ -22,11 +31,22 @@ module.exports.findFiles = (node, name) => {
|
|
|
22
31
|
return filePaths;
|
|
23
32
|
};
|
|
24
33
|
|
|
25
|
-
|
|
34
|
+
function getFilenamePath(filePath) {
|
|
26
35
|
const filenamePath = getProperty(filePath, 'filename');
|
|
27
|
-
const valuePath = filenamePath.get('value');
|
|
28
|
-
const {value: oldName} = filenamePath.node.value;
|
|
29
36
|
|
|
37
|
+
return filenamePath.get('value');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getFilename(filePath) {
|
|
41
|
+
const {value} = getFilenamePath(filePath).node;
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports.getFilename = getFilename;
|
|
46
|
+
|
|
47
|
+
module.exports.renameFile = (filePath, name) => {
|
|
48
|
+
const oldName = getFilename(filePath);
|
|
49
|
+
const valuePath = getFilenamePath(filePath);
|
|
30
50
|
const baseName = oldName
|
|
31
51
|
.split('/')
|
|
32
52
|
.pop();
|
|
@@ -37,5 +57,12 @@ module.exports.renameFile = (filePath, name) => {
|
|
|
37
57
|
maybeFS.renameFile(oldName, newName);
|
|
38
58
|
};
|
|
39
59
|
|
|
60
|
+
module.exports.removeFile = (filePath) => {
|
|
61
|
+
const filename = getFilename(filePath);
|
|
62
|
+
|
|
63
|
+
filePath.remove();
|
|
64
|
+
maybeFS.removeFile(filename);
|
|
65
|
+
};
|
|
66
|
+
|
|
40
67
|
module.exports.init = maybeFS.init;
|
|
41
68
|
module.exports.deinit = maybeFS.deinit;
|
package/lib/maybe-fs.js
CHANGED
|
@@ -5,6 +5,7 @@ const noop = () => {};
|
|
|
5
5
|
|
|
6
6
|
const defaultFS = {
|
|
7
7
|
renameFile: noop,
|
|
8
|
+
removeFile: noop,
|
|
8
9
|
};
|
|
9
10
|
|
|
10
11
|
const maybeFS = assign({}, defaultFS);
|
|
@@ -13,6 +14,10 @@ module.exports.renameFile = (oldName, newName) => {
|
|
|
13
14
|
maybeFS.renameFile(oldName, newName);
|
|
14
15
|
};
|
|
15
16
|
|
|
17
|
+
module.exports.removeFile = (name) => {
|
|
18
|
+
maybeFS.removeFile(name);
|
|
19
|
+
};
|
|
20
|
+
|
|
16
21
|
module.exports.init = (fsDriver) => {
|
|
17
22
|
assign(maybeFS, fsDriver);
|
|
18
23
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-filesystem",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.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",
|