Skip to content

Commit

Permalink
fix: improve format module resolving (#464)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
byCedric authored and marionebl committed Oct 11, 2018
1 parent 7524fc4 commit baed8b1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (report) {
return 'custom-formatter-ok';
}
4 changes: 3 additions & 1 deletion @commitlint/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
"get-stdin": "5.0.1",
"lodash.merge": "4.6.1",
"lodash.pick": "4.4.0",
"meow": "5.0.0"
"meow": "5.0.0",
"resolve-from": "^4.0.0",
"resolve-global": "^0.1.0"
}
}
15 changes: 9 additions & 6 deletions @commitlint/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const meow = require('meow');
const merge = require('lodash.merge');
const pick = require('lodash.pick');
const stdin = require('get-stdin');
const resolveFrom = require('resolve-from');
const resolveGlobal = require('resolve-global');

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

function loadFormatter(config, flags) {
const moduleName = flags.format || config.formatter;
let modulePath;
const modulePath =
resolveFrom.silent(__dirname, moduleName) ||
resolveFrom.silent(flags.cwd, moduleName) ||
resolveGlobal.silent(moduleName);

try {
modulePath = require.resolve(`${moduleName}`);
} catch (error) {
throw new Error(`Using format ${moduleName}, but cannot find the module.`);
if (modulePath) {
return require(modulePath);
}

return require(modulePath);
throw new Error(`Using format ${moduleName}, but cannot find the module.`);
}

// Catch unhandled rejections globally
Expand Down
17 changes: 17 additions & 0 deletions @commitlint/cli/src/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,23 @@ test('should fail for invalid formatters from flags', async t => {
t.is(actual.code, 1);
});

test('should work with absolute formatter path', async t => {
const formatterPath = path.resolve(__dirname, '../fixtures/custom-formatter/formatters/custom.js');
const cwd = await git.bootstrap('fixtures/custom-formatter');
const actual = await cli(['--format', formatterPath], {cwd})('test: this should work');

t.true(actual.stdout.includes('custom-formatter-ok'));
t.is(actual.code, 0);
});

test('should work with relative formatter path', async t => {
const cwd = path.resolve(await git.bootstrap('fixtures/custom-formatter'), './formatters');
const actual = await cli(['--format', './custom.js'], {cwd})('test: this should work');

t.true(actual.stdout.includes('custom-formatter-ok'));
t.is(actual.code, 0);
});

async function writePkg(payload, options) {
const pkgPath = path.join(options.cwd, 'package.json');
const pkg = JSON.parse(await sander.readFile(pkgPath));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
formatter: './formatters/custom.js'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (report) {
return 'ok';
}
5 changes: 5 additions & 0 deletions @commitlint/load/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
.parserOpts).parserOpts;
}

// Resolve config-relative formatter module
if (typeof config.formatter === 'string') {
preset.formatter = resolveFrom.silent(base, config.formatter) || config.formatter;
}

// Execute rule config functions if needed
const executed = await Promise.all(
['rules']
Expand Down
23 changes: 23 additions & 0 deletions @commitlint/load/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import {fix, git} from '@commitlint/test';
import test from 'ava';
import resolveFrom from 'resolve-from';

import load from '.';

Expand Down Expand Up @@ -233,3 +234,25 @@ test('respects formatter option', async t => {
rules: {}
});
});

test('resolves formatter relative from config directory', async t => {
const cwd = await git.bootstrap('fixtures/formatter-local-module');
const actual = await load({}, {cwd});

t.deepEqual(actual, {
formatter: resolveFrom(cwd, './formatters/custom.js'),
extends: [],
rules: {}
});
});

test('returns formatter name when unable to resolve from config directory', async t => {
const cwd = await git.bootstrap('fixtures/formatter-local-module');
const actual = await load({formatter: './doesnt/exists.js'}, {cwd});

t.deepEqual(actual, {
formatter: './doesnt/exists.js',
extends: [],
rules: {}
});
});

0 comments on commit baed8b1

Please sign in to comment.