Skip to content

fix(core): fall back to globally installed config if available #127

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

Merged
merged 9 commits into from
Nov 19, 2017
3 changes: 3 additions & 0 deletions @commitlint/core/fixtures/global-install/commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@commitlint/config-angular']
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@commitlint/config-angular']
};
2 changes: 2 additions & 0 deletions @commitlint/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@
"find-up": "^2.1.0",
"lodash": "^4.17.4",
"path-exists": "^3.0.0",
"require-uncached": "^1.0.3",
"resolve-from": "^3.0.0",
"resolve-global": "^0.1.0",
"semver": "^5.3.0"
}
}
30 changes: 27 additions & 3 deletions @commitlint/core/src/library/resolve-extends.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'resolve-global'; // eslint-disable-line import/no-unassigned-import
import path from 'path';
import requireUncached from 'require-uncached';

Choose a reason for hiding this comment

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

This package was renamed to import-fresh

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the heads-up!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverting back to require-uncached due to sindresorhus/import-fresh#6

import resolveFrom from 'resolve-from';
import {merge, omit} from 'lodash';

Expand Down Expand Up @@ -38,12 +40,18 @@ function loadExtends(config = {}, context = {}) {
const ctx = merge({}, context, {cwd});

// Resolve parser preset if none was present before
if (!context.parserPreset && typeof c === 'object' && typeof c.parserPreset === 'string') {
if (
!context.parserPreset &&
typeof c === 'object' &&
typeof c.parserPreset === 'string'
) {
const resolvedParserPreset = resolveFrom(cwd, c.parserPreset);

const parserPreset = {
name: c.parserPreset,
path: `./${path.relative(process.cwd(), resolvedParserPreset)}`.split(path.sep).join('/'),
path: `./${path.relative(process.cwd(), resolvedParserPreset)}`
.split(path.sep)
.join('/'),
opts: require(resolvedParserPreset)
};

Expand Down Expand Up @@ -79,5 +87,21 @@ function resolveConfig(raw, context = {}) {
}

function resolveId(id, context = {}) {
return resolveFrom(context.cwd || process.cwd(), id);
const cwd = context.cwd || process.cwd();
const localPath = resolveFrom.silent(cwd, id);

if (typeof localPath === 'string') {
return localPath;
}

const resolveGlobal = requireUncached('resolve-global');
const globalPath = resolveGlobal.silent(id);

if (typeof globalPath === 'string') {
return globalPath;
}

const err = new Error(`Cannot find module "${id}" from "${cwd}"`);
err.code = 'MODULE_NOT_FOUND';
throw err;
}
47 changes: 47 additions & 0 deletions @commitlint/core/src/library/resolve-extends.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import test from 'ava';
import execa from 'execa';
import {fix} from '@commitlint/test';
import * as sander from '@marionebl/sander';
import resolveExtends from './resolve-extends';

const id = id => id;
Expand All @@ -14,6 +17,50 @@ test('returns an equivalent object as passed in', t => {
t.deepEqual(actual, expected);
});

test.serial('falls back to global install', async t => {
const prev = process.env.PREFIX;

const cwd = await fix.bootstrap('fixtures/global-install');
const prefix = `${cwd}/commitlint-npm-packages`;

const npm = args => execa('npm', args, {cwd});

await sander.mkdir(cwd, 'commitlint-npm-packages');

process.env.PREFIX = prefix;

await npm([
'install',
'--global',
'@commitlint/config-angular',
'--prefix',
prefix
]);

const expected = {extends: ['@commitlint/config-angular']};
t.notThrows(() => resolveExtends(expected));

process.env.PREFIX = prev;
});

test.serial('fails for missing extends', async t => {
const prev = process.env.PREFIX;

const cwd = await fix.bootstrap('fixtures/missing-install');
const prefix = `${cwd}/commitlint-npm-packages`;

process.env.PREFIX = prefix;

const input = {extends: ['@commitlint/foo-bar']};

t.throws(
() => resolveExtends(input, {cwd}),
/Cannot find module "@commitlint\/foo-bar" from/
);

process.env.PREFIX = prev;
});

test('uses empty prefix by default', t => {
const input = {extends: ['extender-name']};

Expand Down
1 change: 1 addition & 0 deletions @packages/test/src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async function setup(cwd) {
try {
await execa('git', ['config', 'user.name', 'ava'], {cwd});
await execa('git', ['config', 'user.email', '[email protected]'], {cwd});
await execa('git', ['config', 'commit.gpgsign', 'false'], {cwd});
} catch (err) {
console.warn(`git config in ${cwd} failed`, err.message);
}
Expand Down
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6077,6 +6077,12 @@ resolve-from@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/resolve-from/-/resolve-from-3.0.0.tgz#b22c7af7d9d6881bc8b6e653335eebcb0a188748"

resolve-global@^0.1.0:
version "0.1.0"
resolved "https://registry.npmjs.org/resolve-global/-/resolve-global-0.1.0.tgz#8fb02cfd5b7db20118e886311f15af95bd15fbd9"
dependencies:
global-dirs "^0.1.0"

resolve-pathname@^2.1.0:
version "2.2.0"
resolved "https://registry.npmjs.org/resolve-pathname/-/resolve-pathname-2.2.0.tgz#7e9ae21ed815fd63ab189adeee64dc831eefa879"
Expand Down