Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: drop resolve-from, resolve-global and import-fresh, resolve global packages correctly #3939

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions @commitlint/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
"@commitlint/read": "^19.0.0",
"@commitlint/types": "^19.0.0",
"execa": "^8.0.1",
"resolve-from": "^5.0.0",
"resolve-global": "^2.0.0",
"yargs": "^17.0.0"
},
"gitHead": "70f7f4688b51774e7ac5e40e896cdaa3f132b2bc"
Expand Down
8 changes: 3 additions & 5 deletions @commitlint/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {fileURLToPath, pathToFileURL} from 'url';
import util from 'util';

import lint from '@commitlint/lint';
import load from '@commitlint/load';
import load, {resolveFromSilent, resolveGlobalSilent} from '@commitlint/load';
import read from '@commitlint/read';
import type {
Formatter,
Expand All @@ -16,8 +16,6 @@ import type {
} from '@commitlint/types';
import type {Options} from 'conventional-commits-parser';
import {execa, type ExecaError} from 'execa';
import resolveFrom from 'resolve-from';
import {resolveGlobalSilent} from 'resolve-global';
import yargs, {type Arguments} from 'yargs';

import {CliFlags} from './types.js';
Expand Down Expand Up @@ -458,8 +456,8 @@ function loadFormatter(
): Promise<Formatter> {
const moduleName = flags.format || config.formatter || '@commitlint/format';
const modulePath =
resolveFrom.silent(__dirname, moduleName) ||
resolveFrom.silent(flags.cwd, moduleName) ||
resolveFromSilent(moduleName, __dirname) ||
resolveFromSilent(moduleName, flags.cwd) ||
resolveGlobalSilent(moduleName);

if (modulePath) {
Expand Down
5 changes: 2 additions & 3 deletions @commitlint/load/src/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {fileURLToPath} from 'url';

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

import load from './load.js';
import load, {resolveFrom} from './load.js';
import {isDynamicAwaitSupported} from './utils/load-config.js';

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

expect(actual).toMatchObject({
formatter: resolveFrom(cwd, './formatters/custom.js'),
formatter: resolveFrom('./formatters/custom.js', cwd),
extends: [],
plugins: {},
rules: {},
Expand Down
4 changes: 4 additions & 0 deletions @commitlint/load/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {validateConfig} from '@commitlint/config-validator';
import executeRule from '@commitlint/execute-rule';
import resolveExtends, {
resolveFrom,
resolveFromSilent,
resolveGlobalSilent,
loadParserPreset,
} from '@commitlint/resolve-extends';
import {
Expand Down Expand Up @@ -135,3 +137,5 @@ export default async function load(
prompt,
};
}

export {resolveFrom, resolveFromSilent, resolveGlobalSilent};
5 changes: 2 additions & 3 deletions @commitlint/resolve-extends/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@
"dependencies": {
"@commitlint/config-validator": "^19.0.0",
"@commitlint/types": "^19.0.0",
"import-fresh": "^3.0.0",
"global-directory": "^4.0.1",
"import-meta-resolve": "^4.0.0",
"lodash.mergewith": "^4.6.2",
"resolve-global": "^2.0.0"
"lodash.mergewith": "^4.6.2"
},
"gitHead": "d829bf6260304ca8d6811f329fcdd1b6c50e9749"
}
83 changes: 51 additions & 32 deletions @commitlint/resolve-extends/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import fs from 'fs';
import path from 'path';
import {pathToFileURL, fileURLToPath} from 'url';

import 'resolve-global';

import globalDirectory from 'global-directory';
import {moduleResolve} from 'import-meta-resolve';
import mergeWith from 'lodash.mergewith';
import {validateConfig} from '@commitlint/config-validator';
import type {ParserPreset, UserConfig} from '@commitlint/types';
import importFresh from 'import-fresh';

const dynamicImport = async <T>(id: string): Promise<T> => {
const imported = await import(
Expand All @@ -16,21 +15,42 @@ const dynamicImport = async <T>(id: string): Promise<T> => {
return ('default' in imported && imported.default) || imported;
};

const pathSuffixes = [
'',
'.js',
'.json',
`${path.sep}index.js`,
`${path.sep}index.json`,
];

const specifierSuffixes = ['', '.js', '.json', '/index.js', '/index.json'];

/**
* @see moduleResolve
*/
export const resolveFrom = (specifier: string, parent?: string): string => {
let resolved: URL;
export const resolveFrom = (lookup: string, parent?: string): string => {
if (path.isAbsolute(lookup)) {
for (const suffix of pathSuffixes) {
const filename = lookup + suffix;
if (fs.existsSync(filename)) {
return filename;
}
}
}

let resolveError: Error | undefined;

for (const suffix of ['', '.js', '.json', '/index.js', '/index.json']) {
try {
resolved = moduleResolve(
specifier + suffix,
pathToFileURL(parent ? parent : import.meta.url)
);
const base = pathToFileURL(
parent
? fs.statSync(parent).isDirectory()
? path.join(parent, 'noop.js')
: parent
: import.meta.url
);

return fileURLToPath(resolved);
for (const suffix of specifierSuffixes) {
try {
return fileURLToPath(moduleResolve(lookup + suffix, base));
} catch (err) {
if (!resolveError) {
resolveError = err as Error;
Expand Down Expand Up @@ -90,11 +110,6 @@ export default async function resolveExtends(
);
}

/**
* Fake file name to provide {@link moduleResolve} a filename to resolve from the configuration cwd
*/
const FAKE_FILE_NAME_FOR_RESOLVER = '__';

async function loadExtends(
config: UserConfig = {},
context: ResolveExtendsContext = {}
Expand All @@ -117,10 +132,7 @@ async function loadExtends(
typeof c === 'object' &&
typeof c.parserPreset === 'string'
) {
const resolvedParserPreset = resolveFrom(
c.parserPreset,
path.join(cwd, FAKE_FILE_NAME_FOR_RESOLVER)
);
const resolvedParserPreset = resolveFrom(c.parserPreset, cwd);

const parserPreset: ParserPreset = {
name: c.parserPreset,
Expand Down Expand Up @@ -207,18 +219,25 @@ function resolveId(
throw Object.assign(err, {code: 'MODULE_NOT_FOUND'});
}

function resolveFromSilent(specifier: string, parent: string): string | void {
export function resolveFromSilent(
specifier: string,
parent: string
): string | void {
try {
return resolveFrom(
specifier,
path.join(parent, FAKE_FILE_NAME_FOR_RESOLVER)
);
} catch (err) {}
return resolveFrom(specifier, parent);
} catch {}
}

function resolveGlobalSilent(specifier: string): string | void {
try {
const resolveGlobal = importFresh<(id: string) => string>('resolve-global');
return resolveGlobal(specifier);
} catch (err) {}
/**
* @see https://github.com/sindresorhus/resolve-global/blob/682a6bb0bd8192b74a6294219bb4c536b3708b65/index.js#L7
*/
export function resolveGlobalSilent(specifier: string): string | void {
for (const globalPackages of [
globalDirectory.npm.packages,
globalDirectory.yarn.packages,
]) {
try {
return resolveFrom(specifier, globalPackages);
} catch {}
}
}
9 changes: 1 addition & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4773,7 +4773,7 @@ ignore@^5.0.4, ignore@^5.2.0, ignore@^5.2.4:
resolved "https://registry.npmjs.org/ignore/-/ignore-5.3.0.tgz"
integrity sha512-g7dmpshy+gD7mh88OC9NwSGTKoc3kyLAZQRU1mt53Aw/vnvfXnbC+F/7F7QoYVKbV+KNvJx8wArewKy1vXMtlg==

import-fresh@^3.0.0, import-fresh@^3.2.1, import-fresh@^3.3.0:
import-fresh@^3.2.1, import-fresh@^3.3.0:
version "3.3.0"
resolved "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.0.tgz"
integrity sha512-veYYhQa+D1QBKznvhUHxb8faxlrwUnxseDAbAp457E0wLNio2bOSKnjYDhMj+YiAq61xrMGhQk9iXVk5FzgQMw==
Expand Down Expand Up @@ -7085,13 +7085,6 @@ resolve-from@^5.0.0:
resolved "https://registry.npmjs.org/resolve-from/-/resolve-from-5.0.0.tgz"
integrity sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==

resolve-global@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/resolve-global/-/resolve-global-2.0.0.tgz#2ff55640800bf3692f089b6008357f75e1a27e54"
integrity sha512-gnAQ0Q/KkupGkuiMyX4L0GaBV8iFwlmoXsMtOz+DFTaKmHhOO/dSlP1RMKhpvHv/dh6K/IQkowGJBqUG0NfBUw==
dependencies:
global-directory "^4.0.1"

resolve-pkg@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/resolve-pkg/-/resolve-pkg-2.0.0.tgz#ac06991418a7623edc119084edc98b0e6bf05a41"
Expand Down