Skip to content

Commit

Permalink
fix(core): fall back to globally installed config if available (#127)
Browse files Browse the repository at this point in the history
* chore: avoid gpg password prompt

* test(core): add failing test case for global extends resolving

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

* test: use cwd correctly for git config

* test: improve testability of global fallback

* fix(core): ensure resolve-globals is detected as dependency

* fix: use more recent import-fresh

* fix: fall back to require-uncached

Avoid sindresorhus/import-fresh#6 for the time being

* fix: pull in patch releases
  • Loading branch information
marionebl authored Nov 19, 2017
1 parent 49d88e4 commit 8612eb3
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 146 deletions.
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';
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', 'test@example.com'], {cwd});
await execa('git', ['config', 'commit.gpgsign', 'false'], {cwd});
} catch (err) {
console.warn(`git config in ${cwd} failed`, err.message);
}
Expand Down
Loading

0 comments on commit 8612eb3

Please sign in to comment.