@putout/operator-match-files 2.0.1 → 2.1.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.
Files changed (2) hide show
  1. package/lib/match-files.js +54 -41
  2. package/package.json +2 -2
@@ -4,11 +4,7 @@ const {parse, print} = require('@putout/engine-parser');
4
4
  const {transform} = require('putout/transform');
5
5
  const {findPlaces} = require('putout/find-places');
6
6
 
7
- const {
8
- __filesystem,
9
- toJS,
10
- fromJS,
11
- } = require('@putout/operator-json');
7
+ const {toJS, fromJS} = require('@putout/operator-json');
12
8
 
13
9
  const {
14
10
  readFileContent,
@@ -18,20 +14,20 @@ const {
18
14
 
19
15
  const isObject = (a) => a && typeof a === 'object';
20
16
  const {entries} = Object;
21
- const report = ({message}) => message;
17
+ const report = (path, {message}) => message;
22
18
 
23
19
  module.exports.matchFiles = (files) => {
24
20
  check(files);
25
- const traverse = createTraverse(files);
21
+ const scan = createScan(files);
26
22
 
27
23
  return {
28
24
  fix,
29
- traverse,
25
+ scan,
30
26
  report,
31
27
  };
32
28
  };
33
29
 
34
- function fix({filename, path, matchedJS, matchedAST, plugins}) {
30
+ function fix(path, {filename, matchedJS, matchedAST, plugins}) {
35
31
  transform(matchedAST, matchedJS, {
36
32
  plugins,
37
33
  });
@@ -41,41 +37,58 @@ function fix({filename, path, matchedJS, matchedAST, plugins}) {
41
37
  writeFileContent(path, matchedJSON);
42
38
  }
43
39
 
44
- const createTraverse = (files) => ({push}) => ({
45
- [__filesystem]: (path) => {
46
- for (const [filename, plugin] of entries(files)) {
47
- const [filePath] = findFile(path, filename);
48
-
49
- if (!filePath)
50
- return;
51
-
52
- const fileContent = readFileContent(filePath) || '{}';
53
- const [matchedJS, matchedAST] = magicParse(filename, fileContent);
54
-
55
- const plugins = [
56
- [`match-file/${filename}`, plugin],
57
- ];
58
-
59
- const places = findPlaces(matchedAST, matchedJS, {
60
- plugins,
61
- });
62
-
63
- if (!places.length)
64
- continue;
65
-
66
- const {message} = places[0];
67
-
68
- push({
40
+ const createScan = (files) => (path, {push, progress}) => {
41
+ const allFiles = [];
42
+
43
+ for (const [filename, plugin] of entries(files)) {
44
+ const files = findFile(path, filename);
45
+
46
+ for (const file of files) {
47
+ allFiles.push({
48
+ plugin,
49
+ file,
69
50
  filename,
70
- message,
71
- plugins,
72
- path: filePath,
73
- matchedAST,
74
- matchedJS,
75
51
  });
76
52
  }
77
- },
78
- });
53
+ }
54
+
55
+ const n = allFiles.length;
56
+
57
+ for (const [i, {file, filename, plugin}] of allFiles.entries()) {
58
+ progress({
59
+ i,
60
+ n,
61
+ });
62
+
63
+ const fileContent = readFileContent(file) || '{}';
64
+ const [matchedJS, matchedAST] = magicParse(filename, fileContent);
65
+
66
+ const plugins = [
67
+ [
68
+ `match-file/${filename}`,
69
+ plugin,
70
+ ],
71
+ ];
72
+
73
+ const places = findPlaces(matchedAST, matchedJS, {
74
+ plugins,
75
+ });
76
+
77
+ if (!places.length)
78
+ continue;
79
+
80
+ const {message} = places[0];
81
+
82
+ push(file, {
83
+ filename,
84
+ message,
85
+ plugins,
86
+
87
+ matchedAST,
88
+ matchedJS,
89
+ });
90
+ }
91
+ };
79
92
 
80
93
  function magicParse(name, content) {
81
94
  if (/\.json$/.test(name)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-match-files",
3
- "version": "2.0.1",
3
+ "version": "2.1.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",
@@ -24,7 +24,7 @@
24
24
  "report": "madrun report"
25
25
  },
26
26
  "dependencies": {
27
- "@putout/babel": "^1.5.5",
27
+ "@putout/babel": "^2.0.0",
28
28
  "@putout/engine-parser": "^10.0.2",
29
29
  "@putout/operator-filesystem": "^3.0.0",
30
30
  "@putout/operator-json": "^1.3.0"