@putout/engine-loader 16.2.1 → 16.3.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.
@@ -2,42 +2,49 @@
2
2
 
3
3
  const process = require('node:process');
4
4
  const {join} = require('node:path');
5
- const once = require('once');
6
- const {nanomemoize} = require('nano-memoize');
5
+
6
+ const {nanomemoize: _nanomemoize} = require('nano-memoize');
7
7
  const tryToCatch = require('try-to-catch');
8
- const {simpleImport} = require('./simple-import');
8
+ const {simpleImport: _simpleImport} = require('./simple-import');
9
9
 
10
10
  const {assign} = Object;
11
11
  const stub = () => () => {};
12
12
 
13
- module.exports.createAsyncLoader = (type) => nanomemoize(async (name, load) => {
14
- if (name === 'none')
15
- return stub();
13
+ module.exports.createAsyncLoader = (type, overrides = {}) => {
14
+ const {
15
+ simpleImport = _simpleImport,
16
+ nanomemoize = _nanomemoize,
17
+ } = overrides;
16
18
 
17
- if (name.startsWith('import:')) {
18
- const shortName = name.replace('import:', '');
19
+ return nanomemoize(async (name) => {
20
+ if (name === 'none')
21
+ return stub();
19
22
 
20
- return await cleverLoad([
21
- require.resolve(shortName),
22
- ], load);
23
- }
24
-
25
- const namesBase = [
26
- `@putout/${type}-${name}`,
27
- `putout-${type}-${name}`,
28
- ];
29
-
30
- const namesFromPluginsDirs = namesBase.flatMap(buildPluginsDirs);
31
-
32
- const names = Array.from(new Set([
33
- ...namesBase,
34
- ...namesFromPluginsDirs,
35
- ]));
36
-
37
- return await cleverLoad(names, load);
38
- });
23
+ if (name.startsWith('import:')) {
24
+ const shortName = name.replace('import:', '');
25
+
26
+ return await cleverLoad([
27
+ require.resolve(shortName),
28
+ ], simpleImport);
29
+ }
30
+
31
+ const namesBase = [
32
+ `@putout/${type}-${name}`,
33
+ `putout-${type}-${name}`,
34
+ ];
35
+
36
+ const namesFromPluginsDirs = namesBase.flatMap(buildPluginsDirs);
37
+
38
+ const names = Array.from(new Set([
39
+ ...namesBase,
40
+ ...namesFromPluginsDirs,
41
+ ]));
42
+
43
+ return await cleverLoad(names, simpleImport);
44
+ });
45
+ };
39
46
 
40
- async function cleverLoad(names, load = simpleImport) {
47
+ async function cleverLoad(names, load) {
41
48
  let e;
42
49
  let reporter;
43
50
 
@@ -73,7 +80,7 @@ async function cleverLoad(names, load = simpleImport) {
73
80
  throw e;
74
81
  }
75
82
 
76
- const getPutoutLoadDir = once(() => process.env.PUTOUT_LOAD_DIR);
83
+ const getPutoutLoadDir = () => process.env.PUTOUT_LOAD_DIR;
77
84
 
78
85
  function buildPluginsDirs(name) {
79
86
  const dir = getPutoutLoadDir();
package/lib/load/load.js CHANGED
@@ -1,30 +1,30 @@
1
1
  'use strict';
2
2
 
3
3
  const process = require('node:process');
4
- const {createRequire} = require('node:module');
4
+ const {createRequire: _createRequire} = require('node:module');
5
5
  const {join} = require('node:path');
6
6
  const tryCatch = require('try-catch');
7
- const once = require('once');
8
- const {assign} = Object;
9
7
 
10
8
  const bigFirst = (a) => `${a[0].toUpperCase()}${a.slice(1)}`;
11
9
 
12
- const load = (type) => ({name, namespace}) => {
13
- const [pluginPath, customRequire] = getPath(namespace, type, name);
10
+ const load = (type) => (overrides) => {
11
+ const {
12
+ name,
13
+ namespace,
14
+ getModulePath = _getModulePath,
15
+ createRequire = _createRequire,
16
+ } = overrides;
17
+
18
+ const [pluginPath, customRequire] = getPath(namespace, type, name, {
19
+ getModulePath,
20
+ createRequire,
21
+ });
14
22
 
15
23
  if (!pluginPath)
16
24
  throw Error(`${bigFirst(type)} "${namespace}-${type}-${name}" could not be found!`);
17
25
 
18
26
  const [error, result] = tryCatch(customRequire, pluginPath);
19
27
 
20
- /* c8 ignore start */
21
- if (error?.code === 'ERR_REQUIRE_ESM')
22
- assign(error, {
23
- message: `☝️ Looks like '${name}' is ESM, use 'await putoutAsync()' instead`,
24
- name,
25
- });
26
-
27
- /* c8 ignore end */
28
28
  if (error)
29
29
  throw error;
30
30
 
@@ -34,27 +34,35 @@ const load = (type) => ({name, namespace}) => {
34
34
  module.exports.loadPlugin = load('plugin');
35
35
  module.exports.loadProcessor = load('processor');
36
36
 
37
- function getPath(namespace, type, name) {
37
+ function getPath(namespace, type, name, overrides) {
38
+ const {getModulePath, createRequire} = overrides;
39
+
38
40
  if (name.startsWith('import:'))
39
- return getModulePath(name.replace('import:', ''));
41
+ return getModulePath(name.replace('import:', ''), {
42
+ createRequire,
43
+ });
40
44
 
41
- let [path, customRequire] = getModulePath(`@${namespace}/${type}-${name}`);
45
+ let [path, customRequire] = getModulePath(`@${namespace}/${type}-${name}`, {
46
+ createRequire,
47
+ });
42
48
 
43
49
  if (!path)
44
- [path, customRequire] = getModulePath(`${namespace}-${type}-${name}`);
50
+ [path, customRequire] = getModulePath(`${namespace}-${type}-${name}`, {
51
+ createRequire,
52
+ });
45
53
 
46
54
  if (!path)
47
- [path, customRequire] = getModulePath(name);
55
+ [path, customRequire] = getModulePath(name, {
56
+ createRequire,
57
+ });
48
58
 
49
59
  return [path, customRequire];
50
60
  }
51
61
 
52
- const {
53
- PUTOUT_YARN_PNP = 'putout',
54
- } = process.env;
62
+ const {env} = process;
55
63
 
56
- const createCustomRequire = once(() => createRequire(require.resolve(PUTOUT_YARN_PNP)));
57
- const createPutoutRequire = once(() => createRequire(require.resolve('putout')));
64
+ const createCustomRequire = (createRequire) => createRequire(require.resolve(env.PUTOUT_YARN_PNP || 'putout'));
65
+ const createPutoutRequire = (createRequire) => createRequire(require.resolve('putout'));
58
66
 
59
67
  // That's all for Yarn P'n'P
60
68
  //
@@ -63,11 +71,12 @@ const createPutoutRequire = once(() => createRequire(require.resolve('putout')))
63
71
  // - declared in module that want to extend 🐊Putout;
64
72
  //
65
73
  // https://yarnpkg.com/advanced/rulebook#modules-shouldnt-hardcode-node_modules-paths-to-access-other-modules
66
- function getModulePath(name, {again = false} = {}) {
74
+ function _getModulePath(name, overrides = {}) {
75
+ const {again = false, createRequire} = overrides;
67
76
  let path;
68
77
 
69
- const customRequire = createCustomRequire();
70
- const putoutRequire = createPutoutRequire();
78
+ const customRequire = createCustomRequire(createRequire);
79
+ const putoutRequire = createPutoutRequire(createRequire);
71
80
 
72
81
  [, path] = tryCatch(putoutRequire.resolve, name);
73
82
 
@@ -77,14 +86,15 @@ function getModulePath(name, {again = false} = {}) {
77
86
  [, path] = tryCatch(customRequire.resolve, name);
78
87
 
79
88
  if (!path && !again)
80
- return getModulePath(buildPluginsDir(name), {
89
+ return _getModulePath(buildPluginsDir(name), {
81
90
  again: true,
91
+ createRequire,
82
92
  });
83
93
 
84
94
  return [path, customRequire];
85
95
  }
86
96
 
87
- const getPutoutLoadDir = once(() => process.env.PUTOUT_LOAD_DIR);
97
+ const getPutoutLoadDir = () => process.env.PUTOUT_LOAD_DIR;
88
98
 
89
99
  function buildPluginsDir(name) {
90
100
  const dir = getPutoutLoadDir();
@@ -1,17 +1,19 @@
1
1
  'use strict';
2
2
 
3
3
  const {createAsyncLoader} = require('../load/async-loader');
4
-
5
4
  const parseProcessorNames = require('../processors/parse-processor-names');
6
5
 
7
6
  const {check} = require('../check');
8
7
 
9
- module.exports.loadProcessorsAsync = async (options, load) => {
8
+ module.exports.loadProcessorsAsync = async (options, simpleImport) => {
10
9
  check(options);
11
10
 
12
11
  const {processors = []} = options;
13
12
  const parsedProcessors = parseProcessorNames(processors);
14
- const loadProcessor = createAsyncLoader('processor');
13
+
14
+ const loadProcessor = createAsyncLoader('processor', {
15
+ simpleImport,
16
+ });
15
17
 
16
18
  const list = [];
17
19
 
@@ -21,7 +23,7 @@ module.exports.loadProcessorsAsync = async (options, load) => {
21
23
  continue;
22
24
  }
23
25
 
24
- list.push(loadProcessor(name, load));
26
+ list.push(loadProcessor(name));
25
27
  }
26
28
 
27
29
  return await Promise.all(list);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-loader",
3
- "version": "16.2.1",
3
+ "version": "16.3.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",
@@ -26,7 +26,6 @@
26
26
  "dependencies": {
27
27
  "diff-match-patch": "^1.0.4",
28
28
  "nano-memoize": "^3.0.11",
29
- "once": "^1.4.0",
30
29
  "try-catch": "^3.0.0",
31
30
  "try-to-catch": "^3.0.1"
32
31
  },
@@ -42,19 +41,17 @@
42
41
  "@putout/eslint-flat": "^3.0.0",
43
42
  "@putout/formatter-progress": "*",
44
43
  "@putout/plugin-apply-nullish-coalescing": "*",
45
- "@putout/plugin-convert-commonjs-to-esm": "*",
46
44
  "@putout/plugin-nodejs": "*",
47
45
  "@putout/plugin-remove-debugger": "*",
48
46
  "@putout/processor-javascript": "*",
49
47
  "@putout/processor-markdown": "*",
50
48
  "c8": "^10.0.0",
51
- "eslint": "^9.0.0",
49
+ "eslint": "^10.0.0-alpha.0",
52
50
  "eslint-plugin-n": "^17.0.0",
53
- "eslint-plugin-putout": "^28.0.0",
51
+ "eslint-plugin-putout": "^29.0.0",
54
52
  "estrace": "^6.0.0",
55
53
  "just-camel-case": "^6.2.0",
56
54
  "madrun": "^11.0.0",
57
- "mock-require": "^3.0.3",
58
55
  "montag": "^1.0.0",
59
56
  "nodemon": "^3.0.1",
60
57
  "putout": "*",