UNPKG

2.92 kBJavaScriptView Raw
1/**
2
3 middleware-editor.js
4
5**/
6
7
8//
9// Dependencies
10//
11
12var chalk = require("chalk"),
13 utils = require("./cli-utils");
14
15
16
17//
18// Exports
19//
20
21module.exports = {
22
23 /**
24
25 Outputs a list of middleware.
26
27 **/
28
29 list: function() {
30
31 var editor = require("./config-editor"),
32 middleware = editor.get("middleware"),
33 columns = [];
34
35 for (var key in middleware)
36 columns.push([key, middleware[key].join(", ")]);
37
38 utils.showGrid(columns, {
39 title : "Middleware:",
40 len : 20,
41 headers : ["Position", "Packages"],
42 sort : false
43 });
44
45 },
46
47
48
49 /**
50
51 Adds custom middleware to the stack. Valid positions include: beforeAll,
52 beforeRoutes, afterRoutes and errorHandler
53
54 @param {String} position The middleware position
55 @param {String} pkg The name of the package
56
57 **/
58
59 add: function(position, pkg) {
60
61 var editor = require("./config-editor"),
62 positions = ["beforeAll", "beforeRoutes", "afterRoutes", "errorHandler"],
63 middleware = editor.get("middleware");
64
65 console.log("");
66
67 if (-1 === positions.indexOf(position)) {
68 console.log(chalk.red(" Error: ") + "position must be one of " + chalk.yellow(positions.join(chalk.white(", "))));
69 } else {
70 if (middleware[position].indexOf(pkg) > -1)
71 console.log(" " + chalk.red("No Change:") + " " + chalk.yellow(pkg) + " was already in " + chalk.yellow(position));
72 else {
73 middleware[position].push(pkg);
74 editor.set("middleware." + position, middleware[position]);
75 editor.save();
76 console.log(" " + chalk.cyan("Added:") + " " + chalk.yellow(pkg) + " added to " + chalk.yellow(position));
77 }
78 }
79
80 console.log("");
81
82 },
83
84
85 /**
86
87 Removes custom middleware from the stack. Valid positions include: beforeAll,
88 beforeRoutes, afterRoutes and errorHandler
89
90 @param {String} position The middleware position
91 @param {String} pkg The name of the package
92
93 **/
94
95 remove: function(position, pkg) {
96
97 var editor = require("./config-editor"),
98 positions = ["beforeAll", "beforeRoutes", "afterRoutes", "errorHandler"],
99 middleware = editor.get("middleware");
100
101 console.log("");
102
103 if (-1 === positions.indexOf(position)) {
104 console.log(chalk.red(" Error: ") + "position must be one of " + chalk.yellow(positions.join(chalk.white(", "))));
105 } else {
106 if (middleware[position].indexOf(pkg) === -1)
107 console.log(" " + chalk.red("No Change:") + " " + chalk.yellow(pkg) + " wasn't in " + chalk.yellow(position));
108 else {
109 middleware[position].splice(middleware[position].indexOf(pkg), 1);
110 editor.set("middleware." + position, middleware[position]);
111 editor.save();
112 console.log(" " + chalk.cyan("Removed:") + " " + chalk.yellow(pkg) + " removed from " + chalk.yellow(position));
113 }
114 }
115
116 console.log("");
117
118 }
119
120};