import path from 'path' ; import chalk from 'chalk' ; import { normalizePackageName , getShorthandName } from './plugin-naming' ; import { WhitespacePluginError , MissingPluginError } from './plugin-errors' ; import { PluginRecords } from '@commitlint/types' ; export default function loadPlugin ( plugins , pluginName , debug = false ) { const longName = normalizePackageName ( pluginName ) ; const shortName = getShorthandName ( longName ) ; let plugin = null ; if ( pluginName . match ( /\s+/u ) ) { throw new WhitespacePluginError ( pluginName , { pluginName : longName , } ) ; } const pluginKey = longName === pluginName ? shortName : pluginName ; if ( ! plugins [ pluginKey ] ) { try { plugin = require ( longName ) ; } catch ( pluginLoadErr ) { try { require . resolve ( longName ) ; } catch ( error ) { console . error ( chalk . red ( `Failed to load plugin ${ longName }.` ) ) ; const message = error ?. message || 'Unknown error occurred' ; throw new MissingPluginError ( pluginName , message , { pluginName : longName , commitlintPath : path . resolve ( __dirname , '../..' ) , } ) ; } throw pluginLoadErr ; } if ( debug ) { const resolvedPath = require . resolve ( longName ) ; let version = null ; try { version = require ( `${ longName }/package.json` ) . version ; } catch ( e ) { } const loadedPluginAndVersion = version ? `${ longName }@${ version }` : `${ longName }, version unknown` ; console . log ( chalk . blue ( `Loaded plugin ${ pluginName } (${ loadedPluginAndVersion }) (from ${ resolvedPath })` ) ) ; } plugins [ pluginKey ] = plugin ; } return plugins ; }