Skip to content

Commit 9d69216

Browse files
committed
fix: drop resolve-from, resolve-global and import-fresh, resolve global packages correctly
close #3938
1 parent f1ff121 commit 9d69216

File tree

9 files changed

+49
-51
lines changed

9 files changed

+49
-51
lines changed

@commitlint/cli/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454
"@commitlint/read": "^19.0.0",
5555
"@commitlint/types": "^19.0.0",
5656
"execa": "^8.0.1",
57-
"resolve-from": "^5.0.0",
58-
"resolve-global": "^2.0.0",
5957
"yargs": "^17.0.0"
6058
},
6159
"gitHead": "70f7f4688b51774e7ac5e40e896cdaa3f132b2bc"

@commitlint/cli/src/cli.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ test('strict: should exit with 2 on warning', async () => {
505505
test('strict: should exit with 0 on success', async () => {
506506
const cwd = await gitBootstrap('fixtures/warning');
507507
const actual = await cli(['--strict'], {cwd})('feat: abc');
508+
console.log('stderr:----------', actual.stderr);
508509
expect(actual.exitCode).toBe(0);
509510
});
510511

@commitlint/cli/src/cli.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {fileURLToPath, pathToFileURL} from 'url';
44
import util from 'util';
55

66
import lint from '@commitlint/lint';
7-
import load from '@commitlint/load';
7+
import load, {resolveFromSilent, resolveGlobalSilent} from '@commitlint/load';
88
import read from '@commitlint/read';
99
import type {
1010
Formatter,
@@ -16,8 +16,6 @@ import type {
1616
} from '@commitlint/types';
1717
import type {Options} from 'conventional-commits-parser';
1818
import {execa, type ExecaError} from 'execa';
19-
import resolveFrom from 'resolve-from';
20-
import {resolveGlobalSilent} from 'resolve-global';
2119
import yargs, {type Arguments} from 'yargs';
2220

2321
import {CliFlags} from './types.js';
@@ -458,8 +456,8 @@ function loadFormatter(
458456
): Promise<Formatter> {
459457
const moduleName = flags.format || config.formatter || '@commitlint/format';
460458
const modulePath =
461-
resolveFrom.silent(__dirname, moduleName) ||
462-
resolveFrom.silent(flags.cwd, moduleName) ||
459+
resolveFromSilent(moduleName, __dirname) ||
460+
resolveFromSilent(moduleName, flags.cwd) ||
463461
resolveGlobalSilent(moduleName);
464462

465463
if (modulePath) {

@commitlint/load/src/load.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import {fileURLToPath} from 'url';
55

66
import {RuleConfigSeverity} from '@commitlint/types';
77
import {fix, git, npm} from '@commitlint/test';
8-
import resolveFrom from 'resolve-from';
98

10-
import load from './load.js';
9+
import load, {resolveFrom} from './load.js';
1110
import {isDynamicAwaitSupported} from './utils/load-config.js';
1211

1312
const __dirname = path.resolve(fileURLToPath(import.meta.url), '..');
@@ -459,7 +458,7 @@ test('resolves formatter relative from config directory', async () => {
459458
const actual = await load({}, {cwd});
460459

461460
expect(actual).toMatchObject({
462-
formatter: resolveFrom(cwd, './formatters/custom.js'),
461+
formatter: resolveFrom('./formatters/custom.js', cwd),
463462
extends: [],
464463
plugins: {},
465464
rules: {},

@commitlint/load/src/load.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {validateConfig} from '@commitlint/config-validator';
44
import executeRule from '@commitlint/execute-rule';
55
import resolveExtends, {
66
resolveFrom,
7+
resolveFromSilent,
8+
resolveGlobalSilent,
79
loadParserPreset,
810
} from '@commitlint/resolve-extends';
911
import {
@@ -135,3 +137,5 @@ export default async function load(
135137
prompt,
136138
};
137139
}
140+
141+
export {resolveFrom, resolveFromSilent, resolveGlobalSilent};

@commitlint/resolve-extends/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@
4242
"dependencies": {
4343
"@commitlint/config-validator": "^19.0.0",
4444
"@commitlint/types": "^19.0.0",
45-
"import-fresh": "^3.0.0",
45+
"global-directory": "^4.0.1",
4646
"import-meta-resolve": "^4.0.0",
47-
"lodash.mergewith": "^4.6.2",
48-
"resolve-global": "^2.0.0"
47+
"lodash.mergewith": "^4.6.2"
4948
},
5049
"gitHead": "d829bf6260304ca8d6811f329fcdd1b6c50e9749"
5150
}

@commitlint/resolve-extends/src/index.ts

+35-28
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
import fs from 'fs';
12
import path from 'path';
23
import {pathToFileURL, fileURLToPath} from 'url';
34

4-
import 'resolve-global';
5-
5+
import globalDirectory from 'global-directory';
66
import {moduleResolve} from 'import-meta-resolve';
77
import mergeWith from 'lodash.mergewith';
88
import {validateConfig} from '@commitlint/config-validator';
99
import type {ParserPreset, UserConfig} from '@commitlint/types';
10-
import importFresh from 'import-fresh';
1110

1211
const dynamicImport = async <T>(id: string): Promise<T> => {
1312
const imported = await import(
@@ -16,20 +15,29 @@ const dynamicImport = async <T>(id: string): Promise<T> => {
1615
return ('default' in imported && imported.default) || imported;
1716
};
1817

18+
/**
19+
* Fake file name to provide {@link moduleResolve} a filename to resolve from the configuration cwd
20+
*/
21+
const FAKE_FILE_NAME_FOR_RESOLVER = '__';
22+
1923
/**
2024
* @see moduleResolve
2125
*/
2226
export const resolveFrom = (specifier: string, parent?: string): string => {
2327
let resolved: URL;
2428
let resolveError: Error | undefined;
2529

30+
const base = pathToFileURL(
31+
parent
32+
? fs.statSync(parent).isDirectory()
33+
? path.join(parent, FAKE_FILE_NAME_FOR_RESOLVER)
34+
: parent
35+
: import.meta.url
36+
);
37+
2638
for (const suffix of ['', '.js', '.json', '/index.js', '/index.json']) {
2739
try {
28-
resolved = moduleResolve(
29-
specifier + suffix,
30-
pathToFileURL(parent ? parent : import.meta.url)
31-
);
32-
40+
resolved = moduleResolve(specifier + suffix, base);
3341
return fileURLToPath(resolved);
3442
} catch (err) {
3543
if (!resolveError) {
@@ -90,11 +98,6 @@ export default async function resolveExtends(
9098
);
9199
}
92100

93-
/**
94-
* Fake file name to provide {@link moduleResolve} a filename to resolve from the configuration cwd
95-
*/
96-
const FAKE_FILE_NAME_FOR_RESOLVER = '__';
97-
98101
async function loadExtends(
99102
config: UserConfig = {},
100103
context: ResolveExtendsContext = {}
@@ -117,10 +120,7 @@ async function loadExtends(
117120
typeof c === 'object' &&
118121
typeof c.parserPreset === 'string'
119122
) {
120-
const resolvedParserPreset = resolveFrom(
121-
c.parserPreset,
122-
path.join(cwd, FAKE_FILE_NAME_FOR_RESOLVER)
123-
);
123+
const resolvedParserPreset = resolveFrom(c.parserPreset, cwd);
124124

125125
const parserPreset: ParserPreset = {
126126
name: c.parserPreset,
@@ -207,18 +207,25 @@ function resolveId(
207207
throw Object.assign(err, {code: 'MODULE_NOT_FOUND'});
208208
}
209209

210-
function resolveFromSilent(specifier: string, parent: string): string | void {
210+
export function resolveFromSilent(
211+
specifier: string,
212+
parent: string
213+
): string | void {
211214
try {
212-
return resolveFrom(
213-
specifier,
214-
path.join(parent, FAKE_FILE_NAME_FOR_RESOLVER)
215-
);
216-
} catch (err) {}
215+
return resolveFrom(specifier, parent);
216+
} catch {}
217217
}
218218

219-
function resolveGlobalSilent(specifier: string): string | void {
220-
try {
221-
const resolveGlobal = importFresh<(id: string) => string>('resolve-global');
222-
return resolveGlobal(specifier);
223-
} catch (err) {}
219+
/**
220+
* @see also https://github.com/sindresorhus/resolve-global/blob/682a6bb0bd8192b74a6294219bb4c536b3708b65/index.js#L7
221+
*/
222+
export function resolveGlobalSilent(specifier: string): string | void {
223+
for (const globalPackages of [
224+
globalDirectory.npm.packages,
225+
globalDirectory.yarn.packages,
226+
]) {
227+
try {
228+
return resolveFrom(specifier, globalPackages);
229+
} catch {}
230+
}
224231
}

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
},
2828
"commitlint": {
2929
"extends": [
30-
"@commitlint/config-conventional",
31-
"@commitlint/config-lerna-scopes"
30+
"@commitlint/config-conventional"
3231
]
3332
},
3433
"prettier": {

yarn.lock

+1-8
Original file line numberDiff line numberDiff line change
@@ -4773,7 +4773,7 @@ ignore@^5.0.4, ignore@^5.2.0, ignore@^5.2.4:
47734773
resolved "https://registry.npmjs.org/ignore/-/ignore-5.3.0.tgz"
47744774
integrity sha512-g7dmpshy+gD7mh88OC9NwSGTKoc3kyLAZQRU1mt53Aw/vnvfXnbC+F/7F7QoYVKbV+KNvJx8wArewKy1vXMtlg==
47754775

4776-
import-fresh@^3.0.0, import-fresh@^3.2.1, import-fresh@^3.3.0:
4776+
import-fresh@^3.2.1, import-fresh@^3.3.0:
47774777
version "3.3.0"
47784778
resolved "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.0.tgz"
47794779
integrity sha512-veYYhQa+D1QBKznvhUHxb8faxlrwUnxseDAbAp457E0wLNio2bOSKnjYDhMj+YiAq61xrMGhQk9iXVk5FzgQMw==
@@ -7085,13 +7085,6 @@ resolve-from@^5.0.0:
70857085
resolved "https://registry.npmjs.org/resolve-from/-/resolve-from-5.0.0.tgz"
70867086
integrity sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==
70877087

7088-
resolve-global@^2.0.0:
7089-
version "2.0.0"
7090-
resolved "https://registry.npmjs.org/resolve-global/-/resolve-global-2.0.0.tgz#2ff55640800bf3692f089b6008357f75e1a27e54"
7091-
integrity sha512-gnAQ0Q/KkupGkuiMyX4L0GaBV8iFwlmoXsMtOz+DFTaKmHhOO/dSlP1RMKhpvHv/dh6K/IQkowGJBqUG0NfBUw==
7092-
dependencies:
7093-
global-directory "^4.0.1"
7094-
70957088
resolve-pkg@^2.0.0:
70967089
version "2.0.0"
70977090
resolved "https://registry.npmjs.org/resolve-pkg/-/resolve-pkg-2.0.0.tgz#ac06991418a7623edc119084edc98b0e6bf05a41"

0 commit comments

Comments
 (0)