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

fix(cli): improve format module resolving #464

Merged
merged 3 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
20 changes: 14 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 @@ -260,17 +262,23 @@ function selectParserOpts(parserPreset) {
return parserPreset.parserOpts;
}

function resolveModulePath(name, cwd) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could avoid the catch block via

function resolveModulePath(name, cwd) {
    return resolveFrom.silent(__dirname, name) || resolveFrom.silent(cwd, name) || resolveGlobal.silent(name);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are those "Doh!" moments for me 😅 I've no clue why I didn't think about this. I will change it now! Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I've removed the extra resolveModulePath method. I used it for the try-catch contraption. Right now it's both easy to read, and the order is readable (at least, that's my opinion). 😄

try {
return require.resolve(name);
} catch (error) {
return resolveFrom.silent(cwd, name) || resolveGlobal.silent(name);
}
}

function loadFormatter(config, flags) {
const moduleName = flags.format || config.formatter;
let modulePath;
const modulePath = moduleName && resolveModulePath(moduleName, flags.cwd);

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: {}
});
});