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.
configis a configuration object, in the frockfile formatoptsis an object with the following properties:pwdthe 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:
.routerA router factory, which returns a new router instance. Internallyfrockusescommuteras its router, and there are benefits to you doing the same (see the section on tips for writing plugins).loggerA 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;frockusesboleas its logger, and this gives you access to its singleton.
Registries are Map objects storing external dependencies, and allowing you to
register new dependencies:
.dbsAMapcontaining 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 thedbconfig parameter which automatically creates a database for you:.dbs.register(name)Given a stringnamecreate or get a database with that name
.handlersAMapcontaining the key/value handlers asname/handlerFunction; also has an additional method not typically found on aMap:.handlers.register(name)Given a requirable string/pathnamethis will require and save a handler; anything you pass asnamewill be resolved using node'srequireresolution process, required, and saved to theMap
.pwd- The working directory (where thefrockfile.jsonlives).versionThe semver of the currently running frock
run: emits when thefrockinstance is running, after all services have been startedreload: emits after thefrockinstance has successfully shut down all services and restarted themstop: emits just before thefrockinstance shuts down (and the process exits)