file-routing-expressjs
Version:
A dependency-free, flexible, system-based file routing for Express.
134 lines • 4.55 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var cli_exports = {};
__export(cli_exports, {
flattenRoutes: () => flattenRoutes,
printTable: () => printTable,
printTree: () => printTree
});
module.exports = __toCommonJS(cli_exports);
var import_logging = require("./logging");
function methodColor(method) {
switch (method.toLowerCase()) {
case "get":
return "green";
case "post":
return "blue";
case "put":
return "yellow";
case "delete":
return "red";
case "patch":
return "magenta";
default:
return "cyan";
}
}
function pad(str, len) {
return str + " ".repeat(Math.max(0, len - str.length));
}
function flattenRoutes(nodes) {
const result = [];
function walk(node) {
var _a;
if (node.method !== "-") {
result.push(node);
}
if ((_a = node.children) == null ? void 0 : _a.length) {
node.children.forEach(walk);
}
}
nodes.forEach(walk);
return result;
}
function printTree(nodes, indent = "") {
var _a, _b, _c, _d;
for (const node of nodes) {
const isGroup = node.method === "-";
const method = (_a = node.method) == null ? void 0 : _a.toUpperCase();
const methodColored = method ? (0, import_logging.color)(`[${method}]`, methodColor(method)) : "";
const label = isGroup ? (0, import_logging.color)(node.endpoint, "bold") : `${methodColored} ${node.endpoint}`;
console.log(`${indent}${label}`);
if (!isGroup) {
if ((_b = node.middlewares) == null ? void 0 : _b.length) {
console.log(
`${indent} ${(0, import_logging.color)("\u251C\u2500", "gray")} ${(0, import_logging.color)("middlewares:", "cyan")} ${node.middlewares.join(", ")}`
);
}
if ((_c = node.plugins) == null ? void 0 : _c.length) {
console.log(
`${indent} ${(0, import_logging.color)("\u251C\u2500", "gray")} ${(0, import_logging.color)("plugins:", "magenta")} ${node.plugins.join(", ")}`
);
}
if (node.errorHandler && node.errorHandler !== "-") {
console.log(
`${indent} ${(0, import_logging.color)("\u2514\u2500", "gray")} ${(0, import_logging.color)("error:", "red")} ${node.errorHandler}`
);
}
}
if ((_d = node.children) == null ? void 0 : _d.length) {
printTree(node.children, indent + " ");
}
}
}
function printTable(endpoints) {
const headers = ["Method", "URI", "Name", "Middleware", "Plugins", "Error"];
const widths = headers.map((h) => h.length);
const rows = flattenRoutes(endpoints);
for (const row of rows) {
const values = [
row.method,
row.endpoint,
row.name,
row.middlewares.join(", ") || "-",
row.plugins.join(", ") || "-",
row.errorHandler
];
values.forEach((v, i) => {
widths[i] = Math.max(widths[i], String(v).length);
});
}
const line = (char) => "+" + widths.map((w) => char.repeat(w + 2)).join("+") + "+";
const printRow = (cols, colorize = false) => {
const row = cols.map((c, i) => pad(c, widths[i])).join(" | ");
const content = `| ${row} |`;
return colorize ? (0, import_logging.color)(content, "green") : content;
};
console.log((0, import_logging.color)(line("-"), "green"));
console.log(printRow(headers, true));
console.log((0, import_logging.color)(line("-"), "green"));
for (const row of rows) {
console.log(printRow([
row.method,
row.endpoint,
row.name,
row.middlewares.join(", ") || "-",
row.plugins.join(", ") || "-",
row.errorHandler
], true));
}
console.log((0, import_logging.color)(line("-"), "green"));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
flattenRoutes,
printTable,
printTree
});
//# sourceMappingURL=cli.js.map