@putout/engine-runner 20.0.5 → 20.2.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
@@ -214,16 +214,48 @@ module.exports.fix = (rootPath) => {
214
214
  createFile(rootPath, 'hello.txt', 'hello world');
215
215
  };
216
216
 
217
- module.exports.scan = (rootPath) => {
217
+ module.exports.scan = (rootPath, {push}) => {
218
218
  const [filePath] = findFile(rootPath, 'hello.txt');
219
219
 
220
220
  if (filePath)
221
221
  return null;
222
222
 
223
- return rootPath;
223
+ push(rootPath);
224
224
  };
225
225
  ```
226
226
 
227
+ You can also subscribe to `progress` events:
228
+
229
+ - `start`;
230
+ - `end`;
231
+ - `push`;
232
+
233
+ And one more: `file` if your plugin uses `progress` function:
234
+
235
+ ```js
236
+ export const report = () => 'Create file hello.txt';
237
+
238
+ export const fix = (rootPath) => {
239
+ createFile(rootPath, 'hello.txt', 'hello world');
240
+ };
241
+
242
+ export const scan = (rootPath) => {
243
+ const [filePath] = findFile(rootPath, 'hello.txt');
244
+
245
+ if (filePath)
246
+ return null;
247
+
248
+ progress({
249
+ i: 0,
250
+ n: 1,
251
+ });
252
+
253
+ push(filePath);
254
+ };
255
+ ```
256
+
257
+
258
+
227
259
  ### Finder
228
260
 
229
261
  **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,54 @@
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.file = ({i, n, rule}) => {
17
+ progress.emit('file', {
18
+ i,
19
+ n,
20
+ rule,
21
+ });
22
+ };
23
+
24
+ progress.inc = () => {
25
+ ++pluginsCount;
26
+ };
27
+
28
+ progress.start = (rule) => {
29
+ ++pluginsIndex;
30
+ progress.emit('start', {
31
+ pluginsIndex,
32
+ pluginsCount,
33
+ rule,
34
+ });
35
+ };
36
+
37
+ progress.push = (rule) => {
38
+ progress.emit('push', {
39
+ pluginsIndex,
40
+ pluginsCount,
41
+ rule,
42
+ });
43
+ };
44
+
45
+ progress.end = (rule) => {
46
+ progress.emit('end', {
47
+ rule,
48
+ pluginsIndex,
49
+ pluginsCount,
50
+ });
51
+ };
52
+
53
+ return progress;
54
+ };
@@ -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,23 @@ 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 createFileProgress = ({rule, progress}) => ({i, n}) => {
45
+ progress.file({
46
+ i,
47
+ n,
48
+ rule,
49
+ });
50
+ };
51
+
52
+ const getTraverse = ({scan, rule, progress}) => ({push, options}) => ({
37
53
  ['__putout_processor_filesystem(__)'](path) {
38
54
  log(rule);
55
+ progress.start(rule);
39
56
 
40
57
  const rootPath = path.get('arguments.0');
41
58
  const isSimple = fullstore(false);
@@ -47,7 +64,15 @@ const getTraverse = ({scan, rule}) => ({push, options}) => ({
47
64
  });
48
65
 
49
66
  scan(rootPath, {
50
- push,
67
+ push: watchPush({
68
+ push,
69
+ rule,
70
+ progress,
71
+ }),
72
+ progress: createFileProgress({
73
+ rule,
74
+ progress,
75
+ }),
51
76
  options,
52
77
  });
53
78
 
@@ -57,6 +82,8 @@ const getTraverse = ({scan, rule}) => ({push, options}) => ({
57
82
  rootPath,
58
83
  isSimple,
59
84
  });
85
+
86
+ progress.end(rule);
60
87
  },
61
88
  });
62
89
 
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@putout/engine-runner",
3
- "version": "20.0.5",
3
+ "version": "20.2.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,