1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var chalk = require("chalk"),
|
13 | utils = require("./cli-utils");
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | module.exports = {
|
22 |
|
23 | |
24 |
|
25 |
|
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 |
|
52 |
|
53 |
|
54 |
|
55 |
|
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 |
|
88 |
|
89 |
|
90 |
|
91 |
|
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 | };
|