@putout/engine-loader 6.0.1 → 7.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
@@ -61,17 +61,35 @@ const plugins = loadPlugins({
61
61
  });
62
62
  ```
63
63
 
64
- ### loadProcessors
64
+ ### loadProcessorsAsync
65
65
 
66
66
  ```js
67
67
  const {loadProcessors} = require('@putout/engine-loader');
68
+ const optionalLoad = async (a) => await import(a);
68
69
 
69
- const plugins = loadProcessors({
70
+ const plugins = await loadProcessorsAsync({
70
71
  processors: [
71
72
  ['javascript', 'on'],
72
73
  ['markdown', 'off'],
73
74
  ],
75
+ }, optionalLoad);
76
+ ```
77
+
78
+ ### createAsyncLoader
79
+
80
+ Gives ability to create loader for `processor` or `formatter`.
81
+
82
+ ```js
83
+ const {createAsyncLoader} = require('@putout/engine-loader');
84
+ const {loadProcessor} = createAsyncLoader('processor');
85
+
86
+ await loadProcessors('markdown');
87
+ // loads @putout/processor-markdown
88
+
89
+ await loadProcess('json', () => {
90
+ return Promise.resolve(`will be called instead of 'import'`);
74
91
  });
92
+ // loads @putout/processor-json using custom loader
75
93
  ```
76
94
 
77
95
  ## License
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const tryToCatch = require('try-to-catch');
4
+ const {simpleImport} = require('./simple-import');
5
+
6
+ const {assign} = Object;
7
+ const stub = () => () => {};
8
+
9
+ module.exports.createAsyncLoader = (type) => async (name, load) => {
10
+ if (name === 'none')
11
+ return stub();
12
+
13
+ const [e, reporter] = await cleverLoad([
14
+ `@putout/${type}-${name}`,
15
+ `putout-${type}-${name}`,
16
+ ], load);
17
+
18
+ if (e)
19
+ throw e;
20
+
21
+ return reporter;
22
+ };
23
+
24
+ async function cleverLoad(names, load = simpleImport) {
25
+ let e;
26
+ let reporter;
27
+
28
+ for (const name of names) {
29
+ [e, reporter] = await tryToCatch(load, name);
30
+
31
+ if (!e)
32
+ return [null, reporter];
33
+
34
+ if (e.code === 'ERR_MODULE_NOT_FOUND')
35
+ continue;
36
+
37
+ assign(e, {
38
+ message: `${name}: ${e.message}`,
39
+ });
40
+
41
+ return [e];
42
+ }
43
+
44
+ assign(e, {
45
+ message: e.message.replace(/\simported.*/, ''),
46
+ });
47
+
48
+ return [e];
49
+ }
50
+
@@ -30,6 +30,7 @@ function getChangedLines(lines) {
30
30
  let i = 0;
31
31
  const changedLines = [];
32
32
  let prevState = 0;
33
+
33
34
  for (const [state, line] of lines) {
34
35
  if (state === NOT_CHANGED) {
35
36
  ++i;
package/lib/index.js CHANGED
@@ -3,10 +3,8 @@
3
3
  const memo = require('nano-memoize');
4
4
 
5
5
  const isEnabled = require('./is-enabled');
6
- const {
7
- loadPlugin,
8
- loadProcessor,
9
- } = require('./load');
6
+ const {loadPlugin} = require('./load');
7
+ const {createAsyncLoader} = require('./async-loader');
10
8
  const parsePluginNames = require('./parse-plugin-names');
11
9
  const parseProcessorNames = require('./parse-processor-names');
12
10
  const parseRules = require('./parse-rules');
@@ -40,7 +38,7 @@ const mergeRules = ([rule, plugin], rules) => {
40
38
  };
41
39
  };
42
40
 
43
- module.exports.loadProcessors = memo((options) => {
41
+ module.exports.loadProcessorsAsync = memo(async (options, load) => {
44
42
  check(options);
45
43
 
46
44
  const {
@@ -48,9 +46,9 @@ module.exports.loadProcessors = memo((options) => {
48
46
  } = options;
49
47
 
50
48
  const parsedProcessors = parseProcessorNames(processors);
49
+ const loadProcessor = createAsyncLoader('processor');
51
50
 
52
51
  const list = [];
53
- const namespace = 'putout';
54
52
 
55
53
  for (const [name, fn] of parsedProcessors) {
56
54
  if (fn) {
@@ -58,12 +56,14 @@ module.exports.loadProcessors = memo((options) => {
58
56
  continue;
59
57
  }
60
58
 
61
- list.push(loadProcessor({name, namespace}));
59
+ list.push(loadProcessor(name, load));
62
60
  }
63
61
 
64
- return list;
62
+ return await Promise.all(list);
65
63
  });
66
64
 
65
+ module.exports.createAsyncLoader = createAsyncLoader;
66
+
67
67
  module.exports.loadPlugins = (options) => {
68
68
  check(options);
69
69
 
@@ -122,7 +122,7 @@ function splitRule(rule) {
122
122
  const name = rule
123
123
  .replace('babel/', '');
124
124
 
125
- if (/^babel/.test(rule))
125
+ if (rule.startsWith('babel'))
126
126
  return [
127
127
  name,
128
128
  'babel',
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ // How in other way to mock import using mock require in CommonJS?
4
+ module.exports.simpleImport = async (url) => {
5
+ const result = await import(url);
6
+ return result.default || result;
7
+ };
8
+
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const parse = (rule) => {
4
- if (/^babel\//.test(rule))
4
+ if (rule.startsWith('babel/'))
5
5
  return rule;
6
6
 
7
7
  if (rule.includes('/'))
@@ -42,6 +42,7 @@ const getPlugin = ({name, transform, message}) => ({
42
42
  return;
43
43
 
44
44
  const positions = getPositions(oldCode, newCode);
45
+
45
46
  for (const start of positions) {
46
47
  const node = {
47
48
  loc: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-loader",
3
- "version": "6.0.1",
3
+ "version": "7.1.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "load plugins and prepare them to run",
@@ -29,7 +29,8 @@
29
29
  "diff-match-patch": "^1.0.4",
30
30
  "nano-memoize": "^1.1.8",
31
31
  "once": "^1.4.0",
32
- "try-catch": "^3.0.0"
32
+ "try-catch": "^3.0.0",
33
+ "try-to-catch": "^3.0.1"
33
34
  },
34
35
  "keywords": [
35
36
  "putout",
@@ -39,6 +40,7 @@
39
40
  "devDependencies": {
40
41
  "@babel/plugin-codemod-object-assign-to-object-spread": "^7.7.4",
41
42
  "@cloudcmd/stub": "^3.0.0",
43
+ "@putout/formatter-progress": "*",
42
44
  "@putout/plugin-convert-commonjs-to-esm": "*",
43
45
  "@putout/plugin-remove-unused-variables": "*",
44
46
  "@putout/processor-javascript": "*",
@@ -46,8 +48,8 @@
46
48
  "c8": "^7.5.0",
47
49
  "eslint": "^8.0.1",
48
50
  "eslint-plugin-node": "^11.0.0",
49
- "eslint-plugin-putout": "^14.0.0",
50
- "estrace": "^3.0.2",
51
+ "eslint-plugin-putout": "^15.0.0",
52
+ "estrace": "*",
51
53
  "just-camel-case": "^4.0.2",
52
54
  "lerna": "^4.0.0",
53
55
  "madrun": "^9.0.0",