mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: async/await, remove dupe code for homepage routes
This commit is contained in:
@@ -1,67 +1,58 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
var plugins = require('../../plugins');
|
||||
var meta = require('../../meta');
|
||||
const nconf = require('nconf');
|
||||
const winston = require('winston');
|
||||
const plugins = require('../../plugins');
|
||||
const meta = require('../../meta');
|
||||
|
||||
var pluginsController = module.exports;
|
||||
const pluginsController = module.exports;
|
||||
|
||||
pluginsController.get = function (req, res, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
compatible: function (next) {
|
||||
plugins.list(function (err, plugins) {
|
||||
if (err || !Array.isArray(plugins)) {
|
||||
plugins = [];
|
||||
}
|
||||
pluginsController.get = async function (req, res) {
|
||||
const [compatible, all] = await Promise.all([
|
||||
getCompatiblePluigns(),
|
||||
getAllPlugins(),
|
||||
]);
|
||||
|
||||
next(null, plugins);
|
||||
});
|
||||
},
|
||||
all: function (next) {
|
||||
plugins.list(false, function (err, plugins) {
|
||||
if (err || !Array.isArray(plugins)) {
|
||||
plugins = [];
|
||||
}
|
||||
const compatiblePkgNames = compatible.map(pkgData => pkgData.name);
|
||||
const installedPlugins = compatible.filter(plugin => plugin && plugin.installed);
|
||||
const activePlugins = all.filter(plugin => plugin && plugin.installed && plugin.active);
|
||||
|
||||
next(null, plugins);
|
||||
});
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (payload) {
|
||||
var compatiblePkgNames = payload.compatible.map(function (pkgData) {
|
||||
return pkgData.name;
|
||||
});
|
||||
var installedPlugins = payload.compatible.filter(function (plugin) {
|
||||
return plugin && plugin.installed;
|
||||
});
|
||||
var activePlugins = payload.all.filter(function (plugin) {
|
||||
return plugin && plugin.installed && plugin.active;
|
||||
});
|
||||
|
||||
res.render('admin/extend/plugins', {
|
||||
installed: installedPlugins,
|
||||
installedCount: installedPlugins.length,
|
||||
activeCount: activePlugins.length,
|
||||
inactiveCount: Math.max(0, installedPlugins.length - activePlugins.length),
|
||||
upgradeCount: payload.compatible.reduce(function (count, current) {
|
||||
if (current.installed && current.outdated) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}, 0),
|
||||
download: payload.compatible.filter(function (plugin) {
|
||||
return !plugin.installed;
|
||||
}),
|
||||
incompatible: payload.all.filter(function (plugin) {
|
||||
return !compatiblePkgNames.includes(plugin.name);
|
||||
}),
|
||||
submitPluginUsage: meta.config.submitPluginUsage,
|
||||
version: nconf.get('version'),
|
||||
});
|
||||
},
|
||||
], next);
|
||||
res.render('admin/extend/plugins', {
|
||||
installed: installedPlugins,
|
||||
installedCount: installedPlugins.length,
|
||||
activeCount: activePlugins.length,
|
||||
inactiveCount: Math.max(0, installedPlugins.length - activePlugins.length),
|
||||
upgradeCount: compatible.reduce(function (count, current) {
|
||||
if (current.installed && current.outdated) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}, 0),
|
||||
download: compatible.filter(function (plugin) {
|
||||
return !plugin.installed;
|
||||
}),
|
||||
incompatible: all.filter(function (plugin) {
|
||||
return !compatiblePkgNames.includes(plugin.name);
|
||||
}),
|
||||
submitPluginUsage: meta.config.submitPluginUsage,
|
||||
version: nconf.get('version'),
|
||||
});
|
||||
};
|
||||
|
||||
async function getCompatiblePluigns() {
|
||||
return await getPlugins(true);
|
||||
}
|
||||
|
||||
async function getAllPlugins() {
|
||||
return await getPlugins(false);
|
||||
}
|
||||
|
||||
async function getPlugins(matching) {
|
||||
try {
|
||||
const pluginsData = await plugins.list(matching);
|
||||
return pluginsData || [];
|
||||
} catch (err) {
|
||||
winston.error(err);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user