Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50663 views
1
2
var util = require('util'),
3
director = require('../director');
4
5
var Router = exports.Router = function (routes) {
6
director.Router.call(this, routes);
7
this.recurse = 'backward';
8
};
9
10
//
11
// Inherit from `director.Router`.
12
//
13
util.inherits(Router, director.Router);
14
15
//
16
// ### function configure (options)
17
// #### @options {Object} **Optional** Options to configure this instance with
18
// Configures this instance with the specified `options`.
19
//
20
Router.prototype.configure = function (options) {
21
options = options || {};
22
director.Router.prototype.configure.call(this, options);
23
24
//
25
// Delimiter must always be `\s` in CLI routing.
26
// e.g. `jitsu users create`
27
//
28
this.delimiter = '\\s';
29
return this;
30
};
31
32
//
33
// ### function dispatch (method, path)
34
// #### @method {string} Method to dispatch
35
// #### @path {string} Path to dispatch
36
// Finds a set of functions on the traversal towards
37
// `method` and `path` in the core routing table then
38
// invokes them based on settings in this instance.
39
//
40
Router.prototype.dispatch = function (method, path, tty, callback) {
41
//
42
// Prepend a single space onto the path so that the traversal
43
// algorithm will recognize it. This is because we always assume
44
// that the `path` begins with `this.delimiter`.
45
//
46
path = ' ' + path;
47
var fns = this.traverse(method, path, this.routes, '');
48
if (!fns || fns.length === 0) {
49
if (typeof this.notfound === 'function') {
50
this.notfound.call({ tty: tty, cmd: path }, callback);
51
}
52
else if (callback) {
53
callback(new Error('Could not find path: ' + path));
54
}
55
56
return false;
57
}
58
59
if (this.recurse === 'forward') {
60
fns = fns.reverse();
61
}
62
63
this.invoke(this.runlist(fns), { tty: tty, cmd: path }, callback);
64
return true;
65
};
66
67