Skip to content

Commit 7e65604

Browse files
committed
feature: @putout/engine-loader: async-loader: PUTOUT_LOAD_DIR
1 parent 39ff2d2 commit 7e65604

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

packages/engine-loader/lib/load/async-loader.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
const process = require('node:process');
4+
const {join} = require('node:path');
5+
const once = require('once');
36
const {nanomemoize} = require('nano-memoize');
47
const tryToCatch = require('try-to-catch');
58
const {simpleImport} = require('./simple-import');
@@ -14,10 +17,24 @@ module.exports.createAsyncLoader = (type) => nanomemoize(async (name, load) => {
1417
if (name.startsWith('import:')) {
1518
const shortName = name.replace('import:', '');
1619

17-
return await cleverLoad([require.resolve(shortName)], load);
20+
return await cleverLoad([
21+
require.resolve(shortName),
22+
], load);
1823
}
1924

20-
return await cleverLoad([`@putout/${type}-${name}`, `putout-${type}-${name}`], load);
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);
2138
});
2239

2340
async function cleverLoad(names, load = simpleImport) {
@@ -46,3 +63,19 @@ async function cleverLoad(names, load = simpleImport) {
4663

4764
throw e;
4865
}
66+
67+
const getPutoutLoadDir = once(() => process.env.PUTOUT_LOAD_DIR);
68+
69+
function buildPluginsDirs(name) {
70+
const dir = getPutoutLoadDir();
71+
72+
if (!dir)
73+
return [name];
74+
75+
const base = join(dir, name);
76+
77+
return [
78+
base,
79+
`${base}.js`,
80+
];
81+
}

packages/engine-loader/lib/load/async-loader.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const process = require('node:process');
4+
const {join} = require('node:path');
35
const tryToCatch = require('try-to-catch');
46

57
const {test, stub} = require('supertape');
@@ -81,3 +83,23 @@ test('putout: loader: async-loader: calls load', async (t) => {
8183
t.deepEqual(error, Error('@putout/formatter-xxx: LOAD USED'));
8284
t.end();
8385
});
86+
87+
test('putout: engine-loader: async-loader: PUTOUT_LOAD_DIR', async (t) => {
88+
process.env.PUTOUT_LOAD_DIR = join(__dirname, 'fixture');
89+
90+
const {createAsyncLoader} = reRequire('./async-loader');
91+
const loadAsync = createAsyncLoader('plugin');
92+
93+
const {report} = await loadAsync('hello');
94+
95+
stopAll();
96+
reRequire('./async-loader');
97+
98+
delete process.env.PUTOUT_LOAD_DIR;
99+
100+
const result = report();
101+
const expected = 'hello';
102+
103+
t.equal(result, expected);
104+
t.end();
105+
});

0 commit comments

Comments
 (0)