Skip to content

Commit baed8b1

Browse files
byCedricmarionebl
authored andcommitted
fix: improve format module resolving (#464)
* fix(cli): improve format module resolving * fix(load): resolve formatter module from config dir if available * fix(cli): replace try catch resolve with silent resolve from
1 parent 7524fc4 commit baed8b1

File tree

8 files changed

+66
-7
lines changed

8 files changed

+66
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function (report) {
2+
return 'custom-formatter-ok';
3+
}

@commitlint/cli/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
"get-stdin": "5.0.1",
8585
"lodash.merge": "4.6.1",
8686
"lodash.pick": "4.4.0",
87-
"meow": "5.0.0"
87+
"meow": "5.0.0",
88+
"resolve-from": "^4.0.0",
89+
"resolve-global": "^0.1.0"
8890
}
8991
}

@commitlint/cli/src/cli.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const meow = require('meow');
88
const merge = require('lodash.merge');
99
const pick = require('lodash.pick');
1010
const stdin = require('get-stdin');
11+
const resolveFrom = require('resolve-from');
12+
const resolveGlobal = require('resolve-global');
1113

1214
const pkg = require('../package');
1315
const help = require('./help');
@@ -262,15 +264,16 @@ function selectParserOpts(parserPreset) {
262264

263265
function loadFormatter(config, flags) {
264266
const moduleName = flags.format || config.formatter;
265-
let modulePath;
267+
const modulePath =
268+
resolveFrom.silent(__dirname, moduleName) ||
269+
resolveFrom.silent(flags.cwd, moduleName) ||
270+
resolveGlobal.silent(moduleName);
266271

267-
try {
268-
modulePath = require.resolve(`${moduleName}`);
269-
} catch (error) {
270-
throw new Error(`Using format ${moduleName}, but cannot find the module.`);
272+
if (modulePath) {
273+
return require(modulePath);
271274
}
272275

273-
return require(modulePath);
276+
throw new Error(`Using format ${moduleName}, but cannot find the module.`);
274277
}
275278

276279
// Catch unhandled rejections globally

@commitlint/cli/src/cli.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,23 @@ test('should fail for invalid formatters from flags', async t => {
263263
t.is(actual.code, 1);
264264
});
265265

266+
test('should work with absolute formatter path', async t => {
267+
const formatterPath = path.resolve(__dirname, '../fixtures/custom-formatter/formatters/custom.js');
268+
const cwd = await git.bootstrap('fixtures/custom-formatter');
269+
const actual = await cli(['--format', formatterPath], {cwd})('test: this should work');
270+
271+
t.true(actual.stdout.includes('custom-formatter-ok'));
272+
t.is(actual.code, 0);
273+
});
274+
275+
test('should work with relative formatter path', async t => {
276+
const cwd = path.resolve(await git.bootstrap('fixtures/custom-formatter'), './formatters');
277+
const actual = await cli(['--format', './custom.js'], {cwd})('test: this should work');
278+
279+
t.true(actual.stdout.includes('custom-formatter-ok'));
280+
t.is(actual.code, 0);
281+
});
282+
266283
async function writePkg(payload, options) {
267284
const pkgPath = path.join(options.cwd, 'package.json');
268285
const pkg = JSON.parse(await sander.readFile(pkgPath));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
formatter: './formatters/custom.js'
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function (report) {
2+
return 'ok';
3+
}

@commitlint/load/src/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
5252
.parserOpts).parserOpts;
5353
}
5454

55+
// Resolve config-relative formatter module
56+
if (typeof config.formatter === 'string') {
57+
preset.formatter = resolveFrom.silent(base, config.formatter) || config.formatter;
58+
}
59+
5560
// Execute rule config functions if needed
5661
const executed = await Promise.all(
5762
['rules']

@commitlint/load/src/index.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import {fix, git} from '@commitlint/test';
33
import test from 'ava';
4+
import resolveFrom from 'resolve-from';
45

56
import load from '.';
67

@@ -233,3 +234,25 @@ test('respects formatter option', async t => {
233234
rules: {}
234235
});
235236
});
237+
238+
test('resolves formatter relative from config directory', async t => {
239+
const cwd = await git.bootstrap('fixtures/formatter-local-module');
240+
const actual = await load({}, {cwd});
241+
242+
t.deepEqual(actual, {
243+
formatter: resolveFrom(cwd, './formatters/custom.js'),
244+
extends: [],
245+
rules: {}
246+
});
247+
});
248+
249+
test('returns formatter name when unable to resolve from config directory', async t => {
250+
const cwd = await git.bootstrap('fixtures/formatter-local-module');
251+
const actual = await load({formatter: './doesnt/exists.js'}, {cwd});
252+
253+
t.deepEqual(actual, {
254+
formatter: './doesnt/exists.js',
255+
extends: [],
256+
rules: {}
257+
});
258+
});

0 commit comments

Comments
 (0)