Skip to content

Commit

Permalink
feat: add ./nodebb install <plugin_name>
Browse files Browse the repository at this point in the history
will install the suggested version for current nodebb closes #11060
  • Loading branch information
barisusakli committed Nov 24, 2022
1 parent 8abea39 commit 6fe8797
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,16 @@ program
});

program
.command('install')
.description('Launch the NodeBB web installer for configuration setup')
.action(() => {
require('./setup').webInstall();
.command('install [plugin]')
.description('Launch the NodeBB web installer for configuration setup or install a plugin')
.action((plugin) => {
if (plugin) {
require('./manage').install(plugin);
} else {
require('./setup').webInstall();
}
});

program
.command('build [targets...]')
.description(`Compile static assets ${chalk.red('(JS, CSS, templates, languages)')}`)
Expand Down
32 changes: 31 additions & 1 deletion src/cli/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,36 @@ const plugins = require('../plugins');
const events = require('../events');
const analytics = require('../analytics');
const reset = require('./reset');
const { pluginNamePattern, themeNamePattern } = require('../constants');
const { pluginNamePattern, themeNamePattern, paths } = require('../constants');

async function install(plugin) {
try {
await db.init();
if (!pluginNamePattern.test(plugin)) {
// Allow omission of `nodebb-plugin-`
plugin = `nodebb-plugin-${plugin}`;
}

plugin = await plugins.autocomplete(plugin);

const isInstalled = await plugins.isInstalled(plugin);
if (isInstalled) {
throw new Error('plugin already installed');
}
const nbbVersion = require(paths.currentPackage).version;
const suggested = await plugins.suggest(plugin, nbbVersion);
if (!suggested.version) {
throw new Error(suggested.message);
}
winston.info('Installing Plugin `%s@%s`', plugin, suggested.version);
await plugins.toggleInstall(plugin, suggested.version);

process.exit(0);
} catch (err) {
winston.error(`An error occurred during plugin installation\n${err.stack}`);
process.exit(1);
}
}

async function activate(plugin) {
if (themeNamePattern.test(plugin)) {
Expand Down Expand Up @@ -166,6 +195,7 @@ async function buildWrapper(targets, options) {
}

exports.build = buildWrapper;
exports.install = install;
exports.activate = activate;
exports.listPlugins = listPlugins;
exports.listEvents = listEvents;
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ module.exports = function (Plugins) {
throw new Error('[[error:plugin-not-whitelisted]]');
};

Plugins.suggest = async function (pluginId, nbbVersion) {
const body = await request({
method: 'GET',
url: `https://packages.nodebb.org/api/v1/suggest?package=${encodeURIComponent(pluginId)}&version=${encodeURIComponent(nbbVersion)}`,
json: true,
});
return body;
};

Plugins.toggleInstall = async function (id, version) {
pubsub.publish('plugins:toggleInstall', { hostname: os.hostname(), id: id, version: version });
return await toggleInstall(id, version);
Expand Down

0 comments on commit 6fe8797

Please sign in to comment.