Skip to content

Commit

Permalink
feature(putout) add support of yarn PnP by formatters loader (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Dec 15, 2021
1 parent 02e522a commit b878e04
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
7 changes: 4 additions & 3 deletions packages/putout/lib/cli/formatter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

const {createSimport} = require('simport');
const tryToCatch = require('try-to-catch');

const {
NO_FORMATTER,
CANNOT_LOAD_FORMATTER,
} = require('./exit-codes');

const simport = createSimport(__filename);
const simpleImport = require('./simple-import');

const stub = () => () => {};

const {isArray} = Array;
Expand Down Expand Up @@ -48,7 +48,7 @@ async function loadFormatter(names) {
let reporter;

for (const name of names) {
[e, reporter] = await tryToCatch(simport, name);
[e, reporter] = await tryToCatch(simpleImport, name);

if (!e)
return [null, reporter];
Expand All @@ -69,3 +69,4 @@ async function loadFormatter(names) {

return [e];
}

24 changes: 6 additions & 18 deletions packages/putout/lib/cli/formatter.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const {createSimport} = require('simport');

const test = require('supertape');
const stub = require('@cloudcmd/stub');
const mockRequire = require('mock-require');
Expand All @@ -14,14 +12,12 @@ const {getFormatter} = require('./formatter');

const {reRequire, stopAll} = mockRequire;

const simport = createSimport(__filename);

const {assign} = Object;

test('putout: cli: formatter: get formatter', async (t) => {
const exit = stub();

const progress = await simport('@putout/formatter-progress');
const {default: progress} = await import('@putout/formatter-progress');

const result = await getFormatter('progress', exit);
const expected = [progress, {}];
Expand All @@ -37,7 +33,7 @@ test('putout: cli: formatter: get formatter: options', async (t) => {
minCount: 10,
};

const progress = await simport('@putout/formatter-progress');
const {default: progress} = await import('@putout/formatter-progress');

const result = await getFormatter(['progress', formatterOptions], exit);
const expected = [progress, formatterOptions];
Expand All @@ -50,9 +46,7 @@ test('putout: cli: formatter: get reporter', async (t) => {
const formatter = stub();
const simport = stub().returns(formatter);

mockRequire('simport', {
createSimport: stub().returns(simport),
});
mockRequire('./simple-import', simport);

const {getReporter} = reRequire('./formatter');

Expand All @@ -71,9 +65,7 @@ test('putout: cli: formatter: calls', async (t) => {
code: 'ERR_MODULE_NOT_FOUND',
}));

mockRequire('simport', {
createSimport: stub().returns(simport),
});
mockRequire('./simple-import', simport);

const {getReporter} = reRequire('./formatter');

Expand Down Expand Up @@ -107,9 +99,7 @@ test('putout: cli: formatter: second import', async (t) => {
return 'found from test';
};

mockRequire('simport', {
createSimport: stub().returns(simport),
});
mockRequire('./simple-import', simport);

const {getReporter} = reRequire('./formatter');

Expand Down Expand Up @@ -145,9 +135,7 @@ test('putout: cli: formatter: get reporter: exit: CANNOT_LOAD_FORMATTER', async
const exit = stub();
const simport = stub().rejects(Error('Syntax error'));

mockRequire('simport', {
createSimport: stub().returns(simport),
});
mockRequire('./simple-import', simport);

const {getReporter} = reRequire('./formatter');
await getReporter('xxx', exit);
Expand Down
7 changes: 7 additions & 0 deletions packages/putout/lib/cli/simple-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

// yarn doesn't understand how simport works
// https://github.com/coderaiser/putout/issues/93

module.exports = async (url) => (await import(url)).default;

0 comments on commit b878e04

Please sign in to comment.