UNPKG

2.42 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = loadPlugin;
7
8var _path = require('path');
9
10var _path2 = _interopRequireDefault(_path);
11
12var _chalk = require('chalk');
13
14var _chalk2 = _interopRequireDefault(_chalk);
15
16var _pluginNaming = require('./pluginNaming');
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20function loadPlugin(plugins, pluginName, debug = false) {
21 const longName = (0, _pluginNaming.normalizePackageName)(pluginName);
22 const shortName = (0, _pluginNaming.getShorthandName)(longName);
23 let plugin = null;
24
25 if (pluginName.match(/\s+/u)) {
26 const whitespaceError = new Error(`Whitespace found in plugin name '${pluginName}'`);
27
28 whitespaceError.messageTemplate = 'whitespace-found';
29 whitespaceError.messageData = {
30 pluginName: longName
31 };
32 throw whitespaceError;
33 }
34
35 const pluginKey = longName === pluginName ? shortName : pluginName;
36
37 if (!plugins[pluginKey]) {
38 try {
39 plugin = require(longName);
40 } catch (pluginLoadErr) {
41 try {
42 // Check whether the plugin exists
43 require.resolve(longName);
44 } catch (missingPluginErr) {
45 // If the plugin can't be resolved, display the missing plugin error (usually a config or install error)
46 console.error(_chalk2.default.red(`Failed to load plugin ${longName}.`));
47 missingPluginErr.message = `Failed to load plugin ${pluginName}: ${missingPluginErr.message}`;
48 missingPluginErr.messageTemplate = 'plugin-missing';
49 missingPluginErr.messageData = {
50 pluginName: longName,
51 commitlintPath: _path2.default.resolve(__dirname, '../..')
52 };
53 throw missingPluginErr;
54 }
55
56 // Otherwise, the plugin exists and is throwing on module load for some reason, so print the stack trace.
57 throw pluginLoadErr;
58 }
59
60 // This step is costly, so skip if debug is disabled
61 if (debug) {
62 const resolvedPath = require.resolve(longName);
63
64 let version = null;
65
66 try {
67 version = require(`${longName}/package.json`).version;
68 } catch (e) {
69 // Do nothing
70 }
71
72 const loadedPluginAndVersion = version ? `${longName}@${version}` : `${longName}, version unknown`;
73
74 console.log(_chalk2.default.blue(`Loaded plugin ${pluginName} (${loadedPluginAndVersion}) (from ${resolvedPath})`));
75 }
76
77 plugins[pluginKey] = plugin;
78 }
79}
80module.exports = exports['default'];
81//# sourceMappingURL=loadPlugin.js.map
\No newline at end of file