Skip to content

Commit

Permalink
fix: #8686, deprecate plugin.json/library (#8705)
Browse files Browse the repository at this point in the history
and also fix deprecated hooks with no alternative
  • Loading branch information
pitaj committed Oct 1, 2020
1 parent 39b5170 commit 017af63
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
15 changes: 9 additions & 6 deletions src/plugins/hooks.js
Expand Up @@ -35,12 +35,15 @@ module.exports = function (Plugins) {
return;
}

if (Plugins.deprecatedHooks[data.hook]) {
winston.warn('[plugins/' + id + '] Hook `' + data.hook + '` is deprecated, ' +
(Plugins.deprecatedHooks[data.hook] ?
'please use `' + Plugins.deprecatedHooks[data.hook] + '` instead.' :
'there is no alternative.'
));
// `hasOwnProperty` needed for hooks with no alternative (set to null)
if (Plugins.deprecatedHooks.hasOwnProperty(data.hook)) {
const deprecated = Plugins.deprecatedHooks[data.hook];

if (deprecated) {
winston.warn(`[plugins/${id}] Hook "${data.hook}" is deprecated, please use "${deprecated}" instead.`);
} else {
winston.warn(`[plugins/${id}] Hook "${data.hook}" is deprecated, there is no alternative.`);
}
}

data.id = id;
Expand Down
24 changes: 22 additions & 2 deletions src/plugins/index.js
Expand Up @@ -41,8 +41,28 @@ Plugins.loadedPlugins = [];

Plugins.initialized = false;

Plugins.requireLibrary = function (pluginID, libraryPath) {
Plugins.libraries[pluginID] = require(libraryPath);
Plugins.requireLibrary = function (pluginData) {
let libraryPath;
// attempt to load a plugin directly with `require("nodebb-plugin-*")`
// Plugins should define their entry point in the standard `main` property of `package.json`
try {
libraryPath = pluginData.path;
Plugins.libraries[pluginData.id] = require(libraryPath);
} catch (e) {
// DEPRECATED: @1.15.0, remove in version >=1.17
// for backwards compatibility
// if that fails, fall back to `pluginData.library`
if (pluginData.library) {
winston.warn(` [plugins/${pluginData.id}] The plugin.json field "library" is deprecated. Please use the package.json field "main" instead.`);
winston.verbose(`[plugins/${pluginData.id}] See https://github.com/NodeBB/NodeBB/issues/8686`);

libraryPath = path.join(pluginData.path, pluginData.library);
Plugins.libraries[pluginData.id] = require(libraryPath);
} else {
throw e;
}
}

Plugins.libraryPaths.push(libraryPath);
};

Expand Down
11 changes: 2 additions & 9 deletions src/plugins/load.js
@@ -1,6 +1,5 @@
'use strict';

const path = require('path');
const semver = require('semver');
const async = require('async');
const winston = require('winston');
Expand Down Expand Up @@ -156,22 +155,16 @@ module.exports = function (Plugins) {
}

function registerHooks(pluginData) {
if (!pluginData.library) {
return;
}

const libraryPath = path.join(pluginData.path, pluginData.library);

try {
if (!Plugins.libraries[pluginData.id]) {
Plugins.requireLibrary(pluginData.id, libraryPath);
Plugins.requireLibrary(pluginData);
}

if (Array.isArray(pluginData.hooks)) {
pluginData.hooks.forEach(hook => Plugins.registerHook(pluginData.id, hook));
}
} catch (err) {
winston.warn('[plugins] Unable to parse library for: ' + pluginData.id);
winston.warn('[plugins] Unable to load library for: ' + pluginData.id);
throw err;
}
}
Expand Down

0 comments on commit 017af63

Please sign in to comment.