Skip to content

Commit 8793c63

Browse files
authored
fix: drop resolve-from, resolve-global and import-fresh, resolve global packages correctly (#3939)
* fix: drop `resolve-from`, `resolve-global` and `import-fresh`, resolve global packages correctly close #3938 * chore: fix Windows compatible issue
1 parent f1ff121 commit 8793c63

File tree

7 files changed

+63
-53
lines changed

7 files changed

+63
-53
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.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

+51-32
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,21 +15,42 @@ const dynamicImport = async <T>(id: string): Promise<T> => {
1615
return ('default' in imported && imported.default) || imported;
1716
};
1817

18+
const pathSuffixes = [
19+
'',
20+
'.js',
21+
'.json',
22+
`${path.sep}index.js`,
23+
`${path.sep}index.json`,
24+
];
25+
26+
const specifierSuffixes = ['', '.js', '.json', '/index.js', '/index.json'];
27+
1928
/**
2029
* @see moduleResolve
2130
*/
22-
export const resolveFrom = (specifier: string, parent?: string): string => {
23-
let resolved: URL;
31+
export const resolveFrom = (lookup: string, parent?: string): string => {
32+
if (path.isAbsolute(lookup)) {
33+
for (const suffix of pathSuffixes) {
34+
const filename = lookup + suffix;
35+
if (fs.existsSync(filename)) {
36+
return filename;
37+
}
38+
}
39+
}
40+
2441
let resolveError: Error | undefined;
2542

26-
for (const suffix of ['', '.js', '.json', '/index.js', '/index.json']) {
27-
try {
28-
resolved = moduleResolve(
29-
specifier + suffix,
30-
pathToFileURL(parent ? parent : import.meta.url)
31-
);
43+
const base = pathToFileURL(
44+
parent
45+
? fs.statSync(parent).isDirectory()
46+
? path.join(parent, 'noop.js')
47+
: parent
48+
: import.meta.url
49+
);
3250

33-
return fileURLToPath(resolved);
51+
for (const suffix of specifierSuffixes) {
52+
try {
53+
return fileURLToPath(moduleResolve(lookup + suffix, base));
3454
} catch (err) {
3555
if (!resolveError) {
3656
resolveError = err as Error;
@@ -90,11 +110,6 @@ export default async function resolveExtends(
90110
);
91111
}
92112

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-
98113
async function loadExtends(
99114
config: UserConfig = {},
100115
context: ResolveExtendsContext = {}
@@ -117,10 +132,7 @@ async function loadExtends(
117132
typeof c === 'object' &&
118133
typeof c.parserPreset === 'string'
119134
) {
120-
const resolvedParserPreset = resolveFrom(
121-
c.parserPreset,
122-
path.join(cwd, FAKE_FILE_NAME_FOR_RESOLVER)
123-
);
135+
const resolvedParserPreset = resolveFrom(c.parserPreset, cwd);
124136

125137
const parserPreset: ParserPreset = {
126138
name: c.parserPreset,
@@ -207,18 +219,25 @@ function resolveId(
207219
throw Object.assign(err, {code: 'MODULE_NOT_FOUND'});
208220
}
209221

210-
function resolveFromSilent(specifier: string, parent: string): string | void {
222+
export function resolveFromSilent(
223+
specifier: string,
224+
parent: string
225+
): string | void {
211226
try {
212-
return resolveFrom(
213-
specifier,
214-
path.join(parent, FAKE_FILE_NAME_FOR_RESOLVER)
215-
);
216-
} catch (err) {}
227+
return resolveFrom(specifier, parent);
228+
} catch {}
217229
}
218230

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) {}
231+
/**
232+
* @see https://github.com/sindresorhus/resolve-global/blob/682a6bb0bd8192b74a6294219bb4c536b3708b65/index.js#L7
233+
*/
234+
export function resolveGlobalSilent(specifier: string): string | void {
235+
for (const globalPackages of [
236+
globalDirectory.npm.packages,
237+
globalDirectory.yarn.packages,
238+
]) {
239+
try {
240+
return resolveFrom(specifier, globalPackages);
241+
} catch {}
242+
}
224243
}

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)