Add dictionary hook to load plugin's files

This commit is contained in:
Aurélien Georget 2016-09-01 17:27:30 +02:00
parent 4ab69de4be
commit 4a552298e0
5 changed files with 166 additions and 6 deletions

View File

@ -8,6 +8,7 @@ module.exports = {
dictionary: {
_config: true,
_api: true,
_plugins: true,
_externalHooks: true
},
core: {

View File

@ -133,11 +133,7 @@ module.exports = strapi => {
};
// Delete the definition if it's empty.
_.forEach(strapi.api[api.name], (dictionary, entry) => {
if (_.isEmpty(strapi.api[api.name][entry])) {
delete strapi.api[api.name][entry];
}
});
strapi.api[api.name] = _.omitBy(strapi.api[api.name], _.isEmpty);
// If the module doesn't have a definition at all
// just remove it completely from the dictionary.

View File

@ -86,6 +86,16 @@ module.exports = strapi => {
filter: /(.+)\.(js|json)$/,
depth: 4
}, cb);
},
// Load plugins from `./plugins/**/*.js|json`.
'plugins/**': cb => {
dictionary.optional({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.plugins),
excludeDirs: /(public)$/,
filter: /(.+)\.(js|json)$/,
depth: 4
}, cb);
}
},
@ -116,6 +126,9 @@ module.exports = strapi => {
// Expose user APIs.
strapi.api = config['api/**'];
// Expose user plugins.
strapi.plugins = config['plugins/**'];
// Add user locales for the settings of the `i18n` hook
// aiming to load locales automatically.
if (_.isPlainObject(strapi.config.i18n) && !_.isEmpty(strapi.config.i18n)) {

View File

@ -0,0 +1,149 @@
'use strict';
/**
* Module dependencies
*/
// Node.js core.
const path = require('path');
// Public node modules.
const _ = require('lodash');
const async = require('async');
// Strapi utilities.
const dictionary = require('strapi-utils').dictionary;
/**
* Async module loader to create a
* dictionary of the user plugins.
*/
module.exports = strapi => {
return {
/**
* Initialize the hook
*/
initialize: cb => {
_.forEach(strapi.plugins, (definition, plugin) => {
async.auto({
// Expose the `name` of the plugin for the callback.
'name': cb => {
cb(null, plugin);
},
// Load API controllers from `./plugins/*/controllers/*.js`.
'controllers/*': cb => {
dictionary.optional({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.plugins, plugin, strapi.config.paths.controllers),
filter: /(.+)\.(js)$/,
depth: 1
}, cb);
},
// Load API models from `./plugins/*/models/*.js` and `./plugins/*/models/*.settings.json`.
'models/*': cb => {
async.parallel({
settings: cb => {
dictionary.optional({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.plugins, plugin, strapi.config.paths.models),
filter: /(.+)\.settings.json$/,
depth: 1
}, cb);
},
functions: cb => {
dictionary.optional({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.plugins, plugin, strapi.config.paths.models),
filter: /(.+)\.js$/,
depth: 1
}, cb);
}
}, (err, models) => {
if (err) {
return cb(err);
}
return cb(null, _.merge(models.settings, models.functions));
});
},
// Load API services from `./plugins/*/services/*.js`.
'services/*': cb => {
dictionary.optional({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.plugins, plugin, strapi.config.paths.services),
filter: /(.+)\.(js)$/,
depth: 1
}, cb);
},
// Load API policies from `./plugins/*/policies/*.js`.
'policies/*': cb => {
dictionary.aggregate({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.plugins, plugin, strapi.config.paths.policies),
filter: /(.+)\.(js)$/,
depth: 1
}, cb);
},
// Load API config from `./plugins/*/config/*.js|json` and `./plugins/*/config/environments/**/*.js|json`.
'config/**': cb => {
async.parallel({
common: cb => {
dictionary.aggregate({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.plugins, plugin, strapi.config.paths.config),
filter: /(.+)\.(js|json)$/,
depth: 2
}, cb);
},
specific: cb => {
dictionary.aggregate({
dirname: path.resolve(strapi.config.appPath, strapi.config.paths.plugins, plugin, strapi.config.paths.config, 'environments', strapi.config.environment),
filter: /(.+)\.(js|json)$/,
depth: 2
}, cb);
}
}, (err, config) => {
if (err) {
return cb(err);
}
return cb(null, _.merge(config.common, config.specific));
});
}
},
// Callback.
(err, plugin) => {
// Just in case there is an error.
if (err) {
return cb(err);
}
// Expose the API dictionary.
strapi.plugins[plugin.name] = {
controllers: plugin['controllers/*'],
models: plugin['models/*'],
services: plugin['services/*'],
policies: plugin['policies/*'],
config: plugin['config/**']
};
// Delete the definition if it's empty.
strapi.plugins[plugin.name] = _.omitBy(strapi.plugins[plugin.name], _.isEmpty);
// If the module doesn't have a definition at all
// just remove it completely from the dictionary.
if (_.isEmpty(strapi.plugins[plugin.name])) {
delete strapi.plugins[plugin.name];
}
});
});
cb();
}
};
};

View File

@ -76,7 +76,8 @@ module.exports = strapi => {
controllers: 'controllers',
services: 'services',
policies: 'policies',
models: 'models'
models: 'models',
plugins: 'plugins'
},
// Start off needed empty objects and strings.