@putout/engine-runner 20.0.5 → 20.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.
package/README.md CHANGED
@@ -224,6 +224,12 @@ module.exports.scan = (rootPath) => {
224
224
  };
225
225
  ```
226
226
 
227
+ You can also subscribe to `progress` events:
228
+
229
+ - `start`;
230
+ - `end`;
231
+ - `push`;
232
+
227
233
  ### Finder
228
234
 
229
235
  **Finder** gives you all the control over traversing, but it's the slowest format.
package/lib/index.js CHANGED
@@ -12,20 +12,22 @@ const replace = require('./replace');
12
12
  const declare = require('./declare');
13
13
  const scanner = require('./scanner');
14
14
  const template = require('./template');
15
+ const {createProgress} = require('./progress');
15
16
 
16
17
  const {getPath, getPosition} = require('./get-position');
17
18
 
18
19
  const isRemoved = (a) => a?.removed;
19
20
 
20
- module.exports.runPlugins = ({ast, shebang, fix, fixCount, plugins}) => {
21
+ module.exports.runPlugins = ({ast, shebang, fix, fixCount, plugins, progress = createProgress()}) => {
21
22
  let places = [];
22
23
 
23
24
  const merge = once(mergeVisitors);
24
-
25
25
  const {
26
26
  pluginsFind,
27
27
  pluginsTraverse,
28
- } = splitPlugins(plugins);
28
+ } = splitPlugins(plugins, {
29
+ progress,
30
+ });
29
31
 
30
32
  for (let i = 0; i < fixCount; i++) {
31
33
  places = run({
@@ -38,6 +40,8 @@ module.exports.runPlugins = ({ast, shebang, fix, fixCount, plugins}) => {
38
40
  template,
39
41
  });
40
42
 
43
+ progress.reset();
44
+
41
45
  if (!fix || !places.length)
42
46
  return places;
43
47
 
@@ -142,7 +146,7 @@ function runWithoutMerge({ast, fix, shebang, template, pluginsFind}) {
142
146
  return places;
143
147
  }
144
148
 
145
- function splitPlugins(plugins) {
149
+ function splitPlugins(plugins, {progress}) {
146
150
  const pluginsFind = [];
147
151
  const pluginsTraverse = [];
148
152
 
@@ -175,7 +179,9 @@ function splitPlugins(plugins) {
175
179
  }
176
180
 
177
181
  if (plugin.scan) {
178
- pluginsTraverse.push(scanner(item));
182
+ pluginsTraverse.push(scanner(item, {
183
+ progress,
184
+ }));
179
185
  continue;
180
186
  }
181
187
  }
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ const {EventEmitter} = require('events');
4
+
5
+ module.exports.createProgress = () => {
6
+ let pluginsCount = 0;
7
+ let pluginsIndex = 0;
8
+
9
+ const progress = new EventEmitter();
10
+
11
+ progress.reset = () => {
12
+ pluginsIndex = 0;
13
+ pluginsCount = 0;
14
+ };
15
+
16
+ progress.inc = () => {
17
+ ++pluginsCount;
18
+ };
19
+
20
+ progress.start = (rule) => {
21
+ ++pluginsIndex;
22
+ progress.emit('start', {
23
+ pluginsIndex,
24
+ pluginsCount,
25
+ rule,
26
+ });
27
+ };
28
+
29
+ progress.push = (rule) => {
30
+ progress.emit('push', {
31
+ pluginsIndex,
32
+ pluginsCount,
33
+ rule,
34
+ });
35
+ };
36
+
37
+ progress.end = (rule) => {
38
+ progress.emit('end', {
39
+ rule,
40
+ pluginsIndex,
41
+ pluginsCount,
42
+ });
43
+ };
44
+
45
+ return progress;
46
+ };
@@ -9,16 +9,19 @@ const log = require('debug')('putout:runner:scanner');
9
9
  const fromSimple = require('@putout/plugin-filesystem/from-simple');
10
10
  const toSimple = require('@putout/plugin-filesystem/to-simple');
11
11
 
12
- module.exports = ({rule, plugin, msg, options}) => {
12
+ module.exports = ({rule, plugin, msg, options}, {progress}) => {
13
13
  const {
14
14
  scan,
15
15
  report,
16
16
  fix,
17
17
  } = plugin;
18
18
 
19
+ progress.inc();
20
+
19
21
  const traverse = getTraverse({
20
22
  scan,
21
23
  rule,
24
+ progress,
22
25
  });
23
26
 
24
27
  return {
@@ -33,9 +36,15 @@ module.exports = ({rule, plugin, msg, options}) => {
33
36
  };
34
37
  };
35
38
 
36
- const getTraverse = ({scan, rule}) => ({push, options}) => ({
39
+ const watchPush = ({push, rule, progress}) => (...a) => {
40
+ progress.push(rule);
41
+ push(...a);
42
+ };
43
+
44
+ const getTraverse = ({scan, rule, progress}) => ({push, options}) => ({
37
45
  ['__putout_processor_filesystem(__)'](path) {
38
46
  log(rule);
47
+ progress.start(rule);
39
48
 
40
49
  const rootPath = path.get('arguments.0');
41
50
  const isSimple = fullstore(false);
@@ -47,7 +56,11 @@ const getTraverse = ({scan, rule}) => ({push, options}) => ({
47
56
  });
48
57
 
49
58
  scan(rootPath, {
50
- push,
59
+ push: watchPush({
60
+ push,
61
+ rule,
62
+ progress,
63
+ }),
51
64
  options,
52
65
  });
53
66
 
@@ -57,6 +70,8 @@ const getTraverse = ({scan, rule}) => ({push, options}) => ({
57
70
  rootPath,
58
71
  isSimple,
59
72
  });
73
+
74
+ progress.end(rule);
60
75
  },
61
76
  });
62
77
 
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@putout/engine-runner",
3
- "version": "20.0.5",
3
+ "version": "20.1.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Run 🐊Putout plugins",
7
7
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/engine-runner#readme",
8
- "main": "lib/index.js",
8
+ "main": "./lib/index.js",
9
+ "exports": {
10
+ ".": "./lib/index.js",
11
+ "./progress": "./lib/progress.js"
12
+ },
9
13
  "release": false,
10
14
  "tag": false,
11
15
  "changelog": false,