Plugify
class
new Plugify()
This class determines whether the
plugins are properly shipped and
sets them for the core.
class Plugify {
constructor(plugins, options, defaults) {
log.debug(`class: ${Plugify.name}`, `process: constructor`);
// Local options
this.options = {};
this.defaults = {};
// Initialize the plugins
this.plugins = (() => {
// Check if defaults is empty
if (_.isEmpty(defaults)) {
// Fill the defaults with placeholders (functions that don't do anything)
_.forEach(['api', 'backend', 'parser', 'header', 'localize', 'router'], item => {
this.defaults[Plugify.normalize(item)] = () => {};
});
} else Plugify.unPack(defaults, (key, plugin) => {
// Unpack the gengo-pack and generate the default plugins
Plugify.setPlugin(this.defaults, plugin, this.options);
});
return this.defaults;
})();
// Register the plugins
this.register(plugins);
// Debug
_.forOwn(this.plugins, (value, key) => {
var name = value.package ? value.package.name : '';
log.info(
`class: ${Plugify.name}`,
`plugins: name - ${name}, type - ${key}, typeof - ${typeof value}`);
});
// Create the default options
_.defaultsDeep(options, this.options);
}
register
method
Plugify.prototype.register()
Option name | Type | Description |
---|---|---|
The | Function, Array, Object | plugin to register |
Registers the plugin
register(plugins) {
log.debug(`class: ${Plugify.name}`, `process: register`);
var process = (plugin) => {
if (_.isPlainObject(plugin)) {
if (Plugify.isPack(plugin)) {
Plugify.unPack(plugin, (key, p) => {
if (Plugify.assert(p)) {
Plugify.setPlugin(this.plugins, p, this.options);
}
});
} else
if (Plugify.assert(plugin)) {
Plugify.setPlugin(this.plugins, plugin, this.options);
}
}
};
if (_.isArray(plugins)) {
_.forEach(plugins, plugin => {
plugin = _.isFunction(plugin) ? plugin() :
_.isPlainObject(plugin) ? plugin : undefined;
process(plugin);
});
} else if (_.isFunction(plugins)) {
process(plugins());
} else if (_.isPlainObject(plugins)) process(plugins);
}
static isPack(plugin) {
return !_.has(plugin, 'main') && (() => {
return _.forEach(Object.keys(plugin), key =>
key === 'api' || key === 'parser' || key === 'backend' ||
key === 'header' || key === 'localize' || key === 'router');
})();
}
unPack
method
Plugify.unPack()
Option name | Type | Description |
---|---|---|
plugins | Object, Function | The plugins to unpack. |
callback | Function | The callback function |
Unpacks the gengo-pack and returns the plugin
through a callback
static unPack(plugins, callback) {
_.forOwn(plugins, (plugin, type) => {
callback(type, _.isFunction(plugin) ? plugin() : plugin);
});
}
setPlugin
method
Plugify.setPlugin()
Option name | Type | Description |
---|---|---|
object | Object | The object to set its attributes. |
plugin | Object | The plugin to apply to the object |
options | Object | The options to apply |
Sets the attributes of the plugin
static setPlugin(object, plugin, options) {
log.debug(`class: ${Plugify.name}`, `process: setPlugin`);
var {
main, defaults
} = plugin;
var {
type
} = plugin.package;
type = Plugify.normalize(type);
if (object[type]) object[type] = {};
// Set the plugin fn
object[type] = main;
// Set the package
object[type].package = plugin.package;
// Set the default options
if (!options[type])
options[type] = defaults;
}
plugify
function
plugify()
Option name | Type | Description |
---|---|---|
plugins | Object, Function, Array | The user plugins or plugins to override the default |
options | Object | The options to apply to the plugins |
defaults | Object | The default plugins |
return | Plugify | An instance of Plugify |
Returns the plugins after creating an instance
of Plugify
function plugify(plugins = {}, options = {}, defaults = {}) {
'use strict';
return new Plugify(plugins, options, defaults).plugins;
}
export default plugify;