mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-30 18:46:01 +01:00
feat: #7743 plugins
This commit is contained in:
@@ -1,21 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
var winston = require('winston');
|
||||
var async = require('async');
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
var nconf = require('nconf');
|
||||
var os = require('os');
|
||||
var cproc = require('child_process');
|
||||
const winston = require('winston');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const nconf = require('nconf');
|
||||
const os = require('os');
|
||||
const cproc = require('child_process');
|
||||
const util = require('util');
|
||||
|
||||
var db = require('../database');
|
||||
var meta = require('../meta');
|
||||
var pubsub = require('../pubsub');
|
||||
var events = require('../events');
|
||||
const db = require('../database');
|
||||
const meta = require('../meta');
|
||||
const pubsub = require('../pubsub');
|
||||
const events = require('../events');
|
||||
|
||||
var packageManager = nconf.get('package_manager') === 'yarn' ? 'yarn' : 'npm';
|
||||
var packageManagerExecutable = packageManager;
|
||||
var packageManagerCommands = {
|
||||
const statAsync = util.promisify(fs.stat);
|
||||
|
||||
const packageManager = nconf.get('package_manager') === 'yarn' ? 'yarn' : 'npm';
|
||||
let packageManagerExecutable = packageManager;
|
||||
const packageManagerCommands = {
|
||||
yarn: {
|
||||
install: 'add',
|
||||
uninstall: 'remove',
|
||||
@@ -45,83 +47,43 @@ module.exports = function (Plugins) {
|
||||
});
|
||||
}
|
||||
|
||||
Plugins.toggleActive = function (id, callback) {
|
||||
callback = callback || function () {};
|
||||
var isActive;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Plugins.isActive(id, next);
|
||||
},
|
||||
function (_isActive, next) {
|
||||
isActive = _isActive;
|
||||
if (isActive) {
|
||||
db.sortedSetRemove('plugins:active', id, next);
|
||||
} else {
|
||||
db.sortedSetCard('plugins:active', function (err, count) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
db.sortedSetAdd('plugins:active', count, id, next);
|
||||
});
|
||||
}
|
||||
},
|
||||
function (next) {
|
||||
meta.reloadRequired = true;
|
||||
Plugins.fireHook(isActive ? 'action:plugin.deactivate' : 'action:plugin.activate', { id: id });
|
||||
setImmediate(next);
|
||||
},
|
||||
function (next) {
|
||||
events.log({
|
||||
type: 'plugin-' + (isActive ? 'deactivate' : 'activate'),
|
||||
text: id,
|
||||
}, next);
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
winston.warn('[plugins] Could not toggle active state on plugin \'' + id + '\'');
|
||||
return callback(err);
|
||||
}
|
||||
callback(null, { id: id, active: !isActive });
|
||||
Plugins.toggleActive = async function (id) {
|
||||
const isActive = await Plugins.isActive(id);
|
||||
if (isActive) {
|
||||
await db.sortedSetRemove('plugins:active', id);
|
||||
} else {
|
||||
const count = await db.sortedSetCard('plugins:active');
|
||||
await db.sortedSetAdd('plugins:active', count, id);
|
||||
}
|
||||
meta.reloadRequired = true;
|
||||
Plugins.fireHook(isActive ? 'action:plugin.deactivate' : 'action:plugin.activate', { id: id });
|
||||
await events.log({
|
||||
type: 'plugin-' + (isActive ? 'deactivate' : 'activate'),
|
||||
text: id,
|
||||
});
|
||||
return { id: id, active: !isActive };
|
||||
};
|
||||
|
||||
Plugins.toggleInstall = function (id, version, callback) {
|
||||
Plugins.toggleInstall = async function (id, version) {
|
||||
pubsub.publish('plugins:toggleInstall', { hostname: os.hostname(), id: id, version: version });
|
||||
toggleInstall(id, version, callback);
|
||||
return await toggleInstall(id, version);
|
||||
};
|
||||
|
||||
function toggleInstall(id, version, callback) {
|
||||
var installed;
|
||||
var type;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Plugins.isInstalled(id, next);
|
||||
},
|
||||
function (_installed, next) {
|
||||
installed = _installed;
|
||||
type = installed ? 'uninstall' : 'install';
|
||||
Plugins.isActive(id, next);
|
||||
},
|
||||
function (active, next) {
|
||||
if (active) {
|
||||
Plugins.toggleActive(id, function (err) {
|
||||
next(err);
|
||||
});
|
||||
return;
|
||||
}
|
||||
setImmediate(next);
|
||||
},
|
||||
function (next) {
|
||||
runPackageManagerCommand(type, id, version || 'latest', next);
|
||||
},
|
||||
function (next) {
|
||||
Plugins.get(id, next);
|
||||
},
|
||||
function (pluginData, next) {
|
||||
Plugins.fireHook('action:plugin.' + type, { id: id, version: version });
|
||||
setImmediate(next, null, pluginData);
|
||||
},
|
||||
], callback);
|
||||
const runPackageManagerCommandAsync = util.promisify(runPackageManagerCommand);
|
||||
|
||||
async function toggleInstall(id, version) {
|
||||
const [installed, active] = await Promise.all([
|
||||
Plugins.isInstalled(id),
|
||||
Plugins.isActive(id),
|
||||
]);
|
||||
const type = installed ? 'uninstall' : 'install';
|
||||
if (active) {
|
||||
await Plugins.toggleActive(id);
|
||||
}
|
||||
await runPackageManagerCommandAsync(type, id, version || 'latest');
|
||||
const pluginData = await Plugins.get(id);
|
||||
Plugins.fireHook('action:plugin.' + type, { id: id, version: version });
|
||||
return pluginData;
|
||||
}
|
||||
|
||||
function runPackageManagerCommand(command, pkgName, version, callback) {
|
||||
@@ -139,37 +101,34 @@ module.exports = function (Plugins) {
|
||||
});
|
||||
}
|
||||
|
||||
Plugins.upgrade = function (id, version, callback) {
|
||||
|
||||
Plugins.upgrade = async function (id, version) {
|
||||
pubsub.publish('plugins:upgrade', { hostname: os.hostname(), id: id, version: version });
|
||||
upgrade(id, version, callback);
|
||||
return await upgrade(id, version);
|
||||
};
|
||||
|
||||
function upgrade(id, version, callback) {
|
||||
async.waterfall([
|
||||
async.apply(runPackageManagerCommand, 'install', id, version || 'latest'),
|
||||
function (next) {
|
||||
Plugins.isActive(id, next);
|
||||
},
|
||||
function (isActive, next) {
|
||||
meta.reloadRequired = isActive;
|
||||
next(null, isActive);
|
||||
},
|
||||
], callback);
|
||||
async function upgrade(id, version) {
|
||||
await runPackageManagerCommandAsync('install', id, version || 'latest');
|
||||
const isActive = await Plugins.isActive(id);
|
||||
meta.reloadRequired = isActive;
|
||||
return isActive;
|
||||
}
|
||||
|
||||
Plugins.isInstalled = function (id, callback) {
|
||||
var pluginDir = path.join(__dirname, '../../node_modules', id);
|
||||
|
||||
fs.stat(pluginDir, function (err, stats) {
|
||||
callback(null, err ? false : stats.isDirectory());
|
||||
});
|
||||
Plugins.isInstalled = async function (id) {
|
||||
const pluginDir = path.join(__dirname, '../../node_modules', id);
|
||||
try {
|
||||
const stats = await statAsync(pluginDir);
|
||||
return stats.isDirectory();
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
Plugins.isActive = function (id, callback) {
|
||||
db.isSortedSetMember('plugins:active', id, callback);
|
||||
Plugins.isActive = async function (id) {
|
||||
return await db.isSortedSetMember('plugins:active', id);
|
||||
};
|
||||
|
||||
Plugins.getActive = function (callback) {
|
||||
db.getSortedSetRange('plugins:active', 0, -1, callback);
|
||||
Plugins.getActive = async function () {
|
||||
return await db.getSortedSetRange('plugins:active', 0, -1);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user