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

121 lines
3.5 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 RegClient = require("npm-registry-client")
var registry = new RegClient(
{ registry: "http://registry.npmjs.org"
, cache: npm.cache }
);
var withNpm = function (npmfn, final, 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) {
console.error(er);
return cb({progress:1, error: er.message});
}
2012-03-19 17:16:49 +01:00
if (!data) data = {};
data.progress = 1;
data.message = "Done.";
cb(data);
final();
2012-03-19 17:16:49 +01:00
});
});
}
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);
});
});
},
function () {
hooks.aCallAll("restartServer", {}, function () {});
},
2012-03-19 17:16:49 +01:00
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
},
function () {
hooks.aCallAll("restartServer", {}, function () {});
},
2012-03-19 17:16:49 +01:00
cb
);
};
2012-04-18 13:43:34 +02:00
exports.searchCache = null;
var cacheTimestamp = 0;
2012-04-18 13:43:34 +02:00
exports.search = function(query, maxCacheAge, cb) {
2012-03-19 17:16:49 +01:00
withNpm(
function (cb) {
2012-04-18 13:43:34 +02:00
var getData = function (cb) {
if (maxCacheAge && exports.searchCache && Math.round(+new Date/1000)-cacheTimestamp < maxCacheAge) {
2012-04-18 13:43:34 +02:00
cb(null, exports.searchCache);
} else {
registry.get(
"/-/all", 600, false, true,
2012-04-18 13:43:34 +02:00
function (er, data) {
if (er) return cb(er);
exports.searchCache = data;
cacheTimestamp = Math.round(+new Date/1000)
2012-04-18 13:43:34 +02:00
cb(er, data);
}
);
}
}
getData(
2012-03-19 17:16:49 +01:00
function (er, data) {
if (er) return cb(er);
var res = {};
2012-04-17 22:18:43 +02:00
var i = 0;
2013-01-31 19:05:23 +01:00
var pattern = query.pattern.toLowerCase();
for (key in data) { // for every plugin in the data from npm
2012-04-18 13:43:34 +02:00
if ( key.indexOf(plugins.prefix) == 0
2013-01-31 19:05:23 +01:00
&& key.indexOf(pattern) != -1
|| key.indexOf(plugins.prefix) == 0
2013-01-31 19:05:23 +01:00
&& data[key].description.indexOf(pattern) != -1
) { // If the name contains ep_ and the search string is in the name or description
2012-04-17 22:18:43 +02:00
i++;
if (i > query.offset
&& i <= query.offset + query.limit) {
res[key] = data[key];
}
}
2012-03-19 17:16:49 +01:00
}
2012-04-17 22:18:43 +02:00
cb(null, {results:res, query: query, total:i});
}
2012-03-19 17:16:49 +01:00
);
},
function () { },
2012-03-19 17:16:49 +01:00
cb
);
};