Skip to content

Commit d48ac97

Browse files
committed
fix(core): fall back to globally installed config if available #126
1 parent 9dfd315 commit d48ac97

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

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

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'path';
22
import resolveFrom from 'resolve-from';
3+
import resolveGlobal from 'resolve-global';
34
import {merge, omit} from 'lodash';
45

56
// Resolve extend configs
@@ -38,12 +39,18 @@ function loadExtends(config = {}, context = {}) {
3839
const ctx = merge({}, context, {cwd});
3940

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

4449
const parserPreset = {
4550
name: c.parserPreset,
46-
path: `./${path.relative(process.cwd(), resolvedParserPreset)}`.split(path.sep).join('/'),
51+
path: `./${path.relative(process.cwd(), resolvedParserPreset)}`
52+
.split(path.sep)
53+
.join('/'),
4754
opts: require(resolvedParserPreset)
4855
};
4956

@@ -79,5 +86,20 @@ function resolveConfig(raw, context = {}) {
7986
}
8087

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

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('returns an equivalent object as passed in', t => {
1717
t.deepEqual(actual, expected);
1818
});
1919

20-
test.serial.failing('falls back to global install', async t => {
20+
test.serial('falls back to global install', async t => {
2121
const prev = process.env.NPM_PACKAGES;
2222

2323
const cwd = await fix.bootstrap('fixtures/global-install');
@@ -26,7 +26,13 @@ test.serial.failing('falls back to global install', async t => {
2626
const npm = args => execa('npm', args, {cwd});
2727

2828
await sander.mkdir(cwd, 'commitlint-npm-packages');
29-
await npm(['install', '--global', '@commitlint/config-angular', '--prefix', prefix]);
29+
await npm([
30+
'install',
31+
'--global',
32+
'@commitlint/config-angular',
33+
'--prefix',
34+
prefix
35+
]);
3036

3137
process.env.NPM_PACKAGES = prefix;
3238

@@ -41,7 +47,10 @@ test.serial('fails for missing extends', async t => {
4147
const cwd = await fix.bootstrap('fixtures/missing-install');
4248
const input = {extends: ['@commitlint/foo-bar']};
4349

44-
t.throws(() => resolveExtends(input, {cwd}), /Cannot find module "@commitlint\/foo-bar" from/);
50+
t.throws(
51+
() => resolveExtends(input, {cwd}),
52+
/Cannot find module "@commitlint\/foo-bar" from/
53+
);
4554
});
4655

4756
test('uses empty prefix by default', t => {

yarn.lock

+6
Original file line numberDiff line numberDiff line change
@@ -6077,6 +6077,12 @@ resolve-from@^3.0.0:
60776077
version "3.0.0"
60786078
resolved "https://registry.npmjs.org/resolve-from/-/resolve-from-3.0.0.tgz#b22c7af7d9d6881bc8b6e653335eebcb0a188748"
60796079

6080+
resolve-global@^0.1.0:
6081+
version "0.1.0"
6082+
resolved "https://registry.npmjs.org/resolve-global/-/resolve-global-0.1.0.tgz#8fb02cfd5b7db20118e886311f15af95bd15fbd9"
6083+
dependencies:
6084+
global-dirs "^0.1.0"
6085+
60806086
resolve-pathname@^2.1.0:
60816087
version "2.2.0"
60826088
resolved "https://registry.npmjs.org/resolve-pathname/-/resolve-pathname-2.2.0.tgz#7e9ae21ed815fd63ab189adeee64dc831eefa879"

0 commit comments

Comments
 (0)