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 declaration_1 = require("./options/declaration");
|
13 | let PluginHost = class PluginHost extends component_1.AbstractComponent {
|
14 | load() {
|
15 | const logger = this.application.logger;
|
16 | const plugins = this.plugins || this.discoverNpmPlugins();
|
17 | let i, c = plugins.length;
|
18 | for (i = 0; i < c; i++) {
|
19 | const plugin = plugins[i];
|
20 | if (typeof plugin !== 'string') {
|
21 | logger.error('Unknown plugin %s', plugin);
|
22 | return false;
|
23 | }
|
24 | else if (plugin.toLowerCase() === 'none') {
|
25 | return true;
|
26 | }
|
27 | }
|
28 | for (i = 0; i < c; i++) {
|
29 | const plugin = plugins[i];
|
30 | try {
|
31 | const instance = require(plugin);
|
32 | const initFunction = typeof instance.load === 'function'
|
33 | ? instance.load
|
34 | : instance;
|
35 | if (typeof initFunction === 'function') {
|
36 | instance(this);
|
37 | logger.write('Loaded plugin %s', plugin);
|
38 | }
|
39 | else {
|
40 | logger.error('Invalid structure in plugin %s, no function found.', plugin);
|
41 | }
|
42 | }
|
43 | catch (error) {
|
44 | logger.error('The plugin %s could not be loaded.', plugin);
|
45 | logger.writeln(error.stack);
|
46 | return false;
|
47 | }
|
48 | }
|
49 | return true;
|
50 | }
|
51 | discoverNpmPlugins() {
|
52 | const result = [];
|
53 | const logger = this.application.logger;
|
54 | discover();
|
55 | return result;
|
56 | function discover() {
|
57 | let path = process.cwd(), previous;
|
58 | do {
|
59 | const modules = Path.join(path, 'node_modules');
|
60 | if (FS.existsSync(modules) && FS.statSync(modules).isDirectory()) {
|
61 | discoverModules(modules);
|
62 | }
|
63 | previous = path;
|
64 | path = Path.resolve(Path.join(previous, '..'));
|
65 | } while (previous !== path);
|
66 | }
|
67 | function discoverModules(basePath) {
|
68 | const candidates = [];
|
69 | FS.readdirSync(basePath).forEach((name) => {
|
70 | const dir = Path.join(basePath, name);
|
71 | if (name.startsWith('@')) {
|
72 | FS.readdirSync(dir).forEach((n) => {
|
73 | candidates.push(Path.join(name, n));
|
74 | });
|
75 | }
|
76 | candidates.push(name);
|
77 | });
|
78 | candidates.forEach((name) => {
|
79 | const infoFile = Path.join(basePath, name, 'package.json');
|
80 | if (!FS.existsSync(infoFile)) {
|
81 | return;
|
82 | }
|
83 | const info = loadPackageInfo(infoFile);
|
84 | if (isPlugin(info)) {
|
85 | result.push(Path.join(basePath, name));
|
86 | }
|
87 | });
|
88 | }
|
89 | function loadPackageInfo(fileName) {
|
90 | try {
|
91 | return JSON.parse(FS.readFileSync(fileName, { encoding: 'utf-8' }));
|
92 | }
|
93 | catch (error) {
|
94 | logger.error('Could not parse %s', fileName);
|
95 | return {};
|
96 | }
|
97 | }
|
98 | function isPlugin(info) {
|
99 | const keywords = info.keywords;
|
100 | if (!keywords || !Array.isArray(keywords)) {
|
101 | return false;
|
102 | }
|
103 | for (let i = 0, c = keywords.length; i < c; i++) {
|
104 | const keyword = keywords[i];
|
105 | if (typeof keyword === 'string' && keyword.toLowerCase() === 'typedocplugin') {
|
106 | return true;
|
107 | }
|
108 | }
|
109 | return false;
|
110 | }
|
111 | }
|
112 | };
|
113 | __decorate([
|
114 | component_1.Option({
|
115 | name: 'plugin',
|
116 | help: 'Specify the npm plugins that should be loaded. Omit to load all installed plugins, set to \'none\' to load no plugins.',
|
117 | type: declaration_1.ParameterType.Array
|
118 | })
|
119 | ], PluginHost.prototype, "plugins", void 0);
|
120 | PluginHost = __decorate([
|
121 | component_1.Component({ name: 'plugin-host', internal: true })
|
122 | ], PluginHost);
|
123 | exports.PluginHost = PluginHost;
|
124 |
|
\ | No newline at end of file |