@putout/operator-match-files 2.2.0 → 2.4.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 +40 -0
- package/lib/match-files.js +51 -14
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -65,6 +65,46 @@ If you want to pass options use:
|
|
|
65
65
|
|
|
66
66
|
Instead of this, [`redlint`](https://github.com/putoutjs/redlint) can be used, it will generate `.filesystem.json` which can be processed by 🐊**Putout**.
|
|
67
67
|
|
|
68
|
+
### Rename
|
|
69
|
+
|
|
70
|
+
If you want to save with other name use `->`:
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
const {operator} = require('putout');
|
|
74
|
+
const {matchFiles} = operator;
|
|
75
|
+
const updateTSConfig = require('../update-tsconfig');
|
|
76
|
+
|
|
77
|
+
module.exports = matchFiles({
|
|
78
|
+
'tsconfig.json -> hello.json': updateTSConfig,
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Matcher
|
|
83
|
+
|
|
84
|
+
You can even use file matchers:
|
|
85
|
+
|
|
86
|
+
```json
|
|
87
|
+
{
|
|
88
|
+
"rules": {
|
|
89
|
+
"filesystem/convert-json-to-js": ["on", {
|
|
90
|
+
"filename": "package.json"
|
|
91
|
+
}]
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Matchers: `__filename = __name.__ext`
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
const {operator} = require('putout');
|
|
100
|
+
const {matchFiles} = operator;
|
|
101
|
+
const updateTSConfig = require('../update-tsconfig');
|
|
102
|
+
|
|
103
|
+
module.exports = matchFiles({
|
|
104
|
+
'__name.json -> __name.js': updateTSConfig,
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
68
108
|
## License
|
|
69
109
|
|
|
70
110
|
MIT
|
package/lib/match-files.js
CHANGED
|
@@ -12,6 +12,8 @@ const {
|
|
|
12
12
|
findFile,
|
|
13
13
|
writeFileContent,
|
|
14
14
|
getFilename,
|
|
15
|
+
createFile,
|
|
16
|
+
getParentDirectory,
|
|
15
17
|
} = require('@putout/operator-filesystem');
|
|
16
18
|
|
|
17
19
|
const isObject = (a) => a && typeof a === 'object';
|
|
@@ -29,12 +31,12 @@ module.exports.matchFiles = (files) => {
|
|
|
29
31
|
};
|
|
30
32
|
};
|
|
31
33
|
|
|
32
|
-
function fix(path, {
|
|
34
|
+
function fix(path, {outputFilename, matchedJS, matchedAST, plugins}) {
|
|
33
35
|
transform(matchedAST, matchedJS, {
|
|
34
36
|
plugins,
|
|
35
37
|
});
|
|
36
38
|
|
|
37
|
-
const matchedJSON = magicPrint(
|
|
39
|
+
const matchedJSON = magicPrint(outputFilename, matchedAST);
|
|
38
40
|
|
|
39
41
|
writeFileContent(path, matchedJSON);
|
|
40
42
|
}
|
|
@@ -44,36 +46,46 @@ const createScan = (files) => (path, {push, progress, options}) => {
|
|
|
44
46
|
const cwd = getFilename(path);
|
|
45
47
|
|
|
46
48
|
for (const [filename, plugin] of entries(files)) {
|
|
47
|
-
const
|
|
49
|
+
const [matchInputFilename, outputFilename = matchInputFilename] = parseMatcher(filename, options);
|
|
50
|
+
const inputFiles = findFile(path, matchInputFilename);
|
|
48
51
|
|
|
49
|
-
for (const
|
|
50
|
-
const
|
|
52
|
+
for (const inputFile of inputFiles) {
|
|
53
|
+
const dirPath = getParentDirectory(inputFile);
|
|
54
|
+
const inputFilename = getFilename(inputFile);
|
|
55
|
+
const outputFile = getOutputFile(path, {
|
|
56
|
+
dirPath,
|
|
57
|
+
matchInputFilename,
|
|
58
|
+
outputFilename,
|
|
59
|
+
inputFile,
|
|
60
|
+
});
|
|
51
61
|
|
|
52
|
-
if (ignores(cwd,
|
|
62
|
+
if (ignores(cwd, inputFilename, options))
|
|
53
63
|
continue;
|
|
54
64
|
|
|
55
65
|
allFiles.push({
|
|
56
66
|
plugin,
|
|
57
|
-
|
|
58
|
-
|
|
67
|
+
inputFile,
|
|
68
|
+
outputFile,
|
|
69
|
+
inputFilename,
|
|
70
|
+
outputFilename,
|
|
59
71
|
});
|
|
60
72
|
}
|
|
61
73
|
}
|
|
62
74
|
|
|
63
75
|
const n = allFiles.length;
|
|
64
76
|
|
|
65
|
-
for (const [i, {
|
|
77
|
+
for (const [i, {inputFile, outputFile, inputFilename, outputFilename, plugin}] of allFiles.entries()) {
|
|
66
78
|
progress({
|
|
67
79
|
i,
|
|
68
80
|
n,
|
|
69
81
|
});
|
|
70
82
|
|
|
71
|
-
const fileContent = readFileContent(
|
|
72
|
-
const [matchedJS, matchedAST] = magicParse(
|
|
83
|
+
const fileContent = readFileContent(inputFile) || '{}';
|
|
84
|
+
const [matchedJS, matchedAST] = magicParse(inputFilename, fileContent);
|
|
73
85
|
|
|
74
86
|
const plugins = [
|
|
75
87
|
[
|
|
76
|
-
`match-file/${
|
|
88
|
+
`match-file/${inputFilename}`,
|
|
77
89
|
plugin,
|
|
78
90
|
],
|
|
79
91
|
];
|
|
@@ -87,8 +99,8 @@ const createScan = (files) => (path, {push, progress, options}) => {
|
|
|
87
99
|
|
|
88
100
|
const {message} = places[0];
|
|
89
101
|
|
|
90
|
-
push(
|
|
91
|
-
|
|
102
|
+
push(outputFile, {
|
|
103
|
+
outputFilename,
|
|
92
104
|
message,
|
|
93
105
|
plugins,
|
|
94
106
|
|
|
@@ -133,3 +145,28 @@ function check(files) {
|
|
|
133
145
|
throw Error(`☝️ Looks like provided to 'matchFiles()' typeof of plugin is not an 'object' but '${typeof plugin}'`);
|
|
134
146
|
}
|
|
135
147
|
}
|
|
148
|
+
|
|
149
|
+
function getOutputFile(path, {dirPath, matchInputFilename, outputFilename, inputFile}) {
|
|
150
|
+
if (matchInputFilename === outputFilename)
|
|
151
|
+
return inputFile;
|
|
152
|
+
|
|
153
|
+
const [outputFile] = findFile(dirPath, outputFilename);
|
|
154
|
+
|
|
155
|
+
if (outputFile)
|
|
156
|
+
return outputFile;
|
|
157
|
+
|
|
158
|
+
return createFile(dirPath, outputFilename);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function parseMatcher(matcher, options) {
|
|
162
|
+
for (const [name, value] of entries(options)) {
|
|
163
|
+
if (name === 'filename') {
|
|
164
|
+
const [name, ext] = value.split('.');
|
|
165
|
+
|
|
166
|
+
matcher = matcher.replaceAll(`__name`, name);
|
|
167
|
+
matcher = matcher.replaceAll(`__ext`, ext);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return matcher.split(' -> ');
|
|
172
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-match-files",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.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 match files to plugins",
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@putout/plugin-nodejs": "*",
|
|
40
40
|
"@putout/test": "^8.0.0",
|
|
41
|
-
"c8": "^
|
|
42
|
-
"eslint": "^
|
|
41
|
+
"c8": "^9.0.0",
|
|
42
|
+
"eslint": "^9.0.0-alpha.0",
|
|
43
43
|
"eslint-plugin-n": "^16.0.0",
|
|
44
44
|
"eslint-plugin-putout": "^22.0.0",
|
|
45
45
|
"lerna": "^6.0.1",
|