UNPKG

2.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.PluginsLoader = void 0;
4const path_1 = require("path");
5const util_1 = require("util");
6const ui_1 = require("../ui");
7const PLUGIN_ENTRY_FILENAME = 'plugin';
8class PluginsLoader {
9 load(plugins = []) {
10 const pluginNames = plugins.map((entry) => (0, util_1.isObject)(entry) ? entry.name : entry);
11 const nodeModulePaths = [
12 (0, path_1.join)(process.cwd(), 'node_modules'),
13 ...module.paths,
14 ];
15 const pluginRefs = pluginNames.map((item) => {
16 try {
17 try {
18 const binaryPath = require.resolve((0, path_1.join)(item, PLUGIN_ENTRY_FILENAME), {
19 paths: nodeModulePaths,
20 });
21 return require(binaryPath);
22 }
23 catch (_a) { }
24 const binaryPath = require.resolve(item, { paths: nodeModulePaths });
25 return require(binaryPath);
26 }
27 catch (e) {
28 throw new Error(`"${item}" plugin could not be found!`);
29 }
30 });
31 const beforeHooks = [];
32 const afterHooks = [];
33 const afterDeclarationsHooks = [];
34 pluginRefs.forEach((plugin, index) => {
35 if (!plugin.before && !plugin.after && !plugin.afterDeclarations) {
36 throw new Error(ui_1.CLI_ERRORS.WRONG_PLUGIN(pluginNames[index]));
37 }
38 const options = (0, util_1.isObject)(plugins[index])
39 ? plugins[index].options || {}
40 : {};
41 plugin.before &&
42 beforeHooks.push(plugin.before.bind(plugin.before, options));
43 plugin.after && afterHooks.push(plugin.after.bind(plugin.after, options));
44 plugin.afterDeclarations &&
45 afterDeclarationsHooks.push(plugin.afterDeclarations.bind(plugin.afterDeclarations, options));
46 });
47 return {
48 beforeHooks,
49 afterHooks,
50 afterDeclarationsHooks,
51 };
52 }
53}
54exports.PluginsLoader = PluginsLoader;