frock is primarily a CLI utility, but it can be used programatically:
$ npm install frock
// index.js
var fs = require('fs')
var createFrock = require('frock')
var config = fs.readFileSync('path/to/frockfile.json')
var frock = createFrock(config, {pwd: process.cwd()})
frock.run(function () {
console.log('started!')
})
Instantiates a new frock.
config
is a configuration object, in the frockfile formatopts
is an object with the following properties:pwd
the current working directory, where frock will perform all of its resolution when loading plugins and files.
Returns a frock
, which is an EventEmitter
with a number of additional
methods and properties; these are split into a few different categories by their
functions:
These publicly available methods allow you to control the state of the frock
instance:
.run([ready])
Starts the mocks.ready
(function) optional callback to execute after all servers are started
.reload([config] [, ready])
Stop and restart all servers, optionally loading a new config.config
(object) an optional new config to loadready
(function) optional callback to execute after all servers are restarted
.stop([ready])
Shuts down all servers and handlers.ready
(function) optional callback to execute after all servers are ended
Factories allow you to create new objects/methods using frock's internal
libraries, ensuring that your plugin uses the same version as the core frock
:
.router
A router factory, which returns a new router instance. Internallyfrock
usescommuter
as its router, and there are benefits to you doing the same (see the section on tips for writing plugins).logger
A logger factory; in general you'll use the logger you're passed, but you might want access to the core logger, say to listen to events;frock
usesbole
as its logger, and this gives you access to its singleton.
Registries are Map
objects storing external dependencies, and allowing you to
register new dependencies:
.dbs
AMap
containing the key/value databases asname/levelDb
; with some additional methods for registering databases; this is an alternate allowed from within your plugin, rather than using thedb
config parameter which automatically creates a database for you:.dbs.register(name)
Given a stringname
create or get a database with that name
.handlers
AMap
containing the key/value handlers asname/handlerFunction
; also has an additional method not typically found on aMap
:.handlers.register(name)
Given a requirable string/pathname
this will require and save a handler; anything you pass asname
will be resolved using node'srequire
resolution process, required, and saved to theMap
.pwd
- The working directory (where thefrockfile.json
lives).version
The semver of the currently running frock
run
: emits when thefrock
instance is running, after all services have been startedreload
: emits after thefrock
instance has successfully shut down all services and restarted themstop
: emits just before thefrock
instance shuts down (and the process exits)