mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-22 08:26:16 -04:00
Merge branch 'fix/client_plugins' into ace2_refactoring
This commit is contained in:
commit
6c647baa02
4 changed files with 63 additions and 40 deletions
|
@ -6,6 +6,7 @@ var settings = require("../../utils/Settings");
|
||||||
var Yajsml = require('yajsml');
|
var Yajsml = require('yajsml');
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var ERR = require("async-stacktrace");
|
var ERR = require("async-stacktrace");
|
||||||
|
var _ = require("underscore");
|
||||||
|
|
||||||
exports.expressCreateServer = function (hook_name, args, cb) {
|
exports.expressCreateServer = function (hook_name, args, cb) {
|
||||||
// Cache both minified and static.
|
// Cache both minified and static.
|
||||||
|
@ -35,8 +36,22 @@ exports.expressCreateServer = function (hook_name, args, cb) {
|
||||||
// serve plugin definitions
|
// serve plugin definitions
|
||||||
// not very static, but served here so that client can do require("pluginfw/static/js/plugin-definitions.js");
|
// not very static, but served here so that client can do require("pluginfw/static/js/plugin-definitions.js");
|
||||||
args.app.get('/pluginfw/plugin-definitions.json', function (req, res, next) {
|
args.app.get('/pluginfw/plugin-definitions.json', function (req, res, next) {
|
||||||
|
|
||||||
|
var clientParts = _(plugins.parts)
|
||||||
|
.filter(function(part){ return _(part).has('client_hooks') });
|
||||||
|
|
||||||
|
var clientPlugins = {};
|
||||||
|
|
||||||
|
_(clientParts).chain()
|
||||||
|
.map(function(part){ return part.plugin })
|
||||||
|
.uniq()
|
||||||
|
.each(function(name){
|
||||||
|
clientPlugins[name] = _(plugins.plugins[name]).clone();
|
||||||
|
delete clientPlugins[name]['package'];
|
||||||
|
});
|
||||||
|
|
||||||
res.header("Content-Type","application/json; charset=utf-8");
|
res.header("Content-Type","application/json; charset=utf-8");
|
||||||
res.write(JSON.stringify({"plugins": plugins.plugins, "parts": plugins.parts}));
|
res.write(JSON.stringify({"plugins": clientPlugins, "parts": clientParts}));
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,7 +131,10 @@ exports.minify = function(req, res, next)
|
||||||
res.end();
|
res.end();
|
||||||
} else if (req.method == 'GET') {
|
} else if (req.method == 'GET') {
|
||||||
getFileCompressed(filename, contentType, function (error, content) {
|
getFileCompressed(filename, contentType, function (error, content) {
|
||||||
if(ERR(error)) return;
|
if(ERR(error, function(){
|
||||||
|
res.writeHead(500, {});
|
||||||
|
res.end();
|
||||||
|
})) return;
|
||||||
res.header("Content-Type", contentType);
|
res.header("Content-Type", contentType);
|
||||||
res.writeHead(200, {});
|
res.writeHead(200, {});
|
||||||
res.write(content);
|
res.write(content);
|
||||||
|
|
|
@ -23,9 +23,8 @@
|
||||||
"log4js" : "0.4.1",
|
"log4js" : "0.4.1",
|
||||||
"jsdom-nocontextifiy" : "0.2.10",
|
"jsdom-nocontextifiy" : "0.2.10",
|
||||||
"async-stacktrace" : "0.0.2",
|
"async-stacktrace" : "0.0.2",
|
||||||
"npm" : "1.1",
|
"npm" : "1.1",
|
||||||
"ejs" : "0.6.1",
|
"ejs" : "0.6.1",
|
||||||
"node.extend" : "1.0.0",
|
|
||||||
"graceful-fs" : "1.1.5",
|
"graceful-fs" : "1.1.5",
|
||||||
"slide" : "1.1.3",
|
"slide" : "1.1.3",
|
||||||
"semver" : "1.0.13",
|
"semver" : "1.0.13",
|
||||||
|
|
|
@ -12,10 +12,9 @@ if (!exports.isClient) {
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var tsort = require("./tsort");
|
var tsort = require("./tsort");
|
||||||
var util = require("util");
|
var util = require("util");
|
||||||
var extend = require("node.extend");
|
|
||||||
_ = require("underscore");
|
_ = require("underscore");
|
||||||
}else{
|
}else{
|
||||||
var $, jQuery
|
var $, jQuery;
|
||||||
$ = jQuery = require("ep_etherpad-lite/static/js/rjquery").$;
|
$ = jQuery = require("ep_etherpad-lite/static/js/rjquery").$;
|
||||||
_ = require("ep_etherpad-lite/static/js/underscore");
|
_ = require("ep_etherpad-lite/static/js/underscore");
|
||||||
}
|
}
|
||||||
|
@ -31,15 +30,15 @@ exports.ensure = function (cb) {
|
||||||
exports.update(cb);
|
exports.update(cb);
|
||||||
else
|
else
|
||||||
cb();
|
cb();
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.formatPlugins = function () {
|
exports.formatPlugins = function () {
|
||||||
return _.keys(exports.plugins).join(", ");
|
return _.keys(exports.plugins).join(", ");
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.formatParts = function () {
|
exports.formatParts = function () {
|
||||||
return _.map(exports.parts, function (part) { return part.full_name; }).join("\n");
|
return _.map(exports.parts, function (part) { return part.full_name; }).join("\n");
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.formatHooks = function () {
|
exports.formatHooks = function () {
|
||||||
var res = [];
|
var res = [];
|
||||||
|
@ -49,33 +48,39 @@ exports.formatHooks = function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return res.join("\n");
|
return res.join("\n");
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.loadFn = function (path) {
|
exports.loadFn = function (path, hookName) {
|
||||||
var x = path.split(":");
|
var x = path.split(":");
|
||||||
var fn = require(x[0]);
|
var fn = require(x[0]);
|
||||||
_.each(x[1].split("."), function (name) {
|
var functionName = x[1] ? x[1] : hookName;
|
||||||
|
|
||||||
|
_.each(functionName.split("."), function (name) {
|
||||||
fn = fn[name];
|
fn = fn[name];
|
||||||
});
|
});
|
||||||
return fn;
|
return fn;
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.extractHooks = function (parts, hook_set_name) {
|
exports.extractHooks = function (parts, hook_set_name) {
|
||||||
var hooks = {};
|
var hooks = {};
|
||||||
_.each(parts,function (part) {
|
_.each(parts,function (part) {
|
||||||
_.chain(part[hook_set_name] || {}).keys().each(function (hook_name) {
|
_.chain(part[hook_set_name] || {})
|
||||||
|
.keys()
|
||||||
|
.each(function (hook_name) {
|
||||||
if (hooks[hook_name] === undefined) hooks[hook_name] = [];
|
if (hooks[hook_name] === undefined) hooks[hook_name] = [];
|
||||||
|
|
||||||
|
|
||||||
var hook_fn_name = part[hook_set_name][hook_name];
|
var hook_fn_name = part[hook_set_name][hook_name];
|
||||||
var hook_fn = exports.loadFn(part[hook_set_name][hook_name]);
|
var hook_fn = exports.loadFn(hook_fn_name, hook_name);
|
||||||
if (hook_fn) {
|
if (hook_fn) {
|
||||||
hooks[hook_name].push({"hook_name": hook_name, "hook_fn": hook_fn, "hook_fn_name": hook_fn_name, "part": part});
|
hooks[hook_name].push({"hook_name": hook_name, "hook_fn": hook_fn, "hook_fn_name": hook_fn_name, "part": part});
|
||||||
} else {
|
} else {
|
||||||
console.error("Unable to load hook function for " + part.full_name + " for hook " + hook_name + ": " + part.hooks[hook_name]);
|
console.error("Unable to load hook function for " + part.full_name + " for hook " + hook_name + ": " + part.hooks[hook_name]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return hooks;
|
return hooks;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
if (exports.isClient) {
|
if (exports.isClient) {
|
||||||
|
@ -90,7 +95,7 @@ if (exports.isClient) {
|
||||||
console.error("Failed to load plugin-definitions: " + err);
|
console.error("Failed to load plugin-definitions: " + err);
|
||||||
cb();
|
cb();
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
exports.update = function (cb) {
|
exports.update = function (cb) {
|
||||||
|
@ -104,15 +109,15 @@ exports.update = function (cb) {
|
||||||
exports.loadPlugin(packages, plugin_name, plugins, parts, cb);
|
exports.loadPlugin(packages, plugin_name, plugins, parts, cb);
|
||||||
},
|
},
|
||||||
function (err) {
|
function (err) {
|
||||||
exports.plugins = plugins;
|
exports.plugins = plugins;
|
||||||
exports.parts = exports.sortParts(parts);
|
exports.parts = exports.sortParts(parts);
|
||||||
exports.hooks = exports.extractHooks(exports.parts, "hooks");
|
exports.hooks = exports.extractHooks(exports.parts, "hooks");
|
||||||
exports.loaded = true;
|
exports.loaded = true;
|
||||||
cb(err);
|
cb(err);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.getPackages = function (cb) {
|
exports.getPackages = function (cb) {
|
||||||
// Load list of installed NPM packages, flatten it to a list, and filter out only packages with names that
|
// Load list of installed NPM packages, flatten it to a list, and filter out only packages with names that
|
||||||
|
@ -122,49 +127,50 @@ exports.getPackages = function (cb) {
|
||||||
var packages = {};
|
var packages = {};
|
||||||
function flatten(deps) {
|
function flatten(deps) {
|
||||||
_.chain(deps).keys().each(function (name) {
|
_.chain(deps).keys().each(function (name) {
|
||||||
if (name.indexOf(exports.prefix) == 0) {
|
if (name.indexOf(exports.prefix) === 0) {
|
||||||
packages[name] = extend({}, deps[name]);
|
packages[name] = _.clone(deps[name]);
|
||||||
// Delete anything that creates loops so that the plugin
|
// Delete anything that creates loops so that the plugin
|
||||||
// list can be sent as JSON to the web client
|
// list can be sent as JSON to the web client
|
||||||
delete packages[name].dependencies;
|
delete packages[name].dependencies;
|
||||||
delete packages[name].parent;
|
delete packages[name].parent;
|
||||||
}
|
}
|
||||||
if (deps[name].dependencies !== undefined)
|
|
||||||
flatten(deps[name].dependencies);
|
if (deps[name].dependencies !== undefined) flatten(deps[name].dependencies);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmp = {};
|
var tmp = {};
|
||||||
tmp[data.name] = data;
|
tmp[data.name] = data;
|
||||||
flatten(tmp);
|
flatten(tmp);
|
||||||
cb(null, packages);
|
cb(null, packages);
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.loadPlugin = function (packages, plugin_name, plugins, parts, cb) {
|
exports.loadPlugin = function (packages, plugin_name, plugins, parts, cb) {
|
||||||
var plugin_path = path.resolve(packages[plugin_name].path, "ep.json");
|
var plugin_path = path.resolve(packages[plugin_name].path, "ep.json");
|
||||||
fs.readFile(
|
fs.readFile(
|
||||||
plugin_path,
|
plugin_path,
|
||||||
function (er, data) {
|
function (er, data) {
|
||||||
if (er) {
|
if (er) {
|
||||||
console.error("Unable to load plugin definition file " + plugin_path);
|
console.error("Unable to load plugin definition file " + plugin_path);
|
||||||
return cb();
|
return cb();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
var plugin = JSON.parse(data);
|
var plugin = JSON.parse(data);
|
||||||
plugin.package = packages[plugin_name];
|
plugin['package'] = packages[plugin_name];
|
||||||
plugins[plugin_name] = plugin;
|
plugins[plugin_name] = plugin;
|
||||||
_.each(plugin.parts, function (part) {
|
_.each(plugin.parts, function (part) {
|
||||||
part.plugin = plugin_name;
|
part.plugin = plugin_name;
|
||||||
part.full_name = plugin_name + "/" + part.name;
|
part.full_name = plugin_name + "/" + part.name;
|
||||||
parts[part.full_name] = part;
|
parts[part.full_name] = part;
|
||||||
});
|
});
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
console.error("Unable to parse plugin definition file " + plugin_path + ": " + ex.toString());
|
console.error("Unable to parse plugin definition file " + plugin_path + ": " + ex.toString());
|
||||||
}
|
}
|
||||||
cb();
|
cb();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.partsToParentChildList = function (parts) {
|
exports.partsToParentChildList = function (parts) {
|
||||||
var res = [];
|
var res = [];
|
||||||
|
@ -180,7 +186,7 @@ exports.partsToParentChildList = function (parts) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return res;
|
return res;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
// Used only in Node, so no need for _
|
// Used only in Node, so no need for _
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue