123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const path_1 = __importDefault(require("path"));
- const chalk_1 = __importDefault(require("chalk"));
- const plugin_naming_1 = require("./plugin-naming");
- const plugin_errors_1 = require("./plugin-errors");
- function loadPlugin(plugins, pluginName, debug = false) {
- const longName = plugin_naming_1.normalizePackageName(pluginName);
- const shortName = plugin_naming_1.getShorthandName(longName);
- let plugin = null;
- if (pluginName.match(/\s+/u)) {
- throw new plugin_errors_1.WhitespacePluginError(pluginName, {
- pluginName: longName,
- });
- }
- 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 (error) {
- // If the plugin can't be resolved, display the missing plugin error (usually a config or install error)
- console.error(chalk_1.default.red(`Failed to load plugin ${longName}.`));
- throw new plugin_errors_1.MissingPluginError(pluginName, error.message, {
- pluginName: longName,
- commitlintPath: path_1.default.resolve(__dirname, '../..'),
- });
- }
- // 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_1.default.blue(`Loaded plugin ${pluginName} (${loadedPluginAndVersion}) (from ${resolvedPath})`));
- }
- plugins[pluginKey] = plugin;
- }
- return plugins;
- }
- exports.default = loadPlugin;
- //# sourceMappingURL=load-plugin.js.map
|