Skip to content

Commit 8612eb3

Browse files
authored
fix(core): fall back to globally installed config if available (#127)
* 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
1 parent 49d88e4 commit 8612eb3

File tree

7 files changed

+286
-146
lines changed

7 files changed

+286
-146
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-angular']
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-angular']
3+
};

@commitlint/core/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@
9292
"find-up": "^2.1.0",
9393
"lodash": "^4.17.4",
9494
"path-exists": "^3.0.0",
95+
"require-uncached": "^1.0.3",
9596
"resolve-from": "^3.0.0",
97+
"resolve-global": "^0.1.0",
9698
"semver": "^5.3.0"
9799
}
98100
}

@commitlint/core/src/library/resolve-extends.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import 'resolve-global'; // eslint-disable-line import/no-unassigned-import
12
import path from 'path';
3+
import requireUncached from 'require-uncached';
24
import resolveFrom from 'resolve-from';
35
import {merge, omit} from 'lodash';
46

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

4042
// Resolve parser preset if none was present before
41-
if (!context.parserPreset && typeof c === 'object' && typeof c.parserPreset === 'string') {
43+
if (
44+
!context.parserPreset &&
45+
typeof c === 'object' &&
46+
typeof c.parserPreset === 'string'
47+
) {
4248
const resolvedParserPreset = resolveFrom(cwd, c.parserPreset);
4349

4450
const parserPreset = {
4551
name: c.parserPreset,
46-
path: `./${path.relative(process.cwd(), resolvedParserPreset)}`.split(path.sep).join('/'),
52+
path: `./${path.relative(process.cwd(), resolvedParserPreset)}`
53+
.split(path.sep)
54+
.join('/'),
4755
opts: require(resolvedParserPreset)
4856
};
4957

@@ -79,5 +87,21 @@ function resolveConfig(raw, context = {}) {
7987
}
8088

8189
function resolveId(id, context = {}) {
82-
return resolveFrom(context.cwd || process.cwd(), id);
90+
const cwd = context.cwd || process.cwd();
91+
const localPath = resolveFrom.silent(cwd, id);
92+
93+
if (typeof localPath === 'string') {
94+
return localPath;
95+
}
96+
97+
const resolveGlobal = requireUncached('resolve-global');
98+
const globalPath = resolveGlobal.silent(id);
99+
100+
if (typeof globalPath === 'string') {
101+
return globalPath;
102+
}
103+
104+
const err = new Error(`Cannot find module "${id}" from "${cwd}"`);
105+
err.code = 'MODULE_NOT_FOUND';
106+
throw err;
83107
}

@commitlint/core/src/library/resolve-extends.test.js

+47
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import test from 'ava';
2+
import execa from 'execa';
3+
import {fix} from '@commitlint/test';
4+
import * as sander from '@marionebl/sander';
25
import resolveExtends from './resolve-extends';
36

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

20+
test.serial('falls back to global install', async t => {
21+
const prev = process.env.PREFIX;
22+
23+
const cwd = await fix.bootstrap('fixtures/global-install');
24+
const prefix = `${cwd}/commitlint-npm-packages`;
25+
26+
const npm = args => execa('npm', args, {cwd});
27+
28+
await sander.mkdir(cwd, 'commitlint-npm-packages');
29+
30+
process.env.PREFIX = prefix;
31+
32+
await npm([
33+
'install',
34+
'--global',
35+
'@commitlint/config-angular',
36+
'--prefix',
37+
prefix
38+
]);
39+
40+
const expected = {extends: ['@commitlint/config-angular']};
41+
t.notThrows(() => resolveExtends(expected));
42+
43+
process.env.PREFIX = prev;
44+
});
45+
46+
test.serial('fails for missing extends', async t => {
47+
const prev = process.env.PREFIX;
48+
49+
const cwd = await fix.bootstrap('fixtures/missing-install');
50+
const prefix = `${cwd}/commitlint-npm-packages`;
51+
52+
process.env.PREFIX = prefix;
53+
54+
const input = {extends: ['@commitlint/foo-bar']};
55+
56+
t.throws(
57+
() => resolveExtends(input, {cwd}),
58+
/Cannot find module "@commitlint\/foo-bar" from/
59+
);
60+
61+
process.env.PREFIX = prev;
62+
});
63+
1764
test('uses empty prefix by default', t => {
1865
const input = {extends: ['extender-name']};
1966

@packages/test/src/git.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ async function setup(cwd) {
3636
try {
3737
await execa('git', ['config', 'user.name', 'ava'], {cwd});
3838
await execa('git', ['config', 'user.email', '[email protected]'], {cwd});
39+
await execa('git', ['config', 'commit.gpgsign', 'false'], {cwd});
3940
} catch (err) {
4041
console.warn(`git config in ${cwd} failed`, err.message);
4142
}

0 commit comments

Comments
 (0)