1 | "use strict";
|
2 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
3 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
4 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
5 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
6 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
7 | };
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | const FS = require("fs");
|
10 | const Path = require("path");
|
11 | const component_1 = require("./component");
|
12 | const options_1 = require("./options");
|
13 | const fs_1 = require("./fs");
|
14 | let PluginHost = class PluginHost extends component_1.AbstractComponent {
|
15 | load() {
|
16 | const logger = this.application.logger;
|
17 | const plugins = this.plugins.length ? this.resolvePluginPaths(this.plugins) : this.discoverNpmPlugins();
|
18 | if (plugins.some(plugin => plugin.toLowerCase() === 'none')) {
|
19 | return true;
|
20 | }
|
21 | for (const plugin of plugins) {
|
22 | try {
|
23 | const instance = require(plugin);
|
24 | const initFunction = typeof instance.load === 'function'
|
25 | ? instance.load
|
26 | : instance;
|
27 | if (typeof initFunction === 'function') {
|
28 | initFunction(this);
|
29 | logger.write('Loaded plugin %s', plugin);
|
30 | }
|
31 | else {
|
32 | logger.error('Invalid structure in plugin %s, no function found.', plugin);
|
33 | }
|
34 | }
|
35 | catch (error) {
|
36 | logger.error('The plugin %s could not be loaded.', plugin);
|
37 | logger.writeln(error.stack);
|
38 | return false;
|
39 | }
|
40 | }
|
41 | return true;
|
42 | }
|
43 | discoverNpmPlugins() {
|
44 | const result = [];
|
45 | const logger = this.application.logger;
|
46 | discover();
|
47 | return result;
|
48 | function discover() {
|
49 | let path = process.cwd(), previous;
|
50 | do {
|
51 | const modules = Path.join(path, 'node_modules');
|
52 | if (FS.existsSync(modules) && FS.statSync(modules).isDirectory()) {
|
53 | discoverModules(modules);
|
54 | }
|
55 | previous = path;
|
56 | path = Path.resolve(Path.join(previous, '..'));
|
57 | } while (previous !== path);
|
58 | }
|
59 | function discoverModules(basePath) {
|
60 | const candidates = [];
|
61 | FS.readdirSync(basePath).forEach((name) => {
|
62 | const dir = Path.join(basePath, name);
|
63 | if (name.startsWith('@') && FS.statSync(dir).isDirectory()) {
|
64 | FS.readdirSync(dir).forEach((n) => {
|
65 | candidates.push(Path.join(name, n));
|
66 | });
|
67 | }
|
68 | candidates.push(name);
|
69 | });
|
70 | candidates.forEach((name) => {
|
71 | const infoFile = Path.join(basePath, name, 'package.json');
|
72 | if (!FS.existsSync(infoFile)) {
|
73 | return;
|
74 | }
|
75 | const info = loadPackageInfo(infoFile);
|
76 | if (isPlugin(info)) {
|
77 | result.push(Path.join(basePath, name));
|
78 | }
|
79 | });
|
80 | }
|
81 | function loadPackageInfo(fileName) {
|
82 | try {
|
83 | return JSON.parse(fs_1.readFile(fileName));
|
84 | }
|
85 | catch (error) {
|
86 | logger.error('Could not parse %s', fileName);
|
87 | return {};
|
88 | }
|
89 | }
|
90 | function isPlugin(info) {
|
91 | const keywords = info.keywords;
|
92 | if (!keywords || !Array.isArray(keywords)) {
|
93 | return false;
|
94 | }
|
95 | for (let i = 0, c = keywords.length; i < c; i++) {
|
96 | const keyword = keywords[i];
|
97 | if (typeof keyword === 'string' && keyword.toLowerCase() === 'typedocplugin') {
|
98 | return true;
|
99 | }
|
100 | }
|
101 | return false;
|
102 | }
|
103 | }
|
104 | resolvePluginPaths(plugins) {
|
105 | const cwd = process.cwd();
|
106 | return plugins.map(plugin => {
|
107 | if (plugin.startsWith('.')) {
|
108 | return Path.resolve(cwd, plugin);
|
109 | }
|
110 | return plugin;
|
111 | });
|
112 | }
|
113 | };
|
114 | __decorate([
|
115 | options_1.BindOption('plugin')
|
116 | ], PluginHost.prototype, "plugins", void 0);
|
117 | PluginHost = __decorate([
|
118 | component_1.Component({ name: 'plugin-host', internal: true })
|
119 | ], PluginHost);
|
120 | exports.PluginHost = PluginHost;
|
121 |
|
\ | No newline at end of file |