1var util = require('util'),2director = require('../director');34var Router = exports.Router = function (routes) {5director.Router.call(this, routes);6this.recurse = 'backward';7};89//10// Inherit from `director.Router`.11//12util.inherits(Router, director.Router);1314//15// ### function configure (options)16// #### @options {Object} **Optional** Options to configure this instance with17// Configures this instance with the specified `options`.18//19Router.prototype.configure = function (options) {20options = options || {};21director.Router.prototype.configure.call(this, options);2223//24// Delimiter must always be `\s` in CLI routing.25// e.g. `jitsu users create`26//27this.delimiter = '\\s';28return this;29};3031//32// ### function dispatch (method, path)33// #### @method {string} Method to dispatch34// #### @path {string} Path to dispatch35// Finds a set of functions on the traversal towards36// `method` and `path` in the core routing table then37// invokes them based on settings in this instance.38//39Router.prototype.dispatch = function (method, path, tty, callback) {40//41// Prepend a single space onto the path so that the traversal42// algorithm will recognize it. This is because we always assume43// that the `path` begins with `this.delimiter`.44//45path = ' ' + path;46var fns = this.traverse(method, path, this.routes, '');47if (!fns || fns.length === 0) {48if (typeof this.notfound === 'function') {49this.notfound.call({ tty: tty, cmd: path }, callback);50}51else if (callback) {52callback(new Error('Could not find path: ' + path));53}5455return false;56}5758if (this.recurse === 'forward') {59fns = fns.reverse();60}6162this.invoke(this.runlist(fns), { tty: tty, cmd: path }, callback);63return true;64};656667