mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 07:56:16 -04:00
Make npm registry access code more sane
This commit is contained in:
parent
0070eab416
commit
b297784288
2 changed files with 46 additions and 94 deletions
|
@ -28,7 +28,6 @@
|
||||||
"jsdom-nocontextifiy" : "0.2.10",
|
"jsdom-nocontextifiy" : "0.2.10",
|
||||||
"async-stacktrace" : "0.0.2",
|
"async-stacktrace" : "0.0.2",
|
||||||
"npm" : "1.2.x",
|
"npm" : "1.2.x",
|
||||||
"npm-registry-client" : "0.2.10",
|
|
||||||
"ejs" : "0.6.1",
|
"ejs" : "0.6.1",
|
||||||
"graceful-fs" : "1.1.5",
|
"graceful-fs" : "1.1.5",
|
||||||
"slide" : "1.1.3",
|
"slide" : "1.1.3",
|
||||||
|
|
|
@ -1,120 +1,73 @@
|
||||||
var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
|
var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
|
||||||
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
|
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
|
||||||
var npm = require("npm");
|
var npm = require("npm");
|
||||||
var RegClient = require("npm-registry-client")
|
|
||||||
|
|
||||||
var registry = new RegClient(
|
var withNpm = function (npmfn, cb) {
|
||||||
{ registry: "http://registry.npmjs.org"
|
|
||||||
, cache: npm.cache }
|
|
||||||
);
|
|
||||||
|
|
||||||
var withNpm = function (npmfn, final, cb) {
|
|
||||||
npm.load({}, function (er) {
|
npm.load({}, function (er) {
|
||||||
if (er) return cb({progress:1, error:er});
|
if (er) return cb(er);
|
||||||
npm.on("log", function (message) {
|
npm.on("log", function (message) {
|
||||||
cb({progress: 0.5, message:message.msg + ": " + message.pref});
|
console.log('npm: ',message)
|
||||||
});
|
|
||||||
npmfn(function (er, data) {
|
|
||||||
if (er) {
|
|
||||||
console.error(er);
|
|
||||||
return cb({progress:1, error: er.message});
|
|
||||||
}
|
|
||||||
if (!data) data = {};
|
|
||||||
data.progress = 1;
|
|
||||||
data.message = "Done.";
|
|
||||||
cb(data);
|
|
||||||
final();
|
|
||||||
});
|
});
|
||||||
|
npmfn();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
exports.uninstall = function(plugin_name, cb) {
|
||||||
withNpm(
|
withNpm(function () {
|
||||||
function (cb) {
|
npm.commands.uninstall([plugin_name], function (er) {
|
||||||
npm.commands.uninstall([plugin_name], function (er) {
|
if (er) return cb && cb(er);
|
||||||
|
hooks.aCallAll("pluginUninstall", {plugin_name: plugin_name}, function (er, data) {
|
||||||
if (er) return cb(er);
|
if (er) return cb(er);
|
||||||
hooks.aCallAll("pluginUninstall", {plugin_name: plugin_name}, function (er, data) {
|
plugins.update(cb);
|
||||||
if (er) return cb(er);
|
cb && cb();
|
||||||
plugins.update(cb);
|
hooks.aCallAll("restartServer", {}, function () {});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
},
|
});
|
||||||
function () {
|
});
|
||||||
hooks.aCallAll("restartServer", {}, function () {});
|
|
||||||
},
|
|
||||||
cb
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.install = function(plugin_name, cb) {
|
exports.install = function(plugin_name, cb) {
|
||||||
withNpm(
|
withNpm(function () {
|
||||||
function (cb) {
|
|
||||||
npm.commands.install([plugin_name], function (er) {
|
npm.commands.install([plugin_name], function (er) {
|
||||||
if (er) return cb(er);
|
if (er) return cb && cb(er);
|
||||||
hooks.aCallAll("pluginInstall", {plugin_name: plugin_name}, function (er, data) {
|
hooks.aCallAll("pluginInstall", {plugin_name: plugin_name}, function (er, data) {
|
||||||
if (er) return cb(er);
|
if (er) return cb(er);
|
||||||
plugins.update(cb);
|
plugins.update(cb);
|
||||||
|
cb && cb();
|
||||||
|
hooks.aCallAll("restartServer", {}, function () {});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
});
|
||||||
function () {
|
|
||||||
hooks.aCallAll("restartServer", {}, function () {});
|
|
||||||
},
|
|
||||||
cb
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.searchCache = null;
|
exports.availablePlugins = null;
|
||||||
var cacheTimestamp = 0;
|
var cacheTimestamp = 0;
|
||||||
|
|
||||||
exports.search = function(query, maxCacheAge, cb) {
|
exports.getAvailablePlugins = function(maxCacheAge, cb) {
|
||||||
withNpm(
|
withNpm(function () {
|
||||||
function (cb) {
|
if(exports.availablePlugins && maxCacheAge && Math.round(+new Date/1000)-cacheTimestamp <= maxCacheAge) {
|
||||||
var getData = function (cb) {
|
return cb && cb(null, exports.availablePlugins)
|
||||||
if (maxCacheAge && exports.searchCache && Math.round(+new Date/1000)-cacheTimestamp < maxCacheAge) {
|
}
|
||||||
cb(null, exports.searchCache);
|
npm.commands.search(['ep_'], function(er, results) {
|
||||||
} else {
|
if(er) return cb && cb(er);
|
||||||
registry.get(
|
exports.availablePlugins = results;
|
||||||
"/-/all", 600, false, true,
|
cacheTimestamp = Math.round(+new Date/1000);
|
||||||
function (er, data) {
|
cb && cb(null, results)
|
||||||
if (er) return cb(er);
|
})
|
||||||
exports.searchCache = data;
|
});
|
||||||
cacheTimestamp = Math.round(+new Date/1000)
|
};
|
||||||
cb(er, data);
|
|
||||||
}
|
|
||||||
);
|
exports.search = function(searchTerm, maxCacheAge, cb) {
|
||||||
}
|
exports.getAvailablePlugins(maxCacheAge, function(er, results) {
|
||||||
}
|
if(er) return cb && cb(er);
|
||||||
getData(
|
var res = {};
|
||||||
function (er, data) {
|
searchTerm = searchTerm.toLowerCase();
|
||||||
if (er) return cb(er);
|
for (var pluginName in results) { // for every available plugin
|
||||||
var res = {};
|
if (pluginName.indexOf(plugins.prefix) != 0) continue; // TODO: Also search in keywords here!
|
||||||
var i = 0;
|
if(pluginName.indexOf(searchTerm) < 0 && results[pluginName].description.indexOf(searchTerm) < 0) continue;
|
||||||
var pattern = query.pattern.toLowerCase();
|
res[pluginName] = results[pluginName];
|
||||||
for (key in data) { // for every plugin in the data from npm
|
}
|
||||||
if ( key.indexOf(plugins.prefix) == 0
|
cb && cb(null, res)
|
||||||
&& key.indexOf(pattern) != -1
|
})
|
||||||
|| key.indexOf(plugins.prefix) == 0
|
|
||||||
&& data[key].description.indexOf(pattern) != -1
|
|
||||||
) { // If the name contains ep_ and the search string is in the name or description
|
|
||||||
i++;
|
|
||||||
if (i > query.offset
|
|
||||||
&& i <= query.offset + query.limit) {
|
|
||||||
res[key] = data[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cb(null, {results:res, query: query, total:i});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
},
|
|
||||||
function () { },
|
|
||||||
cb
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue