Files
NodeBB/src/plugins/install.js

155 lines
4.2 KiB
JavaScript
Raw Normal View History

2014-12-26 18:54:20 -05:00
'use strict';
2019-07-22 00:30:47 -04:00
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');
const request = require('request-promise-native');
2019-07-22 00:30:47 -04:00
const db = require('../database');
const meta = require('../meta');
const pubsub = require('../pubsub');
const { paths } = require('../constants');
2019-07-22 00:30:47 -04:00
const supportedPackageManagerList = require('../cli/package-install').supportedPackageManager;
// load config from src/cli/package-install.js
const packageManager = supportedPackageManagerList.indexOf(nconf.get('package_manager')) >= 0 ? nconf.get('package_manager') : 'npm';
2019-07-22 00:30:47 -04:00
let packageManagerExecutable = packageManager;
const packageManagerCommands = {
yarn: {
install: 'add',
uninstall: 'remove',
},
npm: {
install: 'install',
uninstall: 'uninstall',
},
cnpm: {
install: 'install',
uninstall: 'uninstall',
},
pnpm: {
install: 'install',
uninstall: 'uninstall',
},
};
if (process.platform === 'win32') {
packageManagerExecutable += '.cmd';
}
module.exports = function (Plugins) {
if (nconf.get('isPrimary')) {
2021-02-04 00:01:39 -07:00
pubsub.on('plugins:toggleInstall', (data) => {
if (data.hostname !== os.hostname()) {
toggleInstall(data.id, data.version);
}
});
2021-02-04 00:01:39 -07:00
pubsub.on('plugins:upgrade', (data) => {
if (data.hostname !== os.hostname()) {
upgrade(data.id, data.version);
}
});
}
2019-07-22 00:30:47 -04:00
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;
const hook = isActive ? 'deactivate' : 'activate';
Plugins.hooks.fire(`action:plugin.${isActive}`, { id: id });
2019-07-22 00:30:47 -04:00
return { id: id, active: !isActive };
2014-12-26 18:54:20 -05:00
};
Plugins.checkWhitelist = async function (id, version) {
const body = await request({
method: 'GET',
url: `https://packages.nodebb.org/api/v1/plugins/${encodeURIComponent(id)}`,
json: true,
});
if (body && body.code === 'ok' && (version === 'latest' || body.payload.valid.includes(version))) {
return;
}
throw new Error('[[error:plugin-not-whitelisted]]');
};
2019-07-22 00:30:47 -04:00
Plugins.toggleInstall = async function (id, version) {
2017-02-18 12:30:49 -07:00
pubsub.publish('plugins:toggleInstall', { hostname: os.hostname(), id: id, version: version });
2019-07-22 00:30:47 -04:00
return await toggleInstall(id, version);
};
2019-07-22 00:30:47 -04:00
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);
2021-02-03 23:59:08 -07:00
Plugins.hooks.fire(`action:plugin.${type}`, { id: id, version: version });
2019-07-22 00:30:47 -04:00
return pluginData;
}
2014-12-26 18:54:20 -05:00
function runPackageManagerCommand(command, pkgName, version, callback) {
cproc.execFile(packageManagerExecutable, [
packageManagerCommands[packageManager][command],
2021-02-03 23:59:08 -07:00
pkgName + (command === 'install' ? `@${version}` : ''),
'--save',
2021-02-04 00:01:39 -07:00
], (err, stdout) => {
if (err) {
return callback(err);
}
2021-02-03 23:59:08 -07:00
winston.verbose(`[plugins/${command}] ${stdout}`);
2016-11-18 14:03:06 +03:00
callback();
2016-12-15 16:08:32 +03:00
});
}
2019-07-22 00:30:47 -04:00
Plugins.upgrade = async function (id, version) {
2017-02-18 12:30:49 -07:00
pubsub.publish('plugins:upgrade', { hostname: os.hostname(), id: id, version: version });
2019-07-22 00:30:47 -04:00
return await upgrade(id, version);
};
2019-07-22 00:30:47 -04:00
async function upgrade(id, version) {
await runPackageManagerCommandAsync('install', id, version || 'latest');
const isActive = await Plugins.isActive(id);
meta.reloadRequired = isActive;
return isActive;
}
2014-12-26 18:54:20 -05:00
2019-07-22 00:30:47 -04:00
Plugins.isInstalled = async function (id) {
const pluginDir = path.join(paths.nodeModules, id);
2019-07-22 00:30:47 -04:00
try {
2020-08-14 00:05:03 -04:00
const stats = await fs.promises.stat(pluginDir);
2019-07-22 00:30:47 -04:00
return stats.isDirectory();
} catch (err) {
return false;
}
2014-12-26 18:54:20 -05:00
};
2019-07-22 00:30:47 -04:00
Plugins.isActive = async function (id) {
return await db.isSortedSetMember('plugins:active', id);
2014-12-26 18:54:20 -05:00
};
2015-02-23 15:55:35 -05:00
2019-07-22 00:30:47 -04:00
Plugins.getActive = async function () {
return await db.getSortedSetRange('plugins:active', 0, -1);
2015-02-23 15:55:35 -05:00
};
2017-02-18 02:30:48 -07:00
};