Skip to content

Node.js based forum software built for the modern web

refactor: show a louder deprecation notice, alert once for each hook, not per plugin per hook

Julian Lam93b80f170bc7
diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js
index 04603b486d..db65852978 100644
--- a/src/plugins/hooks.js
+++ b/src/plugins/hooks.js
@@ -7,10 +7,20 @@ const utils = require('../utils');
const Hooks = module.exports;
-Hooks.deprecatedHooks = {
- 'filter:email.send': 'static:email.send', // πŸ‘‹ @ 1.19.0
- 'filter:router.page': 'response:router.page', // πŸ‘‹ @ 2.0.0
-};
+Hooks._deprecated = new Map([
+ ['filter:email.send', {
+ new: 'static:email.send',
+ since: 'v1.17.0',
+ until: 'v1.19.0',
+ affected: new Set(),
+ }],
+ ['filter:router.page', {
+ new: 'response:router.page',
+ since: 'v1.15.3',
+ until: 'v2.1.0',
+ affected: new Set(),
+ }],
+]);
Hooks.internals = {
_register: function (data) {
@@ -39,14 +49,10 @@ Hooks.register = function (id, data) {
}
// `hasOwnProperty` needed for hooks with no alternative (set to null)
- if (Hooks.deprecatedHooks.hasOwnProperty(data.hook)) {
- const deprecated = Hooks.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.`);
- }
+ if (Hooks._deprecated.has(data.hook)) {
+ const deprecation = Hooks._deprecated.get(data.hook);
+ deprecation.affected.add(id);
+ Hooks._deprecated.set(data.hook, deprecation);
}
data.id = id;
diff --git a/src/plugins/index.js b/src/plugins/index.js
index 38110c1fe3..a84eb40ddc 100644
--- a/src/plugins/index.js
+++ b/src/plugins/index.js
@@ -127,6 +127,17 @@ Plugins.reload = async function () {
posts.registerHooks();
meta.configs.registerHooks();
+ // Deprecation notices
+ Plugins.hooks._deprecated.forEach((deprecation, hook) => {
+ if (!deprecation.affected.size) {
+ return;
+ }
+
+ const replacement = deprecation.hasOwnProperty('new') ? `Please use ${chalk.yellow(deprecation.new)} instead.` : 'There is no alternative.';
+ winston.warn(`[plugins/load] ${chalk.white.bgRed.bold('DEPRECATION')} The hook ${chalk.yellow(hook)} has been deprecated as of ${deprecation.since}, and slated for removal in ${deprecation.until}. ${replacement} The following plugins are still listening for this hook:`);
+ deprecation.affected.forEach(id => console.log(` ${chalk.yellow('*')} ${id}`));
+ });
+
// Lower priority runs earlier
Object.keys(Plugins.loadedHooks).forEach((hook) => {
Plugins.loadedHooks[hook].sort((a, b) => a.priority - b.priority);