1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | exports.openConfiguration = function (configName) {
|
16 | var NA = this,
|
17 | path = NA.modules.path,
|
18 | data = {},
|
19 | pathfile = path.join(NA.serverPath, configName);
|
20 |
|
21 | try {
|
22 |
|
23 |
|
24 | delete require.cache[pathfile];
|
25 | return require(pathfile);
|
26 | } catch (exception) {
|
27 | if (exception.toString().indexOf("SyntaxError") !== -1) {
|
28 |
|
29 |
|
30 | data.syntaxError = exception.toString();
|
31 | data.fileName = configName;
|
32 | NA.log(NA.cliLabels.webconfigSyntaxError.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return data[matches]; }));
|
33 | } else {
|
34 |
|
35 |
|
36 | NA.log('openConfiguration exception', exception);
|
37 | }
|
38 |
|
39 |
|
40 | process.kill(process.pid);
|
41 | }
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | exports.log = function () {
|
53 | var logs = console.log,
|
54 | args = arguments,
|
55 | color = "\u001b[36m";
|
56 |
|
57 | if (/[\u001b]/.test(arguments[0])) {
|
58 | args = Array.prototype.slice.call(arguments, 1);
|
59 | color = arguments[0];
|
60 | }
|
61 |
|
62 | for (var log in args) {
|
63 | if (args.hasOwnProperty(log)) {
|
64 | logs(color + args[log] + '\u001B[0m');
|
65 | }
|
66 | }
|
67 | };
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | exports.openController = function (controller) {
|
78 | var NA = this,
|
79 | path = NA.modules.path,
|
80 | commonControllerPath = path.join(
|
81 | NA.serverPath,
|
82 | NA.webconfig.controllersRelativePath,
|
83 | (controller) ? controller : ''
|
84 | );
|
85 |
|
86 |
|
87 | if (typeof controller !== 'undefined') {
|
88 |
|
89 |
|
90 | try {
|
91 | NA.controllers[controller] = require(commonControllerPath);
|
92 |
|
93 | } catch (err) {
|
94 | if (err && err.code === 'MODULE_NOT_FOUND') {
|
95 | err.controller = commonControllerPath;
|
96 | return NA.log(NA.cliLabels.notFound.controller.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return err[matches]; }));
|
97 | } else if (err) {
|
98 | return NA.log('openController exception', err);
|
99 | }
|
100 | }
|
101 | }
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | function explainError(NA, err, variationsPath, languageCode, errorDisabled) {
|
116 | err.variationsPath = variationsPath;
|
117 | if (err.code === 'ENOENT' && !errorDisabled && !languageCode) {
|
118 | NA.log(NA.cliLabels.variationNotFound.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return err[matches]; }));
|
119 | } else if (err.toString().indexOf('SyntaxError') !== -1) {
|
120 | err.syntaxError = err.toString();
|
121 | NA.log(NA.cliLabels.variationSyntaxError.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return err[matches]; }));
|
122 | } else if (err.code !== 'ENOENT') {
|
123 | NA.log(err);
|
124 | }
|
125 | return false;
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | exports.openVariation = function (variationName, languageCode, errorDisabled) {
|
140 | var NA = this,
|
141 | fs = NA.modules.fs,
|
142 | path = NA.modules.path,
|
143 | variationsPath;
|
144 |
|
145 |
|
146 | variationsPath = path.join(
|
147 | NA.serverPath,
|
148 | NA.webconfig.variationsRelativePath,
|
149 | (languageCode) ? languageCode : '',
|
150 | (variationName) ? variationName : ''
|
151 | );
|
152 |
|
153 |
|
154 | if (typeof variationName !== 'undefined') {
|
155 | try {
|
156 |
|
157 | return JSON.parse(fs.readFileSync(variationsPath, 'utf-8'));
|
158 | } catch (err) {
|
159 |
|
160 | explainError(NA, err, variationsPath, languageCode, errorDisabled);
|
161 | }
|
162 | } else {
|
163 | return {};
|
164 | }
|
165 | };
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | exports.openView = function (routeParameters, viewsPath, callback) {
|
178 | var NA = this,
|
179 | fs = NA.modules.fs;
|
180 |
|
181 | fs.readFile(viewsPath, 'utf-8', function (err, data) {
|
182 | if (NA.webconfig.view) {
|
183 | data = data.replace("#{routeParameters.view}", routeParameters.view);
|
184 | }
|
185 | if (err) {
|
186 | err.viewsPath = viewsPath;
|
187 | if (typeof routeParameters.view === 'undefined') {
|
188 | NA.log(NA.cliLabels.viewNotSet);
|
189 | } else {
|
190 | NA.log(NA.cliLabels.viewNotFound.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return err[matches]; }));
|
191 | }
|
192 | } else {
|
193 |
|
194 | |
195 |
|
196 |
|
197 |
|
198 |
|
199 | callback(data);
|
200 | }
|
201 | });
|
202 | };
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | exports.clone = function (object) {
|
213 | var NA = this,
|
214 | copy,
|
215 | result;
|
216 |
|
217 |
|
218 | if (null === object || undefined === object || "object" !== typeof object) {
|
219 | result = object;
|
220 | }
|
221 |
|
222 |
|
223 | if (object instanceof Date) {
|
224 | copy = new Date();
|
225 | copy.setTime(object.getTime());
|
226 | result = copy;
|
227 | }
|
228 |
|
229 |
|
230 | if (object instanceof Array) {
|
231 | result = object.slice(0);
|
232 | }
|
233 |
|
234 |
|
235 | if (object instanceof Object) {
|
236 | copy = {};
|
237 | NA.forEach(object, function (attr) {
|
238 | copy[attr] = NA.clone(object[attr]);
|
239 | });
|
240 | result = copy;
|
241 | }
|
242 |
|
243 | return result;
|
244 | };
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | exports.forEach = function (object, callback) {
|
255 | if (object instanceof Array) {
|
256 | for (var i = 0; i < object.length; i++) {
|
257 | callback(object[i], object);
|
258 | }
|
259 | } else {
|
260 | for (var current in object) {
|
261 | if (object.hasOwnProperty(current)) {
|
262 |
|
263 | |
264 |
|
265 |
|
266 |
|
267 | callback(current, object);
|
268 | }
|
269 | }
|
270 | }
|
271 | };
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | exports.ifFileExist = function (physicalPath, fileName, callback) {
|
284 | var NA = this,
|
285 | fs = NA.modules.fs,
|
286 | path = NA.modules.path,
|
287 | pathToResolve = physicalPath;
|
288 |
|
289 | if (typeof fileName === 'string') {
|
290 | pathToResolve = path.join(physicalPath, fileName);
|
291 | }
|
292 |
|
293 |
|
294 | fs.stat(pathToResolve, function (err) {
|
295 | if (err && err.code === 'ENOENT') {
|
296 | err.path = pathToResolve;
|
297 | err.physicalPath = physicalPath;
|
298 | err.fileName = fileName;
|
299 |
|
300 | |
301 |
|
302 |
|
303 |
|
304 | callback(err, false);
|
305 | } else {
|
306 |
|
307 | |
308 |
|
309 |
|
310 | callback(null, true);
|
311 | }
|
312 | });
|
313 | };
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 | exports.common = function (languageCode, locals) {
|
324 | var NA = this,
|
325 | extend = NA.modules.extend;
|
326 |
|
327 |
|
328 | locals = locals || {};
|
329 |
|
330 |
|
331 | locals.common = NA.openVariation(NA.webconfig.variation, languageCode);
|
332 |
|
333 |
|
334 | if (languageCode) {
|
335 | locals.common = extend(true, NA.openVariation(NA.webconfig.variation, undefined, true), locals.common);
|
336 | }
|
337 |
|
338 | return locals;
|
339 | };
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | exports.specific = function (specific, languageCode, locals) {
|
351 | var NA = this,
|
352 | extend = NA.modules.extend;
|
353 |
|
354 |
|
355 | locals = locals || {};
|
356 |
|
357 |
|
358 | locals.specific = NA.openVariation(specific, languageCode);
|
359 |
|
360 |
|
361 | if (languageCode) {
|
362 | locals.specific = extend(true, NA.openVariation(specific, undefined, true), locals.specific);
|
363 | }
|
364 |
|
365 | return locals;
|
366 | };
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 | exports.view = function (viewFile, locals) {
|
377 | var NA = this,
|
378 | data,
|
379 | ejs = NA.modules.ejs,
|
380 | pug = NA.modules.pug,
|
381 | fs = NA.modules.fs,
|
382 | path = NA.modules.path,
|
383 | engine = NA.webconfig.pug ? pug : ejs;
|
384 |
|
385 |
|
386 | locals.filename = path.join(NA.serverPath, NA.webconfig.viewsRelativePath, viewFile);
|
387 |
|
388 | if (typeof locals.pug === "boolean") {
|
389 | engine = locals.pug ? pug : ejs;
|
390 | }
|
391 |
|
392 | try {
|
393 |
|
394 | data = engine.render(
|
395 | fs.readFileSync(path.join(NA.serverPath, NA.webconfig.viewsRelativePath, viewFile), 'utf-8'),
|
396 | locals
|
397 | );
|
398 | } catch (err) {
|
399 |
|
400 | data = err.toString()
|
401 | .replace(/</g, "<")
|
402 | .replace(/[\n]/g, "<br>")
|
403 | .replace(/\t/g, "<span style='display:inline-block;width:32px'></span>")
|
404 | .replace(/ /g, "<span style='display:inline-block;width:32px'></span>")
|
405 | .replace(/ /g, "<span style='display:inline-block;width:32px'></span>")
|
406 | .replace(/ /g, "<span style='display:inline-block;width:32px'></span>")
|
407 | .replace(/ >> /g, "<span style='display:inline-block;width:32px'>>></span>")
|
408 | .replace(/> ([0-9])+\|/g, "<span style='display:inline-block;margin-left:-13px'>> $1|</span>")
|
409 | .replace(/^([a-zA-Z]+):/g, "$1:<br><br>");
|
410 | }
|
411 |
|
412 | return data;
|
413 | };
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | exports.extend = function (objects) {
|
423 | var NA = this;
|
424 |
|
425 | function copyItem(source, prop) {
|
426 | if (source[prop].constructor === Object) {
|
427 | if (!objects[prop] || objects[prop].constructor === Object) {
|
428 | objects[prop] = objects[prop] || {};
|
429 | NA.extend(objects[prop], source[prop]);
|
430 | } else {
|
431 | objects[prop] = source[prop];
|
432 | }
|
433 | } else {
|
434 | objects[prop] = source[prop];
|
435 | }
|
436 | }
|
437 |
|
438 | Array.prototype.slice.call(arguments, 1).forEach(function(source) {
|
439 | if (source) {
|
440 | NA.forEach(source, function (prop) {
|
441 | copyItem(source, prop);
|
442 | });
|
443 | }
|
444 | });
|
445 |
|
446 | return objects;
|
447 | }; |
\ | No newline at end of file |