Package Exports
- nxus-core
- nxus-core/lib/ConfigurationManager
- nxus-core/lib/test/support/TestApp
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nxus-core) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@nxus/core
_
The Nxus Core package includes the basic Application framework for building a Nxus app.
Introduction
You'll probably find the following resources useful background and help in building Nxus applcations.
- Getting Started (TODO)
- Design Patterns (TODO)
- Nxus Modules (TODO)
- Recipes (TODO)
- Developing a (TODO)
Documentation
The full set of Nxus docs is available at http://docs.gonxus.org.
Installation
> npm install @nxus/core --saveUsage
In your root application, create a new Application instance:
import {Application} from '@nxus/core'
let app = new Application(options)
app.start()
export default appEvents
Nxus is built around the concept of a boot cycle. The application dispatches events in the following order:
init: indicates the application is starting up and initializing modules. Other modules are not gauranteed to be available at this phase.load: modules are initialized and loading. This is the place to do any internal setup (outside of the constructor). Other modules are not gauranteed to be available at this phase.startup: all modules have been loaded and are available. This is the place to do any setup that requires data/input from other modules (like Storage).launch: the application is launching and all services have been started. Routes are accessible. Use onceAfter('launch') to gaurantee execution after the application has completely launched.
Module Loading
By defaul the Application will look for other Nxus modules in the following order:
- @nxus namespaced npm modules in your
package.jsonfile. - Any packages that match the 'namespace-' pattern passed in the
namespaceapplication config option. - folders in the ./modules folder in the root of your project
- any modules specified in the modules option passed into Application on instantiation.
Module Access
In order to access module commands, use the Application.get() method.
let router = Application.get('router')Application Configuration Options
Available options are:
appDir: the location to use to load the default 'package.json' file.
namespace: any additional namespaces to use to load modules in the node_modules folder. Can be a string or array of strings.
modules: an array of paths to require into the application
debug: Boolean to display debug messages, including startup banner
script: Boolean to indicate the application is a CLI script, silences all logging/output messages except for explicit console.log calls
API
Application
Extends Dispatcher
The Core Application class.
Parameters
optsObject the configuration options
Examples
import {Application} from '@nxus/core'
let app = new Application(options)
app.start()
export default appboot
Boots the application, cycling through the internal boot stages.
Note: Should rarely be called directly. Instead use #start
Returns Promise
get
Returns an internal Module object for the given name.
Parameters
namestring The name of the module to return
Returns Module
init
Initializes the application by loading plugins, then booting the application.
Note: this should rarely be called directly. Instead use #start
Returns Promise
restart
Restarts the Nxus application.
Returns Promise
start
Starts the Nxus application.
Returns Promise
stop
Stops the currently running application, removing all event listeners.
Returns Promise
PluginManager
The PluginManager handles all of the module loading. Load order is as follows:
- Packages in node_modules that match the passed
namespaceconfig option, and packages in the@nxusnamespace. - Folders in the
/modules directory. - Filepaths passed in the
modulesconfig option
accumulatePackage
Creates the internal representation of a package
Parameters
packagesarray the list of active packages being loaded by Nxusdirectorystring the directory of the new package to load
Returns object the package, as returned by require
arrayify
Helper method to ensure a passed variable is an array. Wraps the value in an array if it isn't already.
Parameters
elanything the item to ensure is an array
Returns Array either a new empty array, or el as is if its an array, or el wrapped in an array.
getDeps
Loads the dependencies for a particular package, as defined by the 'dependencies' object in the package.json file.
Parameters
pkgobject the package object generated by accumulatePackage()
Returns array an array of the package's dependencies, as filepaths or node_module names
getPluginPackageJson
Loads the package.json file for the specified packages.
Parameters
pathstring the root package folder path
Returns object the parsed json object in package.json
loadCustomPlugins
Loads custom plugins in the
Parameters
optionsobject configuration optionspackagespackages the array of packages currently loaded by Nxus
loadPackage
Loads a package
Parameters
namestring The name of the packagedirectorystring A path to the packagepackagesarray An array of the currently loaded packages
loadPackages
Loads all Nxus pacakges for an application
Parameters
optionsobject options to use to load the packagespackagespackages the array of packages currently loaded by Nxus
loadPassedPlugins
Loads manually passed in packages by path
Parameters
optionsobject configuration optionspackagespackages the array of packages currently loaded by Nxus
Dispatcher
Extends EventEmitter
The core Dispatcher class, which implements promisified
Examples
import { Dispatcher } from '@nxus/core'
class MyClass extends Dispatcher {
...
}after
Bind to after an event. Receives the event handlers results, should return modified results or nothing.
Parameters
eventstring The name of the event to bind tolistenercallable The after handler for the event
before
Bind to before an event. Receives the event arguments, should return modified arguments or nothing.
Parameters
eventstring The name of the event to bind tolistenercallable The before handler for the event
emit
Emits an event, calling all registered handlers.
Parameters
eventstring The name of the event to emit.args...Any Arguments to the event handlers
Returns Promise Returns a promise that resolves when all handlers have completed, with any returned results as an array.
once
Bind to an event once
Parameters
eventstring The name of the event to bind tolistenercallable= The handler for the event
Returns Promise Returns a promise that resolves when the event fires
onceAfter
Bind once to after an event. Receives the event handlers results, should return modified results or nothing.
Parameters
eventstring The name of the event to bind tolistenercallable The after handler for the event
Returns Promise Returns a promise that resolves when the event fires
onceBefore
Bind once to before an event. Receives the event arguments, should return modified arguments or nothing.
Parameters
eventstring The name of the event to bind tolistenercallable The before handler for the event
Returns Promise Returns a promise that resolves when the event fires
ConfigurationManager
ConfigurationManager loads the internal app.config hash using the following order (each overwrites any values of the previous):
- Opts loaded into the application object.
- Opts in the
confighash of the project package.json file - Any environment variables
getConfig
Returns the final config option using the loading order described above.
Returns object the final composed configuration object.
getEnvironmentVariables
Extracts the currently avaiable environment variables
Returns object A hash of the current environment variables
getNodeEnv
Returns the current NODE_ENV
Returns string the current NODE_ENV
getPackageJSONConfig
Gets the local package.json file and tries to find an internal config key
Returns object the intenral config object or an empty object if it isn't defined.
Module
Extends Dispatcher
The core Module class. This provides a messaging proxy layer between modules and calling code. The main advantage of this proxy class is that missing modules won't cause exceptions in the code.
Modules are accessed through the Application.get() method
Examples
let router = app.get('router')
Producer modules should register themselves with the use() method, and define gather() and respond() handlers:app.get('router').use(this).gather('route')app.get('templater').use(this).respond('template')
Consumer modules should get the module they need to use and call provide or requestapp.get('router').provide('route', ...)app.get('templater').request('render', ...)
Modules proxy event names as methods to provide/request, so these are synomymous with above:app.get('router').route(...)app.get('templater').render(...)
Default implementations should be indicated by using default() to occur before provide()
Overriding another implementation can use replace() to occur after provide()app.get('router').default('route', GET', '/', ...)app.get('router').replace('route', GET', '/', ...)
Provide, default, and replace all return a proxy object if called with no arguments, so these are synonymous with above:app.get('router').default().route('GET', '/', ...)app.get('router').replace().route('GET', '/', ...)default
Provide default arguments to a delayed gather() call, before other provides
Parameters
namestring The name of the gather eventargs...Any Arguments to provide to the gather event
Returns Promise Resolves when the event is eventually handled
gather
Receive arguments provided to a delayed gather() call.
Parameters
namestring The name of the gather eventhandlercallable The handler for each provided value
provide
Provide arguments to a delayed gather() call.
Parameters
namestring The name of the gather eventargs...Any Arguments to provide to the gather event
Returns Promise Resolves when the event is eventually handled
replace
Provide a replacement for a delayed gather() call (after others are provided)
Parameters
namestring The name of the gather eventargs...Any Arguments to provide to the gather event
Returns Promise Resolves when the event is eventually handled
request
Request the result of processing a named event
Parameters
namestring The name of the request eventargs...Any Arguments to provide to the responder
Returns Promise Resolves to the result of the event's handler
respond
Respond to a named event
Parameters
namestring The name of the request eventhandlercallable The handler for the request
use
Let another instance use this module's events to reduce boilerplate calls
Parameters
instance
Watcher
The Watcher class monitors the project directory and restarts the application whenever there is a change in files detected. Useful for development.