Skip to content

Commit 396d88f

Browse files
committed
feature(putout) getProcessors: sync -> async
1 parent 05963fb commit 396d88f

4 files changed

Lines changed: 32 additions & 142 deletions

File tree

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,28 @@
11
'use strict';
22

33
const tryToCatch = require('try-to-catch');
4+
const {createAsyncLoader} = require('@putout/engine-loader');
45

56
const {
67
NO_FORMATTER,
78
CANNOT_LOAD_FORMATTER,
89
} = require('./exit-codes');
910

10-
const {simpleImportDefault} = require('./simple-import');
11-
12-
const stub = () => () => {};
13-
1411
const {isArray} = Array;
15-
const {assign} = Object;
1612
const maybeArray = (a) => isArray(a) ? a : [a, {}];
1713

18-
module.exports.getFormatter = async (formatter, exit) => {
19-
const [name, formatterOptions] = maybeArray(formatter);
20-
21-
return [
22-
await getReporter(name, exit),
23-
formatterOptions,
24-
];
25-
};
26-
27-
module.exports.getReporter = getReporter;
28-
async function getReporter(name, exit) {
29-
if (name === 'none')
30-
return stub();
14+
module.exports.getFormatter = async (formatterOptional, exit) => {
15+
const [formatterName, formatterOptions] = maybeArray(formatterOptional);
16+
const loadFormatter = createAsyncLoader('formatter');
3117

32-
const [error, reporter] = await loadFormatter([
33-
`@putout/formatter-${name}`,
34-
`putout-formatter-${name}`,
35-
]);
18+
const [error, formatter] = await tryToCatch(loadFormatter, formatterName, exit);
3619

37-
if (reporter)
38-
return reporter;
20+
if (formatter)
21+
return [formatter, formatterOptions];
3922

4023
if (error.code === 'ERR_MODULE_NOT_FOUND')
4124
return exit(NO_FORMATTER, error);
4225

4326
exit(CANNOT_LOAD_FORMATTER, error);
44-
}
45-
46-
async function loadFormatter(names) {
47-
let e;
48-
let reporter;
49-
50-
for (const name of names) {
51-
[e, reporter] = await tryToCatch(simpleImportDefault, name);
52-
53-
if (!e)
54-
return [null, reporter];
55-
56-
if (e.code === 'ERR_MODULE_NOT_FOUND')
57-
continue;
58-
59-
assign(e, {
60-
message: `${name}: ${e.message}`,
61-
});
62-
63-
return [e];
64-
}
65-
66-
assign(e, {
67-
message: e.message.replace(/\simported.*/, ''),
68-
});
69-
70-
return [e];
71-
}
27+
};
7228

packages/putout/lib/cli/formatter.spec.js

Lines changed: 7 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ const {getFormatter} = require('./formatter');
1212

1313
const {reRequire, stopAll} = mockRequire;
1414

15-
const {assign} = Object;
16-
1715
test('putout: cli: formatter: get formatter', async (t) => {
1816
const exit = stub();
1917

@@ -42,88 +40,10 @@ test('putout: cli: formatter: get formatter: options', async (t) => {
4240
t.end();
4341
});
4442

45-
test('putout: cli: formatter: get reporter', async (t) => {
46-
const formatter = stub();
47-
const simpleImportDefault = stub().returns(formatter);
48-
49-
mockRequire('./simple-import', {
50-
simpleImportDefault,
51-
});
52-
53-
const {getReporter} = reRequire('./formatter');
54-
55-
const result = await getReporter('xxx', stub());
56-
57-
stopAll();
58-
reRequire('./formatter');
59-
60-
t.equal(result, formatter);
61-
t.end();
62-
});
63-
64-
test('putout: cli: formatter: calls', async (t) => {
65-
const simpleImportDefault = stub()
66-
.rejects(assign(Error('not found'), {
67-
code: 'ERR_MODULE_NOT_FOUND',
68-
}));
69-
70-
mockRequire('./simple-import', {
71-
simpleImportDefault,
72-
});
73-
74-
const {getReporter} = reRequire('./formatter');
75-
76-
const exit = stub();
77-
await getReporter('xxx', exit);
78-
const expected = [
79-
['@putout/formatter-xxx'],
80-
['putout-formatter-xxx'],
81-
];
82-
83-
stopAll();
84-
reRequire('./formatter');
85-
86-
t.deepEqual(simpleImportDefault.args, expected);
87-
t.end();
88-
});
89-
90-
test('putout: cli: formatter: second import', async (t) => {
91-
let called = false;
92-
93-
const simpleImportDefault = async () => {
94-
if (!called) {
95-
called = true;
96-
const error = assign(Error('not found from test'), {
97-
code: 'ERR_MODULE_NOT_FOUND',
98-
});
99-
100-
throw error;
101-
}
102-
103-
return 'found from test';
104-
};
105-
106-
mockRequire('./simple-import', {
107-
simpleImportDefault,
108-
});
109-
110-
const {getReporter} = reRequire('./formatter');
111-
112-
const exit = stub();
113-
const result = await getReporter('xxx', exit);
114-
const expected = 'found from test';
115-
116-
stopAll();
117-
reRequire('./formatter');
118-
119-
t.deepEqual(result, expected);
120-
t.end();
121-
});
122-
12343
test('putout: cli: formatter: get reporter: exit: NO_FORMATTER', async (t) => {
12444
const exit = stub();
125-
const {getReporter} = reRequire('./formatter');
126-
await getReporter('xxx', exit);
45+
const {getFormatter} = reRequire('./formatter');
46+
await getFormatter('xxx', exit);
12747

12848
const expected = [
12949
NO_FORMATTER,
@@ -139,14 +59,14 @@ test('putout: cli: formatter: get reporter: exit: NO_FORMATTER', async (t) => {
13959

14060
test('putout: cli: formatter: get reporter: exit: CANNOT_LOAD_FORMATTER', async (t) => {
14161
const exit = stub();
142-
const simpleImportDefault = stub().rejects(Error('Syntax error'));
62+
const createAsyncLoader = stub().returns(stub().rejects(Error('@putout/formatter-xxx: Syntax error')));
14363

144-
mockRequire('./simple-import', {
145-
simpleImportDefault,
64+
mockRequire('@putout/engine-loader', {
65+
createAsyncLoader,
14666
});
14767

148-
const {getReporter} = reRequire('./formatter');
149-
await getReporter('xxx', exit);
68+
const {getFormatter} = reRequire('./formatter');
69+
await getFormatter('xxx', exit);
15070

15171
const expected = [
15272
CANNOT_LOAD_FORMATTER,

packages/putout/lib/cli/index.js

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

3+
const tryToCatch = require('try-to-catch');
4+
35
const yargsParser = require('yargs-parser');
46
const {isCI} = require('ci-info');
57
const memo = require('nano-memoize');
@@ -213,7 +215,7 @@ module.exports = async ({argv, halt, log, write, logError, readFile, writeFile})
213215
} = config;
214216

215217
const [currentFormat, formatterOptions] = await getFormatter(format || formatter, exit);
216-
const [error, processorRunners] = tryCatch(getProcessorRunners, processors);
218+
const [error, processorRunners] = await tryToCatch(getProcessorRunners, processors);
217219

218220
if (error)
219221
return exit(CANNOT_LOAD_PROCESSOR, error);
@@ -350,3 +352,4 @@ function addOnce(emitter, name, fn) {
350352
if (!emitter.listenerCount(name))
351353
emitter.on(name, fn);
352354
}
355+

packages/putout/lib/cli/index.spec.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,12 +1446,14 @@ test('putout: cli: not fixable', async (t) => {
14461446
const fileCache = stub().returns({
14471447
setInfo,
14481448
});
1449+
const {getProcessorRunners} = reRequire('@putout/engine-processor');
14491450

14501451
mockRequire('./get-options', getOptions);
14511452
mockRequire('./file-cache', fileCache);
14521453
mockRequire('@putout/engine-processor', {
14531454
runProcessors,
14541455
getFilePatterns,
1456+
getProcessorRunners,
14551457
});
14561458

14571459
const cli = reRequire('.');
@@ -1499,11 +1501,14 @@ test('putout: cli: setInfo: crash', async (t) => {
14991501
setInfo,
15001502
});
15011503

1504+
const {getProcessorRunners} = require('@putout/engine-processor');
1505+
15021506
mockRequire('./get-options', getOptions);
15031507
mockRequire('./file-cache', fileCache);
15041508
mockRequire('@putout/engine-processor', {
15051509
runProcessors,
15061510
getFilePatterns,
1511+
getProcessorRunners,
15071512
});
15081513

15091514
const cli = reRequire('.');
@@ -1827,12 +1832,13 @@ test('putout: cli: cannot load processor: not found', async (t) => {
18271832
const argv = [];
18281833

18291834
const logError = stub();
1830-
const loadProcessors = stub().throws(Error(`Processor "putout-processor-hello" could not be found!`));
1835+
const loadProcessorsAsync = stub().rejects(Error(`Processor "putout-processor-hello" could not be found!`));
18311836

18321837
mockRequire('@putout/engine-loader', {
1833-
loadProcessors,
1838+
loadProcessorsAsync,
18341839
});
18351840

1841+
reRequire('@putout/engine-processor');
18361842
const cli = reRequire('.');
18371843

18381844
await runCli({
@@ -1884,6 +1890,8 @@ test('putout: processor throw', async (t) => {
18841890
],
18851891
});
18861892

1893+
reRequire('@putout/engine-processor');
1894+
18871895
mockRequire('./get-options', getOptions);
18881896

18891897
reRequire('./runner/worker.js');
@@ -1928,6 +1936,8 @@ test('putout: processor throw: raw', async (t) => {
19281936
],
19291937
});
19301938

1939+
reRequire('@putout/engine-processor');
1940+
19311941
mockRequire('./get-options', getOptions);
19321942

19331943
reRequire('./runner/worker.js');
@@ -2014,6 +2024,7 @@ test('putout: processor: invalid config: message', async (t) => {
20142024
};
20152025
});
20162026

2027+
reRequire('@putout/engine-processor');
20172028
reRequire('./runner/worker.js');
20182029
reRequire('./runner/runner.js');
20192030

0 commit comments

Comments
 (0)