@commitlint/resolve-extends
Advanced tools
Comparing version 18.6.1 to 19.0.0
import 'resolve-global'; | ||
import { UserConfig } from '@commitlint/types'; | ||
import type { ParserPreset, UserConfig } from '@commitlint/types'; | ||
/** | ||
* @see moduleResolve | ||
*/ | ||
export declare const resolveFrom: (specifier: string, parent?: string) => string; | ||
/** | ||
* | ||
* @param resolvedParserPreset path resolved by {@link resolveFrom} | ||
* @returns path and parserOpts function retrieved from `resolvedParserPreset` | ||
*/ | ||
export declare const loadParserPreset: (resolvedParserPreset: string) => Promise<Pick<ParserPreset, 'path' | 'parserOpts'>>; | ||
export interface ResolveExtendsContext { | ||
cwd?: string; | ||
parserPreset?: unknown; | ||
parserPreset?: string | ParserPreset; | ||
prefix?: string; | ||
@@ -12,5 +22,5 @@ resolve?(id: string, ctx?: { | ||
resolveGlobal?: (id: string) => string; | ||
require?<T>(id: string): T; | ||
dynamicImport?<T>(id: string): T | Promise<T>; | ||
} | ||
export default function resolveExtends(config?: UserConfig, context?: ResolveExtendsContext): UserConfig; | ||
export default function resolveExtends(config?: UserConfig, context?: ResolveExtendsContext): Promise<UserConfig>; | ||
//# sourceMappingURL=index.d.ts.map |
128
lib/index.js
@@ -1,2 +0,1 @@ | ||
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
@@ -13,19 +12,52 @@ var t = {}; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
import path from 'path'; | ||
import { pathToFileURL, fileURLToPath } from 'url'; | ||
import 'resolve-global'; | ||
import { moduleResolve } from 'import-meta-resolve'; | ||
import mergeWith from 'lodash.mergewith'; | ||
import { validateConfig } from '@commitlint/config-validator'; | ||
import importFresh from 'import-fresh'; | ||
const dynamicImport = async (id) => { | ||
const imported = await import(path.isAbsolute(id) ? pathToFileURL(id).toString() : id); | ||
return ('default' in imported && imported.default) || imported; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const path_1 = __importDefault(require("path")); | ||
require("resolve-global"); | ||
const resolve_from_1 = __importDefault(require("resolve-from")); | ||
const lodash_mergewith_1 = __importDefault(require("lodash.mergewith")); | ||
const config_validator_1 = require("@commitlint/config-validator"); | ||
const importFresh = require('import-fresh'); | ||
function resolveExtends(config = {}, context = {}) { | ||
/** | ||
* @see moduleResolve | ||
*/ | ||
export const resolveFrom = (specifier, parent) => { | ||
let resolved; | ||
let resolveError; | ||
for (const suffix of ['', '.js', '.json', '/index.js', '/index.json']) { | ||
try { | ||
resolved = moduleResolve(specifier + suffix, pathToFileURL(parent ? parent : import.meta.url)); | ||
return fileURLToPath(resolved); | ||
} | ||
catch (err) { | ||
if (!resolveError) { | ||
resolveError = err; | ||
} | ||
} | ||
} | ||
throw resolveError; | ||
}; | ||
/** | ||
* | ||
* @param resolvedParserPreset path resolved by {@link resolveFrom} | ||
* @returns path and parserOpts function retrieved from `resolvedParserPreset` | ||
*/ | ||
export const loadParserPreset = async (resolvedParserPreset) => { | ||
const finalParserOpts = await dynamicImport(resolvedParserPreset); | ||
const relativeParserPath = path.relative(process.cwd(), resolvedParserPreset); | ||
return { | ||
path: `./${relativeParserPath}`.split(path.sep).join('/'), | ||
parserOpts: finalParserOpts, | ||
}; | ||
}; | ||
export default async function resolveExtends(config = {}, context = {}) { | ||
const { extends: e } = config; | ||
const extended = loadExtends(config, context); | ||
const extended = await loadExtends(config, context); | ||
extended.push(config); | ||
return extended.reduce((r, _a) => { | ||
var { extends: _ } = _a, c = __rest(_a, ["extends"]); | ||
return (0, lodash_mergewith_1.default)(r, c, (objValue, srcValue, key) => { | ||
return mergeWith(r, c, (objValue, srcValue, key) => { | ||
if (key === 'plugins') { | ||
@@ -42,11 +74,13 @@ if (Array.isArray(objValue)) { | ||
} | ||
exports.default = resolveExtends; | ||
function loadExtends(config = {}, context = {}) { | ||
/** | ||
* 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 = {}, context = {}) { | ||
const { extends: e } = config; | ||
const ext = e ? (Array.isArray(e) ? e : [e]) : []; | ||
return ext.reduce((configs, raw) => { | ||
const load = context.require || require; | ||
return await ext.reduce(async (configs, raw) => { | ||
const resolved = resolveConfig(raw, context); | ||
const c = load(resolved); | ||
const cwd = path_1.default.dirname(resolved); | ||
const c = await (context.dynamicImport || dynamicImport)(resolved); | ||
const cwd = path.dirname(resolved); | ||
const ctx = Object.assign(Object.assign({}, context), { cwd }); | ||
@@ -57,16 +91,10 @@ // Resolve parser preset if none was present before | ||
typeof c.parserPreset === 'string') { | ||
const resolvedParserPreset = (0, resolve_from_1.default)(cwd, c.parserPreset); | ||
const parserPreset = { | ||
name: c.parserPreset, | ||
path: `./${path_1.default.relative(process.cwd(), resolvedParserPreset)}` | ||
.split(path_1.default.sep) | ||
.join('/'), | ||
parserOpts: require(resolvedParserPreset), | ||
}; | ||
const resolvedParserPreset = resolveFrom(c.parserPreset, path.join(cwd, FAKE_FILE_NAME_FOR_RESOLVER)); | ||
const parserPreset = Object.assign({ name: c.parserPreset }, (await loadParserPreset(resolvedParserPreset))); | ||
ctx.parserPreset = parserPreset; | ||
config.parserPreset = parserPreset; | ||
} | ||
(0, config_validator_1.validateConfig)(resolved, config); | ||
return [...configs, ...loadExtends(c, ctx), c]; | ||
}, []); | ||
validateConfig(resolved, config); | ||
return [...(await configs), ...(await loadExtends(c, ctx)), c]; | ||
}, Promise.resolve([])); | ||
} | ||
@@ -77,3 +105,3 @@ function getId(raw = '', prefix = '') { | ||
const relative = first === '.'; | ||
const absolute = path_1.default.isAbsolute(raw); | ||
const absolute = path.isAbsolute(raw); | ||
if (scoped) { | ||
@@ -85,17 +113,28 @@ return raw.includes('/') ? raw : [raw, prefix].filter(String).join('/'); | ||
function resolveConfig(raw, context = {}) { | ||
const resolve = context.resolve || resolveId; | ||
const resolve = context.resolve || tryResolveId; | ||
const id = getId(raw, context.prefix); | ||
let resolved; | ||
try { | ||
return resolve(id, context); | ||
resolved = resolve(id, context); | ||
} | ||
catch (err) { | ||
const legacy = getId(raw, 'conventional-changelog-lint-config'); | ||
const resolved = resolve(legacy, context); | ||
resolved = resolve(legacy, context); | ||
console.warn(`Resolving ${raw} to legacy config ${legacy}. To silence this warning raise an issue at 'npm repo ${legacy}' to rename to ${id}.`); | ||
return resolved; | ||
} | ||
return resolved; | ||
} | ||
function resolveId(id, context = {}) { | ||
function tryResolveId(id, context) { | ||
const cwd = context.cwd || process.cwd(); | ||
const localPath = resolveFromSilent(cwd, id); | ||
for (const suffix of ['', '.js', '.json', '/index.js', '/index.json']) { | ||
try { | ||
return fileURLToPath(moduleResolve(id + suffix, pathToFileURL(path.join(cwd, id)))); | ||
} | ||
catch (_a) { } | ||
} | ||
return resolveId(id, context); | ||
} | ||
function resolveId(specifier, context = {}) { | ||
const cwd = context.cwd || process.cwd(); | ||
const localPath = resolveFromSilent(specifier, cwd); | ||
if (typeof localPath === 'string') { | ||
@@ -105,20 +144,19 @@ return localPath; | ||
const resolveGlobal = context.resolveGlobal || resolveGlobalSilent; | ||
const globalPath = resolveGlobal(id); | ||
const globalPath = resolveGlobal(specifier); | ||
if (typeof globalPath === 'string') { | ||
return globalPath; | ||
} | ||
const err = new Error(`Cannot find module "${id}" from "${cwd}"`); | ||
err.code = 'MODULE_NOT_FOUND'; | ||
throw err; | ||
const err = new Error(`Cannot find module "${specifier}" from "${cwd}"`); | ||
throw Object.assign(err, { code: 'MODULE_NOT_FOUND' }); | ||
} | ||
function resolveFromSilent(cwd, id) { | ||
function resolveFromSilent(specifier, parent) { | ||
try { | ||
return (0, resolve_from_1.default)(cwd, id); | ||
return resolveFrom(specifier, path.join(parent, FAKE_FILE_NAME_FOR_RESOLVER)); | ||
} | ||
catch (err) { } | ||
} | ||
function resolveGlobalSilent(id) { | ||
function resolveGlobalSilent(specifier) { | ||
try { | ||
const resolveGlobal = importFresh('resolve-global'); | ||
return resolveGlobal(id); | ||
return resolveGlobal(specifier); | ||
} | ||
@@ -125,0 +163,0 @@ catch (err) { } |
{ | ||
"name": "@commitlint/resolve-extends", | ||
"version": "18.6.1", | ||
"type": "module", | ||
"version": "19.0.0", | ||
"description": "Lint your commit messages", | ||
@@ -38,14 +39,14 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@commitlint/utils": "^18.6.1", | ||
"@commitlint/utils": "^19.0.0", | ||
"@types/lodash.mergewith": "^4.6.8" | ||
}, | ||
"dependencies": { | ||
"@commitlint/config-validator": "^18.6.1", | ||
"@commitlint/types": "^18.6.1", | ||
"@commitlint/config-validator": "^19.0.0", | ||
"@commitlint/types": "^19.0.0", | ||
"import-fresh": "^3.0.0", | ||
"import-meta-resolve": "^4.0.0", | ||
"lodash.mergewith": "^4.6.2", | ||
"resolve-from": "^5.0.0", | ||
"resolve-global": "^1.0.0" | ||
"resolve-global": "^2.0.0" | ||
}, | ||
"gitHead": "89f5bf91d1c11b1f457a6f0d99b8ea34583a9311" | ||
"gitHead": "f1ff12159d627ee63bf8982ab02e6cca8f10b09f" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
16348
182
0
Yes
+ Addedimport-meta-resolve@^4.0.0
+ Added@commitlint/config-validator@19.5.0(transitive)
+ Added@commitlint/types@19.5.0(transitive)
+ Added@types/conventional-commits-parser@5.0.0(transitive)
+ Added@types/node@22.9.0(transitive)
+ Addedchalk@5.3.0(transitive)
+ Addedglobal-directory@4.0.1(transitive)
+ Addedimport-meta-resolve@4.1.0(transitive)
+ Addedini@4.1.1(transitive)
+ Addedresolve-global@2.0.0(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removedresolve-from@^5.0.0
- Removed@commitlint/config-validator@18.6.1(transitive)
- Removed@commitlint/types@18.6.1(transitive)
- Removedansi-styles@4.3.0(transitive)
- Removedchalk@4.1.2(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedglobal-dirs@0.1.1(transitive)
- Removedhas-flag@4.0.0(transitive)
- Removedini@1.3.8(transitive)
- Removedresolve-from@5.0.0(transitive)
- Removedresolve-global@1.0.0(transitive)
- Removedsupports-color@7.2.0(transitive)
Updated@commitlint/types@^19.0.0
Updatedresolve-global@^2.0.0