@putout/operator-match-files 3.3.0 → 3.5.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 +20 -4
- package/lib/match-files.js +28 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -96,15 +96,31 @@ You can even use file matchers:
|
|
|
96
96
|
Matchers: `__filename = __name.__ext`
|
|
97
97
|
|
|
98
98
|
```js
|
|
99
|
-
const {operator} = require('putout');
|
|
100
|
-
const {matchFiles} = operator;
|
|
101
|
-
const updateTSConfig = require('../update-tsconfig');
|
|
102
|
-
|
|
103
99
|
module.exports = matchFiles({
|
|
104
100
|
'__name.json -> __name.js': updateTSConfig,
|
|
105
101
|
});
|
|
106
102
|
```
|
|
107
103
|
|
|
104
|
+
### Options
|
|
105
|
+
|
|
106
|
+
You can also pass `options`:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
module.exports = matchFiles({
|
|
110
|
+
'.eslintrc.json -> eslint.config.js': {
|
|
111
|
+
rules: {
|
|
112
|
+
'eslint/declare': {
|
|
113
|
+
type: 'esm',
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
plugins: [
|
|
117
|
+
['eslint/convert-rc-to-flat', rcToFlat],
|
|
118
|
+
['eslint/declare', declare],
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
108
124
|
## License
|
|
109
125
|
|
|
110
126
|
MIT
|
package/lib/match-files.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const path = require('node:path');
|
|
4
4
|
|
|
5
5
|
const {parse, print} = require('@putout/engine-parser');
|
|
6
6
|
const {transform} = require('putout/transform');
|
|
@@ -19,6 +19,8 @@ const {
|
|
|
19
19
|
getParentDirectory,
|
|
20
20
|
} = require('@putout/operator-filesystem');
|
|
21
21
|
|
|
22
|
+
const {join} = path;
|
|
23
|
+
|
|
22
24
|
const isObject = (a) => a && typeof a === 'object';
|
|
23
25
|
const {entries} = Object;
|
|
24
26
|
const report = (path, {message}) => message;
|
|
@@ -34,10 +36,8 @@ module.exports.matchFiles = (files) => {
|
|
|
34
36
|
};
|
|
35
37
|
};
|
|
36
38
|
|
|
37
|
-
function fix(inputFile, {dirPath, mainPath, matchInputFilename, outputFilename, matchedJS, matchedAST,
|
|
38
|
-
transform(matchedAST, matchedJS,
|
|
39
|
-
plugins,
|
|
40
|
-
});
|
|
39
|
+
function fix(inputFile, {dirPath, mainPath, matchInputFilename, outputFilename, matchedJS, matchedAST, options}) {
|
|
40
|
+
transform(matchedAST, matchedJS, options);
|
|
41
41
|
|
|
42
42
|
const matchedJSON = magicPrint(outputFilename, matchedAST);
|
|
43
43
|
const outputFile = getOutputFile(mainPath, {
|
|
@@ -57,7 +57,7 @@ const createScan = (files) => (mainPath, {push, progress, options}) => {
|
|
|
57
57
|
const allFiles = [];
|
|
58
58
|
const cwd = getFilename(mainPath);
|
|
59
59
|
|
|
60
|
-
for (const [filename,
|
|
60
|
+
for (const [filename, rawOptions] of entries(files)) {
|
|
61
61
|
const [matchInputFilename, outputFilename = matchInputFilename] = parseMatcher(filename, options);
|
|
62
62
|
const inputFiles = findFile(mainPath, matchInputFilename);
|
|
63
63
|
|
|
@@ -72,7 +72,7 @@ const createScan = (files) => (mainPath, {push, progress, options}) => {
|
|
|
72
72
|
mainPath,
|
|
73
73
|
dirPath,
|
|
74
74
|
matchInputFilename,
|
|
75
|
-
|
|
75
|
+
rawOptions,
|
|
76
76
|
inputFile,
|
|
77
77
|
inputFilename,
|
|
78
78
|
outputFilename,
|
|
@@ -89,7 +89,7 @@ const createScan = (files) => (mainPath, {push, progress, options}) => {
|
|
|
89
89
|
inputFile,
|
|
90
90
|
inputFilename,
|
|
91
91
|
outputFilename,
|
|
92
|
-
|
|
92
|
+
rawOptions,
|
|
93
93
|
mainPath,
|
|
94
94
|
} = current;
|
|
95
95
|
|
|
@@ -101,14 +101,8 @@ const createScan = (files) => (mainPath, {push, progress, options}) => {
|
|
|
101
101
|
const fileContent = readFileContent(inputFile) || '{}';
|
|
102
102
|
const [matchedJS, matchedAST] = magicParse(inputFilename, fileContent);
|
|
103
103
|
|
|
104
|
-
const
|
|
105
|
-
const
|
|
106
|
-
[name, plugin],
|
|
107
|
-
];
|
|
108
|
-
|
|
109
|
-
const places = findPlaces(matchedAST, matchedJS, {
|
|
110
|
-
plugins,
|
|
111
|
-
});
|
|
104
|
+
const options = parseOptions(inputFilename, rawOptions);
|
|
105
|
+
const places = findPlaces(matchedAST, matchedJS, options);
|
|
112
106
|
|
|
113
107
|
if (!places.length)
|
|
114
108
|
continue;
|
|
@@ -122,7 +116,7 @@ const createScan = (files) => (mainPath, {push, progress, options}) => {
|
|
|
122
116
|
|
|
123
117
|
outputFilename,
|
|
124
118
|
message,
|
|
125
|
-
|
|
119
|
+
options,
|
|
126
120
|
|
|
127
121
|
matchedAST,
|
|
128
122
|
matchedJS,
|
|
@@ -185,11 +179,25 @@ function parseMatcher(matcher, options) {
|
|
|
185
179
|
if (!filename)
|
|
186
180
|
return matcher.split(' -> ');
|
|
187
181
|
|
|
188
|
-
const ext =
|
|
189
|
-
const shortName = filename.replace(ext, '');
|
|
182
|
+
const {ext, name} = path.parse(filename);
|
|
190
183
|
|
|
191
|
-
matcher = matcher.replaceAll(`__name`,
|
|
184
|
+
matcher = matcher.replaceAll(`__name`, name);
|
|
192
185
|
matcher = matcher.replaceAll(`__ext`, ext);
|
|
193
186
|
|
|
194
187
|
return matcher.split(' -> ');
|
|
195
188
|
}
|
|
189
|
+
|
|
190
|
+
function parseOptions(inputFilename, rawOptions) {
|
|
191
|
+
if (rawOptions.plugins)
|
|
192
|
+
return rawOptions;
|
|
193
|
+
|
|
194
|
+
const name = `match-file: ${inputFilename}`;
|
|
195
|
+
|
|
196
|
+
const plugins = [
|
|
197
|
+
[name, rawOptions],
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
plugins,
|
|
202
|
+
};
|
|
203
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-match-files",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.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",
|