Repository navigation
Expand file tree
/
Copy pathplugins.js
More file actions
166 lines (149 loc) · 5.33 KB
/
Copy pathplugins.js
File metadata and controls
166 lines (149 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const attachments = require("./data/attachments");
const logs = require("./data/logs");
const { beforeNewThread } = require("./hooks/beforeNewThread");
const { beforeNewMessageReceived } = require("./hooks/beforeNewMessageReceived");
const { afterNewMessageReceived } = require("./hooks/afterNewMessageReceived");
const { afterThreadClose } = require("./hooks/afterThreadClose");
const { afterThreadCloseScheduled } = require("./hooks/afterThreadCloseScheduled");
const { afterThreadCloseScheduleCanceled } = require("./hooks/afterThreadCloseScheduleCanceled");
const formats = require("./formatters");
const webserver = require("./modules/webserver");
const childProcess = require("child_process");
const pacote = require("pacote");
const path = require("path");
const threads = require("./data/threads");
const displayRoles = require("./data/displayRoles");
const { PluginInstallationError } = require("./PluginInstallationError");
const config = require("./cfg");
const pluginSources = {
npm: {
install(plugins) {
return new Promise((resolve, reject) => {
console.log(`Installing ${plugins.length} plugins from NPM...`);
let finalPluginNames = plugins;
if (! config.useGitForGitHubPlugins) {
// Rewrite GitHub npm package names to full GitHub tarball links to avoid
// needing to have Git installed to install these plugins.
// $1 package author, $2 package name, $3 branch (optional)
const npmGitHubPattern = /^([a-z0-9_.-]+)\/([a-z0-9_.-]+)(?:#([a-z0-9_.-]+))?$/i;
finalPluginNames = plugins.map(pluginName => {
const gitHubPackageParts = pluginName.match(npmGitHubPattern);
if (! gitHubPackageParts) {
return pluginName;
}
return `https://api.github.com/repos/${gitHubPackageParts[1]}/${gitHubPackageParts[2]}/tarball${gitHubPackageParts[3] ? "/" + gitHubPackageParts[3] : ""}`;
});
}
let stderr = "";
const npmProcessName = /^win/.test(process.platform) ? "npm.cmd" : "npm";
const npmProcess = childProcess.spawn(
npmProcessName,
["install", "--verbose", "--no-save", ...finalPluginNames],
{ cwd: process.cwd() }
);
npmProcess.stderr.on("data", data => { stderr += String(data) });
npmProcess.on("close", code => {
if (code !== 0) {
return reject(new PluginInstallationError(stderr));
}
return resolve();
});
});
},
async load(plugin, pluginApi) {
const manifest = await pacote.manifest(plugin);
const packageName = manifest.name;
const pluginFn = require(packageName);
if (typeof pluginFn !== "function") {
throw new PluginInstallationError(`Plugin '${plugin}' is not a valid plugin`);
}
return pluginFn(pluginApi);
},
},
file: {
install(plugins) {},
load(plugin, pluginApi) {
const requirePath = path.join(__dirname, "..", plugin);
const pluginFn = require(requirePath);
if (typeof pluginFn !== "function") {
throw new PluginInstallationError(`Plugin '${plugin}' is not a valid plugin`);
}
return pluginFn(pluginApi);
},
}
};
const defaultPluginSource = "file";
function splitPluginSource(pluginName) {
for (const pluginSource of Object.keys(pluginSources)) {
if (pluginName.startsWith(`${pluginSource}:`)) {
return {
source: pluginSource,
plugin: pluginName.slice(pluginSource.length + 1),
};
}
}
return {
source: defaultPluginSource,
plugin: pluginName,
};
}
module.exports = {
async installPlugins(plugins) {
const pluginsBySource = {};
for (const pluginName of plugins) {
const { source, plugin } = splitPluginSource(pluginName);
pluginsBySource[source] = pluginsBySource[source] || [];
pluginsBySource[source].push(plugin);
}
for (const [source, sourcePlugins] of Object.entries(pluginsBySource)) {
await pluginSources[source].install(sourcePlugins);
}
},
async loadPlugins(plugins, pluginApi) {
for (const pluginName of plugins) {
const { source, plugin } = splitPluginSource(pluginName);
await pluginSources[source].load(plugin, pluginApi);
}
},
/**
* @returns {PluginAPI}
*/
getPluginAPI({ bot, knex, commands }) {
return {
bot,
knex,
config,
commands: {
manager: commands.manager,
addGlobalCommand: commands.addGlobalCommand,
addInboxServerCommand: commands.addInboxServerCommand,
addInboxThreadCommand: commands.addInboxThreadCommand,
addAlias: commands.addAlias
},
attachments: {
addStorageType: attachments.addStorageType,
downloadAttachment: attachments.downloadAttachment,
saveAttachment: attachments.saveAttachment,
},
logs: {
addStorageType: logs.addStorageType,
saveLogToStorage: logs.saveLogToStorage,
getLogUrl: logs.getLogUrl,
getLogFile: logs.getLogFile,
getLogCustomResponse: logs.getLogCustomResponse,
},
hooks: {
beforeNewThread,
beforeNewMessageReceived,
afterNewMessageReceived,
afterThreadClose,
afterThreadCloseScheduled,
afterThreadCloseScheduleCanceled,
},
formats,
webserver,
threads,
displayRoles,
};
},
};