etherpad-lite/src/static/js/pluginfw/installer.js

77 lines
2.1 KiB
JavaScript
Raw Normal View History

var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
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");
2012-03-19 17:16:49 +01:00
var withNpm = function (npmfn, cb) {
npm.load({}, function (er) {
2012-03-19 17:16:49 +01:00
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);
});
});
}
2012-03-19 17:16:49 +01:00
// 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) {
2012-03-19 17:16:49 +01:00
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);
});
});
2012-03-19 17:16:49 +01:00
},
cb
);
};
exports.search = function(pattern, cb) {
2012-03-19 17:16:49 +01:00
withNpm(
function (cb) {
registry.get(
"/-/all", null, 600, false, true,
function (er, data) {
if (er) return cb(er);
var res = {};
for (key in data) {
2012-03-21 19:28:39 +01:00
if (key.indexOf(plugins.prefix) == 0 && key.indexOf(pattern) != -1)
2012-03-19 17:16:49 +01:00
res[key] = data[key];
}
cb(null, {results:res});
}
2012-03-19 17:16:49 +01:00
);
},
cb
);
};