Plugin list can now be reloaded 'live'

This commit is contained in:
Egil Moeller 2012-03-19 17:16:49 +01:00
parent 6fe7f2c2b2
commit c591efb352
6 changed files with 152 additions and 83 deletions

View file

@ -40,14 +40,14 @@ exports.callAll = function (hook_name, args) {
}
exports.aCallAll = function (hook_name, args, cb) {
if (plugins.hooks[hook_name] === undefined) cb([]);
if (plugins.hooks[hook_name] === undefined) return cb(null, []);
async.map(
plugins.hooks[hook_name],
function (hook, cb) {
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
},
function (err, res) {
cb(exports.flatten(res));
cb(null, exports.flatten(res));
}
);
}
@ -58,8 +58,8 @@ exports.callFirst = function (hook_name, args) {
}
exports.aCallFirst = function (hook_name, args, cb) {
if (plugins.hooks[hook_name][0] === undefined) cb([]);
hookCallWrapper(plugins.hooks[hook_name][0], hook_name, args, function (res) { cb(exports.flatten(res)); });
if (plugins.hooks[hook_name][0] === undefined) return cb(null, []);
hookCallWrapper(plugins.hooks[hook_name][0], hook_name, args, function (res) { cb(null, exports.flatten(res)); });
}
exports.callAllStr = function(hook_name, args, sep, pre, post) {

View file

@ -3,43 +3,74 @@ var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
var npm = require("npm");
var registry = require("npm/lib/utils/npm-registry-client/index.js");
exports.uninstall = function(plugin_name, cb) {
var withNpm = function (npmfn, cb) {
npm.load({}, function (er) {
if (er) return cb(er)
npm.commands.uninstall([plugin_name], function (er) {
if (er) return cb(er);
hooks.aCallAll("pluginUninstall", {plugin_name: plugin_name}, function (er) {
cb(er);
});
})
})
}
exports.install = function(plugin_name, cb) {
npm.load({}, function (er) {
if (er) return cb(er)
npm.commands.install([plugin_name], function (er) {
if (er) return cb(er);
hooks.aCallAll("pluginInstall", {plugin_name: plugin_name}, function (er) {
cb(er);
});
if (er) return cb({progress:1, error:er});
npm.on("log", function (message) {
cb({progress: 0.5, message:message.msg + ": " + message.pref});
});
npmfn(function (er, data) {
if (er) return cb({progress:1, error:er.code + ": " + er.path});
if (!data) data = {};
data.progress = 1;
data.message = "Done.";
cb(data);
});
})
}
exports.search = function(pattern, cb) {
npm.load({}, function (er) {
registry.get(
"/-/all", null, 600, false, true,
function (er, data) {
if (er) return cb(er);
var res = {};
for (key in data) {
if (/*key.indexOf(plugins.prefix) == 0 &&*/ key.indexOf(pattern) != -1)
res[key] = data[key];
}
cb(null, res);
}
);
});
}
// All these functions call their callback multiple times with
// {progress:[0,1], message:STRING, error:object}. They will call it
// with progress = 1 at least once, and at all times will either
// message or error be present, not both. It can be called multiple
// times for all values of propgress except for 1.
exports.uninstall = function(plugin_name, cb) {
withNpm(
function (cb) {
npm.commands.uninstall([plugin_name], function (er) {
if (er) return cb(er);
hooks.aCallAll("pluginUninstall", {plugin_name: plugin_name}, function (er, data) {
if (er) return cb(er);
plugins.update(cb);
});
});
},
cb
);
};
exports.install = function(plugin_name, cb) {
withNpm(
function (cb) {
npm.commands.install([plugin_name], function (er) {
if (er) return cb(er);
hooks.aCallAll("pluginInstall", {plugin_name: plugin_name}, function (er, data) {
if (er) return cb(er);
plugins.update(cb);
});
});
},
cb
);
};
exports.search = function(pattern, cb) {
withNpm(
function (cb) {
registry.get(
"/-/all", null, 600, false, true,
function (er, data) {
if (er) return cb(er);
var res = {};
for (key in data) {
if (/*key.indexOf(plugins.prefix) == 0 &&*/ key.indexOf(pattern) != -1)
res[key] = data[key];
}
cb(null, {results:res});
}
);
},
cb
);
};

View file

@ -2,7 +2,7 @@ exports.isClient = typeof global != "object";
if (!exports.isClient) {
var npm = require("npm/lib/npm.js");
var readInstalled = require("npm/lib/utils/read-installed.js");
var readInstalled = require("./read-installed.js");
var relativize = require("npm/lib/utils/relativize.js");
var readJson = require("npm/lib/utils/read-json.js");
var path = require("path");
@ -10,6 +10,7 @@ if (!exports.isClient) {
var fs = require("fs");
var tsort = require("./tsort");
var util = require("util");
var extend = require("node.extend");
}
exports.prefix = 'ep_';
@ -112,14 +113,19 @@ exports.getPackages = function (cb) {
function flatten(deps) {
Object.keys(deps).forEach(function (name) {
if (name.indexOf(exports.prefix) == 0) {
packages[name] = deps[name];
packages[name] = extend({}, 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;
}
if (deps[name].dependencies !== undefined)
flatten(deps[name].dependencies);
delete deps[name].dependencies;
});
}
flatten([data]);
var tmp = {};
tmp[data.name] = data;
flatten(tmp);
cb(null, packages);
});
}