@putout/operator-match-files 2.1.0 → 2.3.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
@@ -48,8 +48,35 @@ And you want to help users avoid updating `.putout.json` config with:
48
48
  }
49
49
  ```
50
50
 
51
+ If you want to pass options use:
52
+
53
+ ```json
54
+ {
55
+ "match": {
56
+ "tsconfig.json": {
57
+ "nextjs/update-tsconfig": ["on", {
58
+ "ignore": []
59
+ }]
60
+ }
61
+ },
62
+ "plugins": ["nextjs"]
63
+ }
64
+ ```
65
+
51
66
  Instead of this, [`redlint`](https://github.com/putoutjs/redlint) can be used, it will generate `.filesystem.json` which can be processed by 🐊**Putout**.
52
67
 
68
+ If you want to save with other name use `->`:
69
+
70
+ ```js
71
+ const {operator} = require('putout');
72
+ const {matchFiles} = operator;
73
+ const updateTSConfig = require('../update-tsconfig');
74
+
75
+ module.exports = matchFiles({
76
+ 'tsconfig.json -> hello.json': updateTSConfig,
77
+ });
78
+ ```
79
+
53
80
  ## License
54
81
 
55
82
  MIT
@@ -3,6 +3,7 @@
3
3
  const {parse, print} = require('@putout/engine-parser');
4
4
  const {transform} = require('putout/transform');
5
5
  const {findPlaces} = require('putout/find-places');
6
+ const ignores = require('putout/ignores');
6
7
 
7
8
  const {toJS, fromJS} = require('@putout/operator-json');
8
9
 
@@ -10,6 +11,9 @@ const {
10
11
  readFileContent,
11
12
  findFile,
12
13
  writeFileContent,
14
+ getFilename,
15
+ createFile,
16
+ getParentDirectory,
13
17
  } = require('@putout/operator-filesystem');
14
18
 
15
19
  const isObject = (a) => a && typeof a === 'object';
@@ -27,45 +31,61 @@ module.exports.matchFiles = (files) => {
27
31
  };
28
32
  };
29
33
 
30
- function fix(path, {filename, matchedJS, matchedAST, plugins}) {
34
+ function fix(path, {outputFilename, matchedJS, matchedAST, plugins}) {
31
35
  transform(matchedAST, matchedJS, {
32
36
  plugins,
33
37
  });
34
38
 
35
- const matchedJSON = magicPrint(filename, matchedAST);
39
+ const matchedJSON = magicPrint(outputFilename, matchedAST);
36
40
 
37
41
  writeFileContent(path, matchedJSON);
38
42
  }
39
43
 
40
- const createScan = (files) => (path, {push, progress}) => {
44
+ const createScan = (files) => (path, {push, progress, options}) => {
41
45
  const allFiles = [];
46
+ const cwd = getFilename(path);
42
47
 
43
48
  for (const [filename, plugin] of entries(files)) {
44
- const files = findFile(path, filename);
49
+ const [matchInputFilename, outputFilename = matchInputFilename] = filename.split(' -> ');
50
+ const inputFiles = findFile(path, matchInputFilename);
45
51
 
46
- for (const file of files) {
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
+ });
61
+
62
+ if (ignores(cwd, inputFilename, options))
63
+ continue;
64
+
47
65
  allFiles.push({
48
66
  plugin,
49
- file,
50
- filename,
67
+ inputFile,
68
+ outputFile,
69
+ inputFilename,
70
+ outputFilename,
51
71
  });
52
72
  }
53
73
  }
54
74
 
55
75
  const n = allFiles.length;
56
76
 
57
- for (const [i, {file, filename, plugin}] of allFiles.entries()) {
77
+ for (const [i, {inputFile, outputFile, inputFilename, outputFilename, plugin}] of allFiles.entries()) {
58
78
  progress({
59
79
  i,
60
80
  n,
61
81
  });
62
82
 
63
- const fileContent = readFileContent(file) || '{}';
64
- const [matchedJS, matchedAST] = magicParse(filename, fileContent);
83
+ const fileContent = readFileContent(inputFile) || '{}';
84
+ const [matchedJS, matchedAST] = magicParse(inputFilename, fileContent);
65
85
 
66
86
  const plugins = [
67
87
  [
68
- `match-file/${filename}`,
88
+ `match-file/${inputFilename}`,
69
89
  plugin,
70
90
  ],
71
91
  ];
@@ -79,8 +99,8 @@ const createScan = (files) => (path, {push, progress}) => {
79
99
 
80
100
  const {message} = places[0];
81
101
 
82
- push(file, {
83
- filename,
102
+ push(outputFile, {
103
+ outputFilename,
84
104
  message,
85
105
  plugins,
86
106
 
@@ -98,6 +118,14 @@ function magicParse(name, content) {
98
118
  return [js, ast];
99
119
  }
100
120
 
121
+ if (/\.(c|m)?ts(x)?$/.test(name)) {
122
+ const ast = parse(content, {
123
+ isTS: true,
124
+ });
125
+
126
+ return [content, ast];
127
+ }
128
+
101
129
  return [content, parse(content)];
102
130
  }
103
131
 
@@ -117,3 +145,15 @@ function check(files) {
117
145
  throw Error(`☝️ Looks like provided to 'matchFiles()' typeof of plugin is not an 'object' but '${typeof plugin}'`);
118
146
  }
119
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-match-files",
3
- "version": "2.1.0",
3
+ "version": "2.3.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",
@@ -39,7 +39,7 @@
39
39
  "@putout/plugin-nodejs": "*",
40
40
  "@putout/test": "^8.0.0",
41
41
  "c8": "^8.0.0",
42
- "eslint": "^8.0.1",
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",