Skip to content

Commit

Permalink
fix: drop resolve-from, resolve-global and import-fresh
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Feb 27, 2024
1 parent f1ff121 commit 86acbd9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 51 deletions.
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
1 change: 1 addition & 0 deletions @commitlint/cli/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ test('strict: should exit with 2 on warning', async () => {
test('strict: should exit with 0 on success', async () => {
const cwd = await gitBootstrap('fixtures/warning');
const actual = await cli(['--strict'], {cwd})('feat: abc');
console.log('stderr:----------', actual.stderr);
expect(actual.exitCode).toBe(0);
});

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"
}
63 changes: 35 additions & 28 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,20 +15,29 @@ const dynamicImport = async <T>(id: string): Promise<T> => {
return ('default' in imported && imported.default) || imported;
};

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

/**
* @see moduleResolve
*/
export const resolveFrom = (specifier: string, parent?: string): string => {
let resolved: URL;
let resolveError: Error | undefined;

const base = pathToFileURL(
parent
? fs.statSync(parent).isDirectory()
? path.join(parent, FAKE_FILE_NAME_FOR_RESOLVER)
: parent
: import.meta.url
);

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

resolved = moduleResolve(specifier + suffix, base);
return fileURLToPath(resolved);
} catch (err) {
if (!resolveError) {
Expand Down Expand Up @@ -90,11 +98,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 +120,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 +207,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 also 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 {}
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
},
"commitlint": {
"extends": [
"@commitlint/config-conventional",
"@commitlint/config-lerna-scopes"
"@commitlint/config-conventional"
]
},
"prettier": {
Expand Down
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

0 comments on commit 86acbd9

Please sign in to comment.