mirror of
https://github.com/strapi/strapi.git
synced 2025-12-26 22:54:31 +00:00
Load external hooks
This commit is contained in:
parent
c45ea07a27
commit
3fa66a1aaf
81
lib/configuration/hooks/_hooks/index.js
Normal file
81
lib/configuration/hooks/_hooks/index.js
Normal file
@ -0,0 +1,81 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
// Node.js core.
|
||||
const path = require('path');
|
||||
|
||||
// Public node modules.
|
||||
const _ = require('lodash');
|
||||
const async = require('async');
|
||||
|
||||
// Local utilities.
|
||||
const dictionary = require('../../../../util/dictionary');
|
||||
|
||||
/**
|
||||
* Async module loader to create a
|
||||
* dictionary of the external hooks
|
||||
*/
|
||||
|
||||
module.exports = function (strapi) {
|
||||
const hook = {
|
||||
|
||||
/**
|
||||
* Initialize the hook
|
||||
*/
|
||||
|
||||
initialize: function (cb) {
|
||||
async.auto({
|
||||
|
||||
// Load external hooks from the `node_modules` directory.
|
||||
externalHooks: function (cb) {
|
||||
dictionary.optional({
|
||||
dirname: path.resolve(strapi.config.appPath, 'node_modules'),
|
||||
filter: /^(package\.json)$/,
|
||||
excludeDirs: /^\./,
|
||||
depth: 2
|
||||
}, cb);
|
||||
}
|
||||
},
|
||||
|
||||
// Callback.
|
||||
function (err, hooks) {
|
||||
|
||||
// Just in case there is an error.
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
// Add the loaded hooks into the hook dictionary exposed at `strapi.hooks`.
|
||||
_.extend(strapi.hooks, _.reduce(hooks.externalHooks, function (memo, module, identity) {
|
||||
if (module['package.json'] && module['package.json'].strapi && module['package.json'].strapi.isHook) {
|
||||
const hookName = identity.match(/^strapi-/) ? identity.replace(/^strapi-/, '') : identity;
|
||||
memo[hookName] = require(path.resolve(strapi.config.appPath, 'node_modules', identity));
|
||||
}
|
||||
return memo;
|
||||
}, {}));
|
||||
|
||||
cb();
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Reload the hook
|
||||
*/
|
||||
|
||||
reload: function () {
|
||||
hook.initialize(function (err) {
|
||||
if (err) {
|
||||
strapi.log.error('Failed to reinitialize the external hooks.');
|
||||
strapi.stop();
|
||||
} else {
|
||||
strapi.emit('hook:_hooks:reloaded');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return hook;
|
||||
};
|
||||
@ -7,6 +7,7 @@
|
||||
module.exports = {
|
||||
_config: true,
|
||||
_api: true,
|
||||
_hooks: true,
|
||||
responseTime: true,
|
||||
bodyParser: true,
|
||||
session: true,
|
||||
|
||||
@ -116,6 +116,16 @@ module.exports = function (strapi) {
|
||||
loadHook('_api', cb);
|
||||
},
|
||||
|
||||
// Load the external hooks.
|
||||
_hooks: function loadExternalHook(cb) {
|
||||
if (!hooks._hooks) {
|
||||
return cb();
|
||||
}
|
||||
prepareHook('_hooks');
|
||||
applyDefaults(hooks._hooks);
|
||||
loadHook('_hooks', cb);
|
||||
},
|
||||
|
||||
// Load the studio hook.
|
||||
studio: function loadStudioHook(cb) {
|
||||
if (!hooks.studio) {
|
||||
@ -128,7 +138,7 @@ module.exports = function (strapi) {
|
||||
|
||||
// Prepare all other hooks.
|
||||
prepare: function prepareHooks(cb) {
|
||||
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
|
||||
async.each(_.without(_.keys(hooks), '_config', '_api', '_hooks', 'studio', 'router'), function (id, cb) {
|
||||
prepareHook(id);
|
||||
process.nextTick(cb);
|
||||
}, cb);
|
||||
@ -136,7 +146,7 @@ module.exports = function (strapi) {
|
||||
|
||||
// Apply the default config for all other hooks.
|
||||
defaults: function defaultConfigHooks(cb) {
|
||||
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
|
||||
async.each(_.without(_.keys(hooks), '_config', '_api', '_hooks', 'studio', 'router'), function (id, cb) {
|
||||
const hook = hooks[id];
|
||||
applyDefaults(hook);
|
||||
process.nextTick(cb);
|
||||
@ -145,7 +155,7 @@ module.exports = function (strapi) {
|
||||
|
||||
// Load all other hooks.
|
||||
load: function loadOtherHooks(cb) {
|
||||
async.each(_.without(_.keys(hooks), '_config', '_api', 'studio', 'router'), function (id, cb) {
|
||||
async.each(_.without(_.keys(hooks), '_config', '_api', '_hooks', 'studio', 'router'), function (id, cb) {
|
||||
loadHook(id, cb);
|
||||
}, cb);
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user