MCPcopy Create free account
hub / github.com/conventional-changelog/commitlint / loadPlugin

Function loadPlugin

@commitlint/load/src/utils/load-plugin.ts:51–186  ·  view source on GitHub ↗
(
	plugins: PluginRecords,
	pluginName: string,
	options: LoadPluginOptions | boolean = {},
)

Source from the content-addressed store, hash-verified

49}
50
51export default async function loadPlugin(
52 plugins: PluginRecords,
53 pluginName: string,
54 options: LoadPluginOptions | boolean = {},
55): Promise<PluginRecords> {
56 const normalized = normalizeOptions(options);
57 const { debug = false, searchPaths = [] } = normalized;
58
59 for (const searchPath of searchPaths) {
60 if (typeof searchPath !== "string" || !path.isAbsolute(searchPath)) {
61 throw new Error(`Invalid searchPath "${searchPath}": must be an absolute path`);
62 }
63 if (!fs.existsSync(searchPath)) {
64 throw new Error(`Invalid searchPath "${searchPath}": directory does not exist`);
65 }
66 if (!fs.statSync(searchPath).isDirectory()) {
67 throw new Error(`Invalid searchPath "${searchPath}": must be a directory, not a file`);
68 }
69 }
70
71 const longName = normalizePackageName(pluginName);
72 const shortName = getShorthandName(longName);
73
74 if (pluginName.match(/\s+/u)) {
75 throw new WhitespacePluginError(pluginName, {
76 pluginName: longName,
77 });
78 }
79
80 const pluginKey = longName === pluginName ? shortName : pluginName;
81
82 if (!plugins[pluginKey]) {
83 let plugin: Plugin | undefined;
84 let resolvedPath: string | undefined;
85
86 // Try to load from npx cache directories using require.resolve
87 const npxResolvedPath = resolveFromNpxCache(longName);
88 if (npxResolvedPath) {
89 try {
90 plugin = await dynamicImport<Plugin>(npxResolvedPath);
91 resolvedPath = npxResolvedPath;
92 } catch (err) {
93 if (debug) {
94 console.debug(
95 `Failed to load plugin ${longName} from npx cache: ${(err as Error).message}`,
96 );
97 }
98 }
99 }
100
101 // Try to load from additional search paths (extended config's node_modules)
102 if (!plugin) {
103 for (const searchPath of searchPaths) {
104 try {
105 resolvedPath = require.resolve(longName, { paths: [searchPath] });
106 plugin = await dynamicImport<Plugin>(resolvedPath);
107 break;
108 } catch (err) {

Callers 2

loadFunction · 0.85

Calls 8

resolveFromNpxCacheFunction · 0.90
normalizeOptionsFunction · 0.85
normalizePackageNameFunction · 0.85
getShorthandNameFunction · 0.85
sanitizeErrorMessageFunction · 0.85
findPackageJsonFunction · 0.85
resolveMethod · 0.80
logMethod · 0.80

Tested by

no test coverage detected