Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Loadconfig accept array #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,22 @@ The architect module exposes two functions as it's main API.

This function starts an architect config. The return value is an `Architect` instance. The optional callback will listen for both "error" and "ready" on the app object and report on which one happens first.

### loadConfig(configPath)
### loadConfig(configPathOrArray, [base], [callback])

This is a sync function that loads a config file and parses all the plugins into a proper config object for use with `createApp`. While this uses sync I/O all steps along the way are memoized and I/O only occurs on the first invocation. It's safe to call this in an event loop provided a small set of configPaths are used.
This is a sync function prepares the config object for each plugin.
While this uses sync I/O all steps along the way are memoized and I/O only occurs on the first invocation. It's safe to call this in an event loop provided a small set of configPaths are used.

It can be used in one of two ways:

#### loadConfig(configPath, [callback])

Loads a config file and parses all the plugins into a proper config object for use with `createApp`.
Base path of plugins is automatically detected from the base name of the path provided.

#### loadConfig(configArray, base, [callback])

Processes the existing config and adds the initialization function for each plugin present.
Note that base is required when using this method.

## Class: Architect

Expand All @@ -105,4 +118,3 @@ When a plugin registers, this event is emitted.
### Event: "ready" (app)

When all plugins are done, the "ready" event is emitted. The value is the Architect instance itself.

44 changes: 28 additions & 16 deletions architect.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ if (typeof module === "object") (function () {
// This is assumed to be used at startup and uses sync I/O as well as can
// throw exceptions. It loads and parses a config file.
function loadConfig(configPath, callback) {
var config = require(configPath);
var base = dirname(configPath);
var config;
var base;
if ('string' === typeof configPath) {
config = require(configPath);
base = dirname(configPath);
} else if ('object' === typeof configPath) {
config = configPath;
if ('string' === typeof callback) {
base = callback;
callback = arguments[2];
}
} else {
throw new Error('Unexpected type for configPath (expected: string path -or- array, base path');
}

return resolveConfig(config, base, callback);
}
Expand Down Expand Up @@ -153,7 +165,7 @@ if (typeof module === "object") (function () {
}
else if (packagePath) {
next(null, dirname(packagePath));
}
}
else {
resolvePackage(base, modulePath, next);
}
Expand Down Expand Up @@ -289,7 +301,7 @@ else (function () {
function resolveConfig(config, base, callback, errback) {
if (typeof base == "function")
return resolveConfig(config, "", arguments[1], arguments[2]);

var paths = [], pluginIndexes = {};
config.forEach(function (plugin, index) {
// Shortcut where string is used for plugin without any options.
Expand Down Expand Up @@ -400,7 +412,7 @@ function checkCycles(config, lookup) {
unresolved[name] = false;
});
});

Object.keys(unresolved).forEach(function(name) {
if (unresolved[name] === false)
delete unresolved[name];
Expand All @@ -425,7 +437,7 @@ function Architect(config) {
app.config = config;
app.packages = {};
app.pluginToPackage = {};

var isAdditionalMode;
var services = app.services = {
hub: {
Expand Down Expand Up @@ -453,15 +465,15 @@ function Architect(config) {
imports[name] = services[name];
});
}

var m = /^plugins\/([^\/]+)|\/plugins\/[^\/]+\/([^\/]+)/.exec(plugin.packagePath);
var packageName = m && (m[1] || m[2]);
if (!app.packages[packageName]) app.packages[packageName] = [];

if (DEBUG) {
recur++;
plugin.setup(plugin, imports, register);

while (callnext && recur <= 1) {
callnext = false;
startPlugins(additional);
Expand All @@ -484,7 +496,7 @@ function Architect(config) {
recur--;
}
}

function register(err, provided) {
if (err) { return app.emit("error", err); }
plugin.provides.forEach(function (name) {
Expand All @@ -501,14 +513,14 @@ function Architect(config) {
isAdditionalMode: isAdditionalMode
};
app.packages[packageName].push(name);

app.emit("service", name, services[name], plugin);
});
if (provided && provided.hasOwnProperty("onDestroy"))
destructors.push(provided.onDestroy);

app.emit("plugin", plugin);

if (recur) return (callnext = true);
startPlugins(additional);
}
Expand All @@ -519,19 +531,19 @@ function Architect(config) {

this.loadAdditionalPlugins = function(additionalConfig, callback){
isAdditionalMode = true;

exports.resolveConfig(additionalConfig, function (err, additionalConfig) {
if (err) return callback(err);

app.once(ready ? "ready-additional" : "ready", function(app){
callback(null, app);
}); // What about error state?

// Check the config - hopefully this works
var _sortedPlugins = checkConfig(additionalConfig, function(name){
return services[name];
});

if (ready) {
sortedPlugins = _sortedPlugins;
// Start Loading additional plugins
Expand Down