{"size": 2049, "ext": "js", "max_stars_repo_path": "@commitlint/load/src/utils/loadPlugin.js", "max_stars_repo_name": "zbanalagay/commitlint", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "@commitlint/load/src/utils/loadPlugin.js", "max_issues_repo_name": "zbanalagay/commitlint", "max_issues_repo_issues_event_min_datetime": "2021-06-28T20:30:21.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-20T22:08:52.000Z", "max_forks_repo_path": "@commitlint/load/src/utils/loadPlugin.js", "max_forks_repo_name": "zbanalagay/commitlint", "max_forks_repo_forks_event_min_datetime": "2022-03-15T21:43:40.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-15T21:43:40.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 27.32, "max_line_length": 108, "alphanum_fraction": 0.6827720839}
import path from 'path';
import chalk from 'chalk';
import {normalizePackageName, getShorthandName} from './pluginNaming';

export default function loadPlugin(plugins, pluginName, debug = false) {
	const longName = normalizePackageName(pluginName);
	const shortName = getShorthandName(longName);
	let plugin = null;

	if (pluginName.match(/\s+/u)) {
		const whitespaceError = new Error(
			`Whitespace found in plugin name '${pluginName}'`
		);

		whitespaceError.messageTemplate = 'whitespace-found';
		whitespaceError.messageData = {
			pluginName: longName
		};
		throw whitespaceError;
	}

	const pluginKey = longName === pluginName ? shortName : pluginName;

	if (!plugins[pluginKey]) {
		try {
			plugin = require(longName);
		} catch (pluginLoadErr) {
			try {
				// Check whether the plugin exists
				require.resolve(longName);
			} catch (missingPluginErr) {
				// If the plugin can't be resolved, display the missing plugin error (usually a config or install error)
				console.error(chalk.red(`Failed to load plugin ${longName}.`));
				missingPluginErr.message = `Failed to load plugin ${pluginName}: ${
					missingPluginErr.message
				}`;
				missingPluginErr.messageTemplate = 'plugin-missing';
				missingPluginErr.messageData = {
					pluginName: longName,
					commitlintPath: path.resolve(__dirname, '../..')
				};
				throw missingPluginErr;
			}

			// Otherwise, the plugin exists and is throwing on module load for some reason, so print the stack trace.
			throw pluginLoadErr;
		}

		// This step is costly, so skip if debug is disabled
		if (debug) {
			const resolvedPath = require.resolve(longName);

			let version = null;

			try {
				version = require(`${longName}/package.json`).version;
			} catch (e) {
				// Do nothing
			}

			const loadedPluginAndVersion = version
				? `${longName}@${version}`
				: `${longName}, version unknown`;

			console.log(
				chalk.blue(
					`Loaded plugin ${pluginName} (${loadedPluginAndVersion}) (from ${resolvedPath})`
				)
			);
		}

		plugins[pluginKey] = plugin;
	}
}
