UNPKG

4.52 kBJavaScriptView Raw
1/**
2
3 routes-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
21var Routes = module.exports = {
22
23 /**
24
25 Outputs a list of routes.
26
27 **/
28
29 list: function() {
30
31 var editor = require("./config-editor"),
32 routes = editor.get("routes"),
33 columns = [];
34
35 for (var route in routes)
36 columns.push([route, routes[route].path, routes[route].type, (routes[route].bundleFolders ? "true" : "")]);
37
38 utils.showGrid(columns, {
39 title : "Routes:",
40 len : 20,
41 headers : ["Route", "Folder", "Contains", "Bundles Folders"]
42 });
43
44 },
45
46
47 /**
48
49 Adds a route and acknolwedges with confirmation. This function is not
50 usually used directory, but rather through helper functions (scripts, etc.)
51
52 @param {String} type The type of files folder contains (scripts, statics, styles, etc.)
53 @param {String} route The base route for the folder
54 @param {String} folder The folder containing the files
55 @param {Array} [options.exts] Array of strings representing permitted file extensions
56 @param {Boolean} [options.bundleFolders] If true, automatically bundles appropriate folders
57
58 **/
59
60 add: function(type, route, folder, options) {
61
62 var editor = require("./config-editor"),
63 routes = editor.get("routes"),
64 prev;
65
66 options = options || {};
67
68 // Ensure that leading "./" is removed from folder
69 if (/^\.\//.test(folder)) folder = folder.slice(2);
70
71 // Ensure that route has preceeding slash
72 if ("/" !== route[0]) route = "/" + route;
73 prev = routes[route];
74
75 function suffix(x, y) {
76 return "route " + chalk.yellow(x) + " requests to the " + chalk.yellow(y) + " folder";
77 }
78
79 console.log("");
80
81 // Remove if existing
82 if (prev) {
83 if (prev.path === folder) {
84 console.log(" " + chalk.cyan("No Change:") + " shipp will already " + suffix(prev.url, prev.path));
85 console.log("");
86 return;
87 }
88 console.log(" " + chalk.cyan("Removed:") + " shipp will no longer " + suffix(prev.url, prev.path));
89 }
90
91 // Add new
92 obj = { type : type, path : folder, url : route };
93 if (options.exts) obj.exts = options.exts;
94 if (options.bundleFolders) obj.bundleFolders = true;
95
96 editor.set("routes." + route, obj);
97 editor.save();
98
99 console.log(" " + chalk.cyan("Added:") + " shipp will " + suffix(route, folder));
100 console.log("");
101
102 },
103
104
105 /**
106
107 Removes a route and acknowledges with confirmation.
108
109 @param {String} route The route to remove
110
111 **/
112
113 remove: function(route) {
114
115 var editor = require("./config-editor"),
116 routes = editor.get("routes"),
117 prev;
118
119 // Ensure that route has preceeding slash
120 if ("/" !== route[0]) route = "/" + route;
121 prev = editor.get("routes." + route);
122
123 editor.unset("routes." + route);
124 editor.save();
125
126 console.log("");
127
128 if ("undefined" === typeof prev)
129 console.log(" " + chalk.red("No Change:") + " there was no route called " + chalk.yellow(route));
130 else
131 console.log(" " + chalk.cyan("Removed:") + " route " + chalk.yellow(route) + " has been removed");
132
133 console.log("");
134
135 },
136
137
138 /**
139
140 Helper function to add a scripts folder
141
142 @param {String} route The base route for the folder
143 @param {String} folder The folder containing the files
144 @param {Boolean} [options.bundleFolders] If true, automatically bundles appropriate folders
145
146 **/
147
148 scripts: function(route, folder, bundleFolders) {
149 bundleFolders = ("false" !== bundleFolders);
150 Routes.add("views", route, folder, { exts : ["js"], bundleFolders : bundleFolders });
151 },
152
153
154 /**
155
156 Helper function to add a statics folder
157
158 @param {String} route The base route for the folder
159 @param {String} folder The folder containing the files
160
161 **/
162
163 statics: function(route, folder) {
164 Routes.add("statics", route, folder);
165 },
166
167
168 /**
169
170 Helper function to add a styles folder
171
172 @param {String} route The base route for the folder
173 @param {String} folder The folder containing the files
174
175 **/
176
177 styles: function(route, folder) {
178 Routes.add("styles", route, folder, { exts : ["css"] });
179 },
180
181
182 /**
183
184 Helper function to add a views folder
185
186 @param {String} route The base route for the folder
187 @param {String} folder The folder containing the files
188
189 **/
190
191 views: function(route, folder) {
192 Routes.add("views", route, folder, { exts : ["html"] });
193 }
194
195};