Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loading custom formatter from package #6228

Merged
merged 9 commits into from
Jul 29, 2022
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ node_modules
.eslintcache
yarn.lock
.vscode/settings.json
.idea
35 changes: 35 additions & 0 deletions lib/__tests__/resolveCustomFormatter.test.js
@@ -0,0 +1,35 @@
const fs = require('fs');
const path = require('path');
const { resolveCustomFormatter } = require('../resolveCustomFormatter');
JounQin marked this conversation as resolved.
Show resolved Hide resolved

jest.mock('fs');
jest.mock('path');
JounQin marked this conversation as resolved.
Show resolved Hide resolved

describe('resolveCustomFormatter', () => {
it('should return absolute path when provided path is a file path', () => {
const aRelativePath = 'a/relative/path';
const expected = `/cwd/${aRelativePath}`;

path.resolve.mockReturnValue(expected);
path.isAbsolute.mockReturnValue(false);
fs.existsSync.mockReturnValue(true);

const result = resolveCustomFormatter(aRelativePath);

expect(result).toEqual(expected);
});

it('should return provided path when path is neither absolute nor relative', () => {
const aModulePath = '@stylelint/prettier-config/index.js';

path.isAbsolute.mockReturnValue(false);
JounQin marked this conversation as resolved.
Show resolved Hide resolved
fs.existsSync.mockReturnValue(false);

const result = resolveCustomFormatter(aModulePath);

const realPathModule = jest.requireActual('path');
const expectedPath = realPathModule.join('node_modules', aModulePath);
JounQin marked this conversation as resolved.
Show resolved Hide resolved

expect(result).toEqual(expect.stringMatching(RegExp(`${expectedPath}$`)));
JounQin marked this conversation as resolved.
Show resolved Hide resolved
JounQin marked this conversation as resolved.
Show resolved Hide resolved
});
});
5 changes: 2 additions & 3 deletions lib/cli.js
Expand Up @@ -13,6 +13,7 @@ const printConfig = require('./printConfig');
const resolveFrom = require('resolve-from');
const standalone = require('./standalone');
const writeOutputFile = require('./writeOutputFile');
const { resolveCustomFormatter } = require('./resolveCustomFormatter');
JounQin marked this conversation as resolved.
Show resolved Hide resolved

const EXIT_CODE_ERROR = 2;

Expand Down Expand Up @@ -338,9 +339,7 @@ module.exports = async (argv) => {
let formatter = cli.flags.formatter;

if (cli.flags.customFormatter) {
const customFormatter = path.isAbsolute(cli.flags.customFormatter)
? cli.flags.customFormatter
: path.join(process.cwd(), cli.flags.customFormatter);
const customFormatter = resolveCustomFormatter(cli.flags.customFormatter);

JounQin marked this conversation as resolved.
Show resolved Hide resolved
formatter = require(customFormatter);
}
Expand Down
20 changes: 20 additions & 0 deletions lib/resolveCustomFormatter.js
@@ -0,0 +1,20 @@
const fs = require('fs');
const path = require('path');

/**
* @param {string} formatterPath
* @returns {string}
*/
function resolveCustomFormatter(formatterPath) {
const resolvedPath = path.resolve(formatterPath);

if (fs.existsSync(resolvedPath)) {
return resolvedPath;
}

return require.resolve(formatterPath);
}

module.exports = {
resolveCustomFormatter,
};
JounQin marked this conversation as resolved.
Show resolved Hide resolved