@putout/operator-match-files 3.2.2 → 3.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 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
@@ -1,10 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- join,
5
- basename,
6
- extname,
7
- } = require('node:path');
3
+ const {join, extname} = require('node:path');
8
4
 
9
5
  const {parse, print} = require('@putout/engine-parser');
10
6
  const {transform} = require('putout/transform');
@@ -38,44 +34,44 @@ module.exports.matchFiles = (files) => {
38
34
  };
39
35
  };
40
36
 
41
- function fix(path, {inputFile, outputFilename, matchedJS, matchedAST, plugins}) {
42
- transform(matchedAST, matchedJS, {
43
- plugins,
44
- });
37
+ function fix(inputFile, {dirPath, mainPath, matchInputFilename, outputFilename, matchedJS, matchedAST, options}) {
38
+ transform(matchedAST, matchedJS, options);
45
39
 
46
40
  const matchedJSON = magicPrint(outputFilename, matchedAST);
41
+ const outputFile = getOutputFile(mainPath, {
42
+ dirPath,
43
+ matchInputFilename,
44
+ outputFilename,
45
+ inputFile,
46
+ });
47
47
 
48
- writeFileContent(path, matchedJSON);
48
+ writeFileContent(outputFile, matchedJSON);
49
49
 
50
- if (path !== inputFile)
50
+ if (inputFile !== outputFile)
51
51
  removeFile(inputFile);
52
52
  }
53
53
 
54
- const createScan = (files) => (path, {push, progress, options}) => {
54
+ const createScan = (files) => (mainPath, {push, progress, options}) => {
55
55
  const allFiles = [];
56
- const cwd = getFilename(path);
56
+ const cwd = getFilename(mainPath);
57
57
 
58
- for (const [filename, plugin] of entries(files)) {
58
+ for (const [filename, rawOptions] of entries(files)) {
59
59
  const [matchInputFilename, outputFilename = matchInputFilename] = parseMatcher(filename, options);
60
- const inputFiles = findFile(path, matchInputFilename);
60
+ const inputFiles = findFile(mainPath, matchInputFilename);
61
61
 
62
62
  for (const inputFile of inputFiles) {
63
63
  const dirPath = getParentDirectory(inputFile);
64
64
  const inputFilename = getFilename(inputFile);
65
- const outputFile = getOutputFile(path, {
66
- dirPath,
67
- matchInputFilename,
68
- outputFilename,
69
- inputFile,
70
- });
71
65
 
72
66
  if (ignores(cwd, inputFilename, options))
73
67
  continue;
74
68
 
75
69
  allFiles.push({
76
- plugin,
70
+ mainPath,
71
+ dirPath,
72
+ matchInputFilename,
73
+ rawOptions,
77
74
  inputFile,
78
- outputFile,
79
75
  inputFilename,
80
76
  outputFilename,
81
77
  });
@@ -84,7 +80,17 @@ const createScan = (files) => (path, {push, progress, options}) => {
84
80
 
85
81
  const n = allFiles.length;
86
82
 
87
- for (const [i, {inputFile, outputFile, inputFilename, outputFilename, plugin}] of allFiles.entries()) {
83
+ for (const [i, current] of allFiles.entries()) {
84
+ const {
85
+ dirPath,
86
+ matchInputFilename,
87
+ inputFile,
88
+ inputFilename,
89
+ outputFilename,
90
+ rawOptions,
91
+ mainPath,
92
+ } = current;
93
+
88
94
  progress({
89
95
  i,
90
96
  n,
@@ -93,25 +99,22 @@ const createScan = (files) => (path, {push, progress, options}) => {
93
99
  const fileContent = readFileContent(inputFile) || '{}';
94
100
  const [matchedJS, matchedAST] = magicParse(inputFilename, fileContent);
95
101
 
96
- const name = `match-file: ${inputFilename}`;
97
- const plugins = [
98
- [name, plugin],
99
- ];
100
-
101
- const places = findPlaces(matchedAST, matchedJS, {
102
- plugins,
103
- });
102
+ const options = parseOptions(inputFilename, rawOptions);
103
+ const places = findPlaces(matchedAST, matchedJS, options);
104
104
 
105
105
  if (!places.length)
106
106
  continue;
107
107
 
108
108
  const {message} = places[0];
109
109
 
110
- push(outputFile, {
111
- inputFile,
110
+ push(inputFile, {
111
+ dirPath,
112
+ mainPath,
113
+ matchInputFilename,
114
+
112
115
  outputFilename,
113
116
  message,
114
- plugins,
117
+ options,
115
118
 
116
119
  matchedAST,
117
120
  matchedJS,
@@ -169,15 +172,31 @@ function getOutputFile(path, {dirPath, matchInputFilename, outputFilename, input
169
172
  }
170
173
 
171
174
  function parseMatcher(matcher, options) {
172
- for (const [name, value] of entries(options)) {
173
- if (name === 'filename') {
174
- const ext = extname(value);
175
- const name = value.replace(ext, '');
176
-
177
- matcher = matcher.replaceAll(`__name`, name);
178
- matcher = matcher.replaceAll(`__ext`, ext);
179
- }
180
- }
175
+ const {filename} = options;
176
+
177
+ if (!filename)
178
+ return matcher.split(' -> ');
179
+
180
+ const ext = extname(filename);
181
+ const shortName = filename.replace(ext, '');
182
+
183
+ matcher = matcher.replaceAll(`__name`, shortName);
184
+ matcher = matcher.replaceAll(`__ext`, ext);
181
185
 
182
186
  return matcher.split(' -> ');
183
187
  }
188
+
189
+ function parseOptions(inputFilename, rawOptions) {
190
+ if (rawOptions.plugins)
191
+ return rawOptions;
192
+
193
+ const name = `match-file: ${inputFilename}`;
194
+
195
+ const plugins = [
196
+ [name, rawOptions],
197
+ ];
198
+
199
+ return {
200
+ plugins,
201
+ };
202
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-match-files",
3
- "version": "3.2.2",
3
+ "version": "3.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",