2020-11-13 13:38:31 -05:00
|
|
|
const fs = require('fs').promises;
|
2020-11-13 13:41:08 -05:00
|
|
|
const hooks = require('./hooks');
|
2012-05-28 18:39:32 -07:00
|
|
|
var npm = require("npm/lib/npm.js");
|
|
|
|
var readInstalled = require("./read-installed.js");
|
|
|
|
var path = require("path");
|
|
|
|
var tsort = require("./tsort");
|
|
|
|
var util = require("util");
|
|
|
|
var _ = require("underscore");
|
2018-11-12 23:20:39 +01:00
|
|
|
var settings = require('../../../node/utils/Settings');
|
2012-05-28 18:39:32 -07:00
|
|
|
|
|
|
|
var pluginUtils = require('./shared');
|
2020-09-06 15:27:18 -04:00
|
|
|
var defs = require('./plugin_defs');
|
2012-03-01 18:45:02 +01:00
|
|
|
|
|
|
|
exports.prefix = 'ep_';
|
|
|
|
|
|
|
|
exports.formatPlugins = function () {
|
2020-09-06 15:27:18 -04:00
|
|
|
return _.keys(defs.plugins).join(", ");
|
2012-04-04 09:51:46 +02:00
|
|
|
};
|
2012-03-01 18:45:02 +01:00
|
|
|
|
2015-04-28 23:41:55 +01:00
|
|
|
exports.formatPluginsWithVersion = function () {
|
|
|
|
var plugins = [];
|
2020-09-06 15:27:18 -04:00
|
|
|
_.forEach(defs.plugins, function(plugin) {
|
2015-04-28 23:41:55 +01:00
|
|
|
if(plugin.package.name !== "ep_etherpad-lite"){
|
|
|
|
var pluginStr = plugin.package.name + "@" + plugin.package.version;
|
|
|
|
plugins.push(pluginStr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return plugins.join(", ");
|
|
|
|
};
|
|
|
|
|
2012-03-01 18:45:02 +01:00
|
|
|
exports.formatParts = function () {
|
2020-09-06 15:27:18 -04:00
|
|
|
return _.map(defs.parts, function(part) { return part.full_name; }).join('\n');
|
2012-04-04 09:51:46 +02:00
|
|
|
};
|
2012-03-01 18:45:02 +01:00
|
|
|
|
2012-06-04 14:33:38 +02:00
|
|
|
exports.formatHooks = function (hook_set_name) {
|
2012-03-01 18:45:02 +01:00
|
|
|
var res = [];
|
2020-09-06 15:27:18 -04:00
|
|
|
var hooks = pluginUtils.extractHooks(defs.parts, hook_set_name || 'hooks');
|
2012-06-04 14:33:38 +02:00
|
|
|
|
|
|
|
_.chain(hooks).keys().forEach(function (hook_name) {
|
|
|
|
_.forEach(hooks[hook_name], function (hook) {
|
|
|
|
res.push("<dt>" + hook.hook_name + "</dt><dd>" + hook.hook_fn_name + " from " + hook.part.full_name + "</dd>");
|
2012-03-01 18:45:02 +01:00
|
|
|
});
|
|
|
|
});
|
2012-06-04 14:33:38 +02:00
|
|
|
return "<dl>" + res.join("\n") + "</dl>";
|
2012-04-04 09:51:46 +02:00
|
|
|
};
|
2012-03-01 18:45:02 +01:00
|
|
|
|
2020-11-13 13:48:38 -05:00
|
|
|
const callInit = () => {
|
2020-09-06 15:27:18 -04:00
|
|
|
let p = Object.keys(defs.plugins).map(function(plugin_name) {
|
|
|
|
let plugin = defs.plugins[plugin_name];
|
2019-01-18 13:52:37 +00:00
|
|
|
let ep_init = path.normalize(path.join(plugin.package.path, ".ep_initialized"));
|
2020-11-13 13:38:31 -05:00
|
|
|
return fs.stat(ep_init).catch(async function() {
|
|
|
|
await fs.writeFile(ep_init, 'done');
|
2019-01-18 13:52:37 +00:00
|
|
|
await hooks.aCallAll("init_" + plugin_name, {});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(p);
|
2012-04-05 01:48:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 17:00:26 -04:00
|
|
|
exports.pathNormalization = function (part, hook_fn_name, hook_name) {
|
|
|
|
const parts = hook_fn_name.split(':');
|
|
|
|
const functionName = (parts.length > 1) ? parts.pop() : hook_name;
|
2020-09-06 15:27:18 -04:00
|
|
|
const packageDir = path.dirname(defs.plugins[part.plugin].package.path);
|
2020-09-05 17:00:26 -04:00
|
|
|
const fileName = path.normalize(path.join(packageDir, parts.join(':')));
|
|
|
|
return fileName + ((functionName == null) ? '' : (':' + functionName));
|
2012-05-28 18:33:03 -07:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:52:37 +00:00
|
|
|
exports.update = async function () {
|
|
|
|
let packages = await exports.getPackages();
|
2020-09-06 13:16:40 -04:00
|
|
|
var parts = {}; // Key is full name. sortParts converts this into a topologically sorted array.
|
2019-01-18 13:52:37 +00:00
|
|
|
var plugins = {};
|
|
|
|
|
|
|
|
// Load plugin metadata ep.json
|
|
|
|
let p = Object.keys(packages).map(function (plugin_name) {
|
|
|
|
return loadPlugin(packages, plugin_name, plugins, parts);
|
2012-03-01 18:45:02 +01:00
|
|
|
});
|
|
|
|
|
2019-01-18 13:52:37 +00:00
|
|
|
return Promise.all(p).then(function() {
|
2020-09-06 15:27:18 -04:00
|
|
|
defs.plugins = plugins;
|
|
|
|
defs.parts = sortParts(parts);
|
|
|
|
defs.hooks = pluginUtils.extractHooks(defs.parts, 'hooks', exports.pathNormalization);
|
|
|
|
defs.loaded = true;
|
2020-11-13 13:48:38 -05:00
|
|
|
}).then(callInit);
|
2019-01-18 13:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.getPackages = async function () {
|
2012-03-01 18:45:02 +01:00
|
|
|
// Load list of installed NPM packages, flatten it to a list, and filter out only packages with names that
|
2018-11-12 23:20:39 +01:00
|
|
|
var dir = settings.root;
|
2019-01-18 13:52:37 +00:00
|
|
|
let data = await util.promisify(readInstalled)(dir);
|
|
|
|
|
|
|
|
var packages = {};
|
|
|
|
function flatten(deps) {
|
|
|
|
_.chain(deps).keys().each(function (name) {
|
|
|
|
if (name.indexOf(exports.prefix) === 0) {
|
|
|
|
packages[name] = _.clone(deps[name]);
|
|
|
|
// Delete anything that creates loops so that the plugin
|
|
|
|
// list can be sent as JSON to the web client
|
|
|
|
delete packages[name].dependencies;
|
|
|
|
delete packages[name].parent;
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-18 13:52:37 +00:00
|
|
|
// I don't think we need recursion
|
|
|
|
//if (deps[name].dependencies !== undefined) flatten(deps[name].dependencies);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmp = {};
|
|
|
|
tmp[data.name] = data;
|
|
|
|
flatten(tmp[data.name].dependencies);
|
|
|
|
return packages;
|
2012-05-28 17:46:14 -07:00
|
|
|
};
|
2012-03-01 18:45:02 +01:00
|
|
|
|
2019-01-18 13:52:37 +00:00
|
|
|
async function loadPlugin(packages, plugin_name, plugins, parts) {
|
2012-03-01 18:45:02 +01:00
|
|
|
var plugin_path = path.resolve(packages[plugin_name].path, "ep.json");
|
2019-01-18 13:52:37 +00:00
|
|
|
try {
|
2020-11-13 13:38:31 -05:00
|
|
|
let data = await fs.readFile(plugin_path);
|
2019-01-18 13:52:37 +00:00
|
|
|
try {
|
|
|
|
var plugin = JSON.parse(data);
|
|
|
|
plugin['package'] = packages[plugin_name];
|
|
|
|
plugins[plugin_name] = plugin;
|
|
|
|
_.each(plugin.parts, function (part) {
|
|
|
|
part.plugin = plugin_name;
|
|
|
|
part.full_name = plugin_name + "/" + part.name;
|
|
|
|
parts[part.full_name] = part;
|
|
|
|
});
|
|
|
|
} catch (ex) {
|
|
|
|
console.error("Unable to parse plugin definition file " + plugin_path + ": " + ex.toString());
|
2012-03-01 18:45:02 +01:00
|
|
|
}
|
2019-01-18 13:52:37 +00:00
|
|
|
} catch (er) {
|
|
|
|
console.error("Unable to load plugin definition file " + plugin_path);
|
|
|
|
}
|
2012-05-28 18:11:59 -07:00
|
|
|
}
|
2012-03-01 18:45:02 +01:00
|
|
|
|
2012-05-28 18:11:59 -07:00
|
|
|
function partsToParentChildList(parts) {
|
2012-03-01 18:45:02 +01:00
|
|
|
var res = [];
|
2012-03-17 13:36:42 +01:00
|
|
|
_.chain(parts).keys().forEach(function (name) {
|
|
|
|
_.each(parts[name].post || [], function (child_name) {
|
2012-03-01 18:45:02 +01:00
|
|
|
res.push([name, child_name]);
|
|
|
|
});
|
2012-03-17 13:36:42 +01:00
|
|
|
_.each(parts[name].pre || [], function (parent_name) {
|
2012-03-01 18:45:02 +01:00
|
|
|
res.push([parent_name, name]);
|
|
|
|
});
|
|
|
|
if (!parts[name].pre && !parts[name].post) {
|
|
|
|
res.push([name, ":" + name]); // Include apps with no dependency info
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return res;
|
2012-05-28 18:11:59 -07:00
|
|
|
}
|
2012-03-17 13:36:42 +01:00
|
|
|
|
|
|
|
// Used only in Node, so no need for _
|
2012-05-28 18:11:59 -07:00
|
|
|
function sortParts(parts) {
|
2012-03-01 18:45:02 +01:00
|
|
|
return tsort(
|
2012-05-28 18:11:59 -07:00
|
|
|
partsToParentChildList(parts)
|
2012-03-01 18:45:02 +01:00
|
|
|
).filter(
|
|
|
|
function (name) { return parts[name] !== undefined; }
|
|
|
|
).map(
|
|
|
|
function (name) { return parts[name]; }
|
|
|
|
);
|
2012-05-28 18:11:59 -07:00
|
|
|
}
|