prepare to async: trivial reformatting

This change is only cosmetic. Its aim is do make it easier to understand the
async changes that are going to be merged later on. It was extracted from the
original work from Ray Bellis.

To verify that nothing has changed, you can run the following command on each
file touched by this commit:
  npm install uglify-es
  diff --unified <(uglify-js --beautify bracketize <BEFORE.js>) <(uglify-js --beautify bracketize <AFTER.js>)



This is a complete script that does the same automatically (works from a
mercurial clone):

```bash
#!/usr/bin/env bash

set -eu

REVISION=<THIS_REVISION>

PARENT_REV=$(hg identify --rev "${REVISION}" --template '{p1rev}')
FILE_LIST=$(hg status --no-status --change ${REVISION})
UGLIFYJS="node_modules/uglify-es/bin/uglifyjs"

for FILE_NAME in ${FILE_LIST[@]}; do
  echo "Checking ${FILE_NAME}"
  diff --unified \
    <("${UGLIFYJS}" --beautify bracketize <(hg cat --rev "${PARENT_REV}" "${FILE_NAME}")) \
    <("${UGLIFYJS}" --beautify bracketize <(hg cat --rev "${REVISION}"   "${FILE_NAME}"))
done
```
This commit is contained in:
muxator 2019-02-08 23:20:57 +01:00
parent cc23bd18a4
commit 9497ee734f
33 changed files with 2706 additions and 2943 deletions

View file

@ -4,12 +4,14 @@ var npm = require("npm");
var request = require("request");
var npmIsLoaded = false;
var withNpm = function (npmfn) {
if(npmIsLoaded) return npmfn();
npm.load({}, function (er) {
var withNpm = function(npmfn) {
if (npmIsLoaded) return npmfn();
npm.load({}, function(er) {
if (er) return npmfn(er);
npmIsLoaded = true;
npm.on("log", function (message) {
npm.on("log", function(message) {
console.log('npm: ',message)
});
npmfn();
@ -17,26 +19,33 @@ var withNpm = function (npmfn) {
}
var tasks = 0
function wrapTaskCb(cb) {
tasks++
tasks++;
return function() {
cb && cb.apply(this, arguments);
tasks--;
if(tasks == 0) onAllTasksFinished();
if (tasks == 0) onAllTasksFinished();
}
}
function onAllTasksFinished() {
hooks.aCallAll("restartServer", {}, function () {});
hooks.aCallAll("restartServer", {}, function() {});
}
exports.uninstall = function(plugin_name, cb) {
cb = wrapTaskCb(cb);
withNpm(function (er) {
withNpm(function(er) {
if (er) return cb && cb(er);
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) {
hooks.aCallAll("pluginUninstall", {plugin_name: plugin_name}, function(er, data) {
if (er) return cb(er);
plugins.update(cb);
});
});
@ -44,13 +53,17 @@ exports.uninstall = function(plugin_name, cb) {
};
exports.install = function(plugin_name, cb) {
cb = wrapTaskCb(cb)
withNpm(function (er) {
cb = wrapTaskCb(cb);
withNpm(function(er) {
if (er) return cb && cb(er);
npm.commands.install([plugin_name], function (er) {
npm.commands.install([plugin_name], function(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);
plugins.update(cb);
});
});
@ -63,41 +76,53 @@ var cacheTimestamp = 0;
exports.getAvailablePlugins = function(maxCacheAge, cb) {
request("https://static.etherpad.org/plugins.json", function(er, response, plugins){
if (er) return cb && cb(er);
if(exports.availablePlugins && maxCacheAge && Math.round(+new Date/1000)-cacheTimestamp <= maxCacheAge) {
return cb && cb(null, exports.availablePlugins)
if (exports.availablePlugins && maxCacheAge && Math.round(+ new Date / 1000) - cacheTimestamp <= maxCacheAge) {
return cb && cb(null, exports.availablePlugins);
}
try {
plugins = JSON.parse(plugins);
} catch (err) {
console.error('error parsing plugins.json:', err);
plugins = [];
}
exports.availablePlugins = plugins;
cacheTimestamp = Math.round(+new Date/1000);
cb && cb(null, plugins)
cacheTimestamp = Math.round(+ new Date / 1000);
cb && cb(null, plugins);
});
};
exports.search = function(searchTerm, maxCacheAge, cb) {
exports.getAvailablePlugins(maxCacheAge, function(er, results) {
if(er) return cb && cb(er);
if (er) return cb && cb(er);
var res = {};
if (searchTerm)
if (searchTerm) {
searchTerm = searchTerm.toLowerCase();
for (var pluginName in results) { // for every available plugin
}
for (var pluginName in results) {
// for every available plugin
if (pluginName.indexOf(plugins.prefix) != 0) continue; // TODO: Also search in keywords here!
if(searchTerm && !~results[pluginName].name.toLowerCase().indexOf(searchTerm)
if (searchTerm && !~results[pluginName].name.toLowerCase().indexOf(searchTerm)
&& (typeof results[pluginName].description != "undefined" && !~results[pluginName].description.toLowerCase().indexOf(searchTerm) )
){
if(typeof results[pluginName].description === "undefined"){
) {
if (typeof results[pluginName].description === "undefined") {
console.debug('plugin without Description: %s', results[pluginName].name);
}
continue;
}
res[pluginName] = results[pluginName];
}
cb && cb(null, res)
})
cb && cb(null, res);
});
};

View file

@ -55,6 +55,7 @@ exports.formatHooks = function (hook_set_name) {
exports.callInit = function (cb) {
var hooks = require("./hooks");
async.map(
Object.keys(exports.plugins),
function (plugin_name, cb) {
@ -83,6 +84,7 @@ exports.update = function (cb) {
exports.getPackages(function (er, packages) {
var parts = [];
var plugins = {};
// Load plugin metadata ep.json
async.forEach(
Object.keys(packages),
@ -106,6 +108,7 @@ exports.getPackages = function (cb) {
var dir = path.resolve(npm.dir, '..');
readInstalled(dir, function (er, data) {
if (er) cb(er, null);
var packages = {};
function flatten(deps) {
_.chain(deps).keys().each(function (name) {
@ -116,12 +119,12 @@ exports.getPackages = function (cb) {
delete packages[name].dependencies;
delete packages[name].parent;
}
// I don't think we need recursion
//if (deps[name].dependencies !== undefined) flatten(deps[name].dependencies);
});
}
var tmp = {};
tmp[data.name] = data;
flatten(tmp[data.name].dependencies);