-
-
Save bmatusiak/2575a5edd80bf297c511 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var fs = require("fs"); | |
module.exports = function(pluginName,pluginDir,init){ | |
return function(options, imports, register) { | |
var pluginObject = imports; | |
var registerObject = {}; | |
registerObject[pluginName] = pluginObject; | |
var Architect = require("architect"); | |
if(!options.plugins){ | |
options.plugins = []; | |
var dirList = fs.readdirSync(pluginDir); | |
for(var i = 0; dirList.length >= i;i++){ | |
if(dirList[i]){ | |
if(options.pluginOptions && options.pluginOptions[dirList[i]]){ | |
options.pluginOptions[dirList[i]].packagePath = pluginDir+"/"+dirList[i]; | |
options.plugins.push(options.pluginOptions[dirList[i]]); | |
}else{ | |
options.plugins.push(pluginDir+"/"+dirList[i]); | |
} | |
} | |
} | |
} | |
var ArchitectConfig = Architect.resolveConfig(options.plugins, pluginDir); | |
ArchitectConfig.push({ | |
packagePath:__dirname, | |
consumes:["hub"], | |
provides:[pluginName], | |
setup:function($options, $imports, $register){ | |
$imports.hub.on("service", function(name, plugin) { | |
if(!plugin.name)plugin.name = name; | |
registerObject[pluginName][plugin.name] = plugin; | |
//console.log("Service loaded " + name); | |
}); | |
$imports.hub.on("ready", function(app) { | |
var plugins = app.services; | |
init(registerObject[pluginName], plugins); | |
register(null, registerObject); | |
console.log(pluginName,"Loaded!"); | |
}); | |
$register(null, registerObject); | |
} | |
}); | |
Architect.createApp(ArchitectConfig, function(err, architect) { | |
if (err) { | |
console.log(err); | |
register(err); | |
} | |
}); | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var setup = require("./app-setup.js"); | |
"use strict"; | |
// package.json | |
/* | |
{ | |
"name": "myPluginName", | |
"version": "0.0.1", | |
"main": "./useage.js", | |
"plugin": { | |
"provides": [ | |
"myPlugin" | |
], | |
"consumes": [ | |
"AnotherPlugin" | |
] | |
} | |
} | |
*/ | |
module.exports = function(options, imports, register) { | |
setup("myPlugin",__dirname+"/plugins",function(myPlugin,subPlugins){ | |
// imports object becomes myPlugin object | |
// ex.. imports.AnotherPlugin === myPlugin.AnotherPlugin | |
// all sub plugins must consumes "myPlugin" | |
// all sub plugins in __dirname+"/plugins" are loaded | |
for(var i in subPlugins){ | |
if(subPlugins[i].init)subPlugins[i].init(); | |
} | |
})(options, imports, register); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment