@putout/engine-loader 5.1.0 → 7.0.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
@@ -3,7 +3,7 @@
3
3
  [NPMIMGURL]: https://img.shields.io/npm/v/@putout/engine-loader.svg?style=flat&longCache=true
4
4
  [NPMURL]: https://npmjs.org/package/@putout/engine-loader"npm"
5
5
 
6
- Load 🐊[`Putout`](https://github.com/coderaiser/putout) `Plugins` and `Processors`.
6
+ Load 🐊[**Putout**](https://github.com/coderaiser/putout) `Plugins` and `Processors`.
7
7
 
8
8
  ## Install
9
9
 
@@ -61,12 +61,12 @@ 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
68
 
69
- const plugins = loadProcessors({
69
+ const plugins = await loadProcessorsAsync({
70
70
  processors: [
71
71
  ['javascript', 'on'],
72
72
  ['markdown', 'off'],
@@ -74,6 +74,18 @@ const plugins = loadProcessors({
74
74
  });
75
75
  ```
76
76
 
77
+ ### createAsyncLoader
78
+
79
+ Gives ability to create loader for `processor` or `formatter`.
80
+
81
+ ```js
82
+ const {createAsyncLoader} = require('@putout/engine-loader');
83
+ const {loadProcessor} = createAsyncLoader('processor');
84
+
85
+ await loadProcessors('markdown');
86
+ // loads @putout/processor-markdown
87
+ ```
88
+
77
89
  ## License
78
90
 
79
91
  MIT
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ const tryToCatch = require('try-to-catch');
4
+ const {simpleImportDefault} = require('./simple-import');
5
+
6
+ const {assign} = Object;
7
+ const stub = () => () => {};
8
+
9
+ module.exports.createAsyncLoader = (type) => async (name) => {
10
+ if (name === 'none')
11
+ return stub();
12
+
13
+ const [e, reporter] = await cleverLoad([
14
+ `@putout/${type}-${name}`,
15
+ `putout-${type}-${name}`,
16
+ ]);
17
+
18
+ if (e)
19
+ throw e;
20
+
21
+ return reporter;
22
+ };
23
+
24
+ async function cleverLoad(names) {
25
+ let e;
26
+ let reporter;
27
+
28
+ for (const name of names) {
29
+ [e, reporter] = await tryToCatch(simpleImportDefault, 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
+ }
@@ -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) => {
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));
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,5 @@
1
+ 'use strict';
2
+
3
+ // How in other way to mock import using mock require in CommonJS?
4
+ module.exports.simpleImportDefault = async (url) => (await import(url)).default;
5
+
@@ -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('/'))
@@ -9,7 +9,7 @@ const babelTransform = require('./transforms/babel');
9
9
 
10
10
  const getMessage = (a) => a
11
11
  .replace(/@babel\/plugin-|babel-plugin-/, '')
12
- .replace(/-/g, ' ');
12
+ .replaceAll('-', ' ');
13
13
 
14
14
  const getModulePath = (name) => {
15
15
  const [, path] = tryCatch(require.resolve, name);
@@ -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": "5.1.0",
3
+ "version": "7.0.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",
@@ -25,11 +25,12 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@babel/core": "^7.12.3",
28
- "@putout/engine-parser": "^4.0.1",
28
+ "@putout/engine-parser": "^5.0.0",
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,20 +48,20 @@
46
48
  "c8": "^7.5.0",
47
49
  "eslint": "^8.0.1",
48
50
  "eslint-plugin-node": "^11.0.0",
49
- "eslint-plugin-putout": "^13.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
- "madrun": "^8.0.1",
55
+ "madrun": "^9.0.0",
54
56
  "mock-require": "^3.0.3",
55
57
  "montag": "^1.0.0",
56
58
  "nodemon": "^2.0.1",
57
59
  "putout": "*",
58
- "supertape": "^6.0.0"
60
+ "supertape": "^7.0.0"
59
61
  },
60
62
  "license": "MIT",
61
63
  "engines": {
62
- "node": ">=14"
64
+ "node": ">=16"
63
65
  },
64
66
  "publishConfig": {
65
67
  "access": "public"