1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = loadPlugin;
|
7 |
|
8 | var _path = require('path');
|
9 |
|
10 | var _path2 = _interopRequireDefault(_path);
|
11 |
|
12 | var _chalk = require('chalk');
|
13 |
|
14 | var _chalk2 = _interopRequireDefault(_chalk);
|
15 |
|
16 | var _pluginNaming = require('./pluginNaming');
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 | function 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 |
|
43 | require.resolve(longName);
|
44 | } catch (missingPluginErr) {
|
45 |
|
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 |
|
57 | throw pluginLoadErr;
|
58 | }
|
59 |
|
60 |
|
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 |
|
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 | }
|
80 | module.exports = exports['default'];
|
81 |
|
\ | No newline at end of file |