forked from pushkin-consortium-deprecated/deprecated1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.js
81 lines (67 loc) · 2.15 KB
/
printer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict';
let _ = require('lodash');
module.exports = (expressApp, filename) => {
const COLUMN_WIDTH_TREE = 1;
const COLUMN_WIDTH_PATH = 1;
const TREE_INDENT_SIZE = 5;
let text = ['<html><body><table><tr><td>'];
function fillWithSpaces(str, len) {
str += `<td colspan="${len}">`;
while (str.length < len) {
str += ' ';
}
return str;
}
function brushIndentation(indentation) {
return indentation.replace(/─/g, ' ').replace(/├/g, '│').replace(/└/g, ' ');
}
function printRoutes(layer, indentation) {
let path = ' ';
if (layer.path) {
path += layer.path;
} else if (layer.route && layer.route.path) {
path += layer.route.path;
} else if (layer.regexp) {
if (layer.regexp.source === '^\\/?$') {
path += '/';
} else if (layer.regexp.source === '^\\/?(?=\\/|$)') {
path += '*';
} else {
path += `/${layer.regexp.source}/`;
}
}
let methods = [];
if (layer.method) {
methods.push(layer.method);
} else if (layer.route) {
if (layer.route.methods) {
methods = _.keys(layer.route.methods);
} else if (layer.route.method) {
methods.push(layer.route.method);
}
}
methods = methods.join(', ').toUpperCase();
text.push(
`${fillWithSpaces(indentation + layer.name, COLUMN_WIDTH_TREE)}${fillWithSpaces(path, COLUMN_WIDTH_PATH)} ${methods}`
);
if (!layer.stack && !(layer.route && layer.route.stack)) {
if (layer.handle.stack) {
return printRoutes(layer.handle, brushIndentation(indentation));
}
return;
}
indentation = `${brushIndentation(indentation)} ├── `;
let stack = layer.stack || layer.route.stack;
for (let i = 0; i < stack.length; i += 1) {
if (i === stack.length - 1) {
indentation = `${indentation.substr(0, indentation.length - 1)} └── `;
}
printRoutes(stack[i], indentation);
}
text.push(indentation.substr(0, indentation.length - TREE_INDENT_SIZE));
}
printRoutes(expressApp._router, '');
text = text.join('</td></tr><tr><td>');
text += '</td></tr></table>';
return text;
};