@putout/engine-loader 4.9.0 → 4.12.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 +5 -0
- package/lib/is-enabled.js +3 -1
- package/lib/load.js +47 -15
- package/lib/wrap-plugin.js +7 -1
- package/package.json +4 -3
- package/lib/get-module-path.js +0 -9
package/README.md
CHANGED
|
@@ -11,6 +11,11 @@ Load putout `plugins`, `processors`.
|
|
|
11
11
|
npm i @putout/engine-loader
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
+
## Env Variables
|
|
15
|
+
|
|
16
|
+
When you need to get things working with Yarn PnP, and using custom `plugins` `formatters` or `processers`, add env variable
|
|
17
|
+
`PUTOUT_YARN_PNP` with name of a package that contains dependencies you need.
|
|
18
|
+
|
|
14
19
|
## Code Example
|
|
15
20
|
|
|
16
21
|
### loadPlugins
|
package/lib/is-enabled.js
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
const {isArray} = Array;
|
|
4
4
|
|
|
5
|
+
const isBool = (a) => typeof a === 'boolean';
|
|
6
|
+
|
|
5
7
|
module.exports = (name, rules) => {
|
|
6
8
|
if (isArray(name))
|
|
7
9
|
[name] = name;
|
|
8
10
|
|
|
9
|
-
if (
|
|
11
|
+
if (isBool(rules[name]))
|
|
10
12
|
return rules[name];
|
|
11
13
|
|
|
12
14
|
let resultState = true;
|
package/lib/load.js
CHANGED
|
@@ -1,34 +1,66 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const {createRequire} = require('module');
|
|
4
|
+
|
|
5
|
+
const tryCatch = require('try-catch');
|
|
6
|
+
const once = require('once');
|
|
7
|
+
|
|
3
8
|
const wrapPlugin = require('./wrap-plugin');
|
|
4
|
-
const getModulePath = require('./get-module-path');
|
|
5
9
|
|
|
6
10
|
const bigFirst = (a) => `${a[0].toUpperCase()}${a.slice(1)}`;
|
|
7
11
|
|
|
8
|
-
function getPath(namespace, type, name) {
|
|
9
|
-
let path = getModulePath(`@${namespace}/${type}-${name}`);
|
|
10
|
-
|
|
11
|
-
if (!path)
|
|
12
|
-
path = getModulePath(`${namespace}-${type}-${name}`);
|
|
13
|
-
|
|
14
|
-
if (!path)
|
|
15
|
-
path = getModulePath(name);
|
|
16
|
-
|
|
17
|
-
return path;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
12
|
const load = (type) => ({name, namespace}) => {
|
|
21
13
|
if (namespace !== 'putout')
|
|
22
14
|
return wrapPlugin(name, namespace);
|
|
23
15
|
|
|
24
|
-
const pluginPath = getPath(namespace, type, name);
|
|
16
|
+
const [pluginPath, customRequire] = getPath(namespace, type, name);
|
|
25
17
|
|
|
26
18
|
if (!pluginPath)
|
|
27
19
|
throw Error(`${bigFirst(type)} "${namespace}-${type}-${name}" could not be found!`);
|
|
28
20
|
|
|
29
|
-
return
|
|
21
|
+
return customRequire(pluginPath);
|
|
30
22
|
};
|
|
31
23
|
|
|
32
24
|
module.exports.loadPlugin = load('plugin');
|
|
33
25
|
module.exports.loadProcessor = load('processor');
|
|
34
26
|
|
|
27
|
+
function getPath(namespace, type, name) {
|
|
28
|
+
let [path, customRequire] = getModulePath(`@${namespace}/${type}-${name}`);
|
|
29
|
+
|
|
30
|
+
if (!path)
|
|
31
|
+
[path, customRequire] = getModulePath(`${namespace}-${type}-${name}`);
|
|
32
|
+
|
|
33
|
+
if (!path)
|
|
34
|
+
[path, customRequire] = getModulePath(name);
|
|
35
|
+
|
|
36
|
+
return [path, customRequire];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const {PUTOUT_YARN_PNP = 'putout'} = process.env;
|
|
40
|
+
|
|
41
|
+
const createCustomRequire = once(() => createRequire(require.resolve(PUTOUT_YARN_PNP)));
|
|
42
|
+
const createPutoutRequire = once(() => createRequire(require.resolve('putout')));
|
|
43
|
+
|
|
44
|
+
// That's all for Yarn P'n'P
|
|
45
|
+
//
|
|
46
|
+
// We need to create a couple version of require for plugins, formatters and processors:
|
|
47
|
+
// - declared in 🐊Putout package.json;
|
|
48
|
+
// - declared in module that want to extend 🐊Putout;
|
|
49
|
+
//
|
|
50
|
+
// https://yarnpkg.com/advanced/rulebook#modules-shouldnt-hardcode-node_modules-paths-to-access-other-modules
|
|
51
|
+
function getModulePath(name) {
|
|
52
|
+
let path;
|
|
53
|
+
|
|
54
|
+
const customRequire = createCustomRequire();
|
|
55
|
+
const putoutRequire = createPutoutRequire();
|
|
56
|
+
|
|
57
|
+
[, path] = tryCatch(putoutRequire.resolve, name);
|
|
58
|
+
|
|
59
|
+
if (path)
|
|
60
|
+
return [path, putoutRequire];
|
|
61
|
+
|
|
62
|
+
[, path] = tryCatch(customRequire.resolve, name);
|
|
63
|
+
|
|
64
|
+
return [path, customRequire];
|
|
65
|
+
}
|
|
66
|
+
|
package/lib/wrap-plugin.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const tryCatch = require('try-catch');
|
|
3
4
|
const {print} = require('@putout/engine-parser');
|
|
5
|
+
|
|
4
6
|
const getPositions = require('./get-positions-by-diff');
|
|
5
|
-
const getModulePath = require('./get-module-path');
|
|
6
7
|
|
|
7
8
|
const babelTransform = require('./transforms/babel');
|
|
8
9
|
const jscodeshiftTransform = require('./transforms/jscodeshift');
|
|
@@ -11,6 +12,11 @@ const getMessage = (a) => a
|
|
|
11
12
|
.replace(/@babel\/plugin-|babel-plugin-/, '')
|
|
12
13
|
.replace(/-/g, ' ');
|
|
13
14
|
|
|
15
|
+
const getModulePath = (name) => {
|
|
16
|
+
const [, path] = tryCatch(require.resolve, name);
|
|
17
|
+
return path;
|
|
18
|
+
};
|
|
19
|
+
|
|
14
20
|
module.exports = (name, namespace) => {
|
|
15
21
|
const message = getMessage(name);
|
|
16
22
|
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/engine-loader",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.12.0",
|
|
4
4
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
5
5
|
"description": "load plugins and prepare them to run",
|
|
6
|
-
"homepage": "
|
|
6
|
+
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/engine-loader",
|
|
7
7
|
"main": "lib/index.js",
|
|
8
8
|
"release": false,
|
|
9
9
|
"tag": false,
|
|
@@ -42,10 +42,11 @@
|
|
|
42
42
|
"@putout/plugin-convert-commonjs-to-esm": "*",
|
|
43
43
|
"@putout/plugin-remove-unused-variables": "*",
|
|
44
44
|
"@putout/processor-markdown": "*",
|
|
45
|
+
"@putout/processor-javascript": "*",
|
|
45
46
|
"c8": "^7.5.0",
|
|
46
47
|
"eslint": "^8.0.1",
|
|
47
48
|
"eslint-plugin-node": "^11.0.0",
|
|
48
|
-
"eslint-plugin-putout": "^
|
|
49
|
+
"eslint-plugin-putout": "^12.0.0",
|
|
49
50
|
"estrace": "^3.0.2",
|
|
50
51
|
"js-codemod": "^7.0.0",
|
|
51
52
|
"just-camel-case": "^4.0.2",
|