UNPKG

22.1 kBJavaScriptView Raw
1/*------------------------------------*\
2 CONFIGURATION
3\*------------------------------------*/
4/* jslint node: true */
5
6/**
7 * Set required variables for application and CLI language messages.
8 * @private
9 * @function initRequiredVars
10 * @memberOf NA#
11 * @this NA
12 */
13exports.initRequiredVars = function () {
14 var NA = this,
15 path = NA.modules.path;
16
17 /**
18 * Name of file which contains language error messages. The name of file is without extension.
19 * @private
20 * @alias cliLanguage
21 * @type {string}
22 * @memberOf NA#
23 * @default "default"
24 */
25 NA.cliLanguage = "default";
26
27 /**
28 * All internationalize labels from `NA#cliLanguage` file.
29 * @private
30 * @alias cliLabels
31 * @type {Object}
32 * @memberOf NA#
33 */
34 NA.cliLabels = require("../languages/" + NA.cliLanguage + ".json");
35
36 /**
37 * OS absolute path which contains NodeAtlas folders and files.
38 * @public
39 * @alias nodeatlasPath
40 * @type {string}
41 * @memberOf NA#
42 * @default « The path where `node-atlas` module is. »
43 */
44 NA.nodeatlasPath = path.join(__dirname, "..");
45
46 /**
47 * Contain all functions of controllers both common and specific.
48 * @namespace controllers[]
49 * @private
50 * @alias controllers
51 * @type {Array.<Object>}
52 * @memberOf NA#
53 * @example // Functions for common controller if common `controller` value is "common.json".
54 * NA.controllers["common.json"].setModules(...);
55 * NA.controllers["common.json"].setSessions(...);
56 * NA.controllers["common.json"].setSockets(...);
57 * NA.controllers["common.json"].setConfigurations(...);
58 * NA.controllers["common.json"].setRoutes(...);
59 * NA.controllers["common.json"].changeVariations(...);
60 * NA.controllers["common.json"].changeDom(...);
61 *
62 * // Functions for specific controller if a route `controller` value is "index.json".
63 * NA.controllers["index.json"].setSockets(...);
64 * NA.controllers["index.json"].changeVariations(...);
65 * NA.controllers["index.json"].changeDom(...);
66 */
67 NA.controllers = [];
68};
69
70/**
71 * Set command line options usable when NodeAtlas is executed as CLI.
72 * @private
73 * @function initCliConfiguration
74 * @memberOf NA#
75 * @this NA
76 */
77exports.initCliConfiguration = function () {
78 var NA = this,
79 commander = NA.modules.commander,
80 commands = [
81 "lang",
82 "generate",
83 "create",
84 "httpSecure",
85 "browse",
86 "version",
87 "cache",
88 "path",
89 "webconfig",
90 "httpHostname",
91 "httpPort",
92 ],
93 i;
94
95 /**
96 * Allows you to know the current version of NodeAtlas.
97 * @public
98 * @alias version
99 * @type {string}
100 * @memberOf NA#
101 */
102 NA.version = require("../package.json").version;
103
104 commander
105
106 /* Version of NodeAtlas currently in use with `--version` option. */
107 .version(NA.version)
108
109 /* Automaticly run default browser with `--browse` options. If a param is setted, the param is added to the and of url. */
110 .option(NA.cliLabels.commander.browse.command, NA.cliLabels.commander.browse.description, String)
111
112 /* Target the directory in which website and NodeAtlas will be running. */
113 .option(NA.cliLabels.commander.path.command, NA.cliLabels.commander.path.description, String)
114
115 /* Change name of JSON file used as the webconfig file. */
116 .option(NA.cliLabels.commander.webconfig.command, NA.cliLabels.commander.webconfig.description, String)
117
118 /* Change the hostname that runs the NodeAtlas website. */
119 .option(NA.cliLabels.commander.httpHostname.command, NA.cliLabels.commander.httpHostname.description, String)
120
121 /* Change the port that runs the NodeAtlas website. */
122 .option(NA.cliLabels.commander.httpPort.command, NA.cliLabels.commander.httpPort.description, String)
123
124 /* Minify all files and re-create all HTML assets into generates folder. */
125 .option(NA.cliLabels.commander.generate.command, NA.cliLabels.commander.generate.description)
126
127 /* Avoid cache at all levels (Server, Template Engine...). */
128 .option(NA.cliLabels.commander.cache.command, NA.cliLabels.commander.cache.description)
129
130 /* Copy all data from `test/<project-name>` from NodeAtlas package to current directory. */
131 .option(NA.cliLabels.commander.create.command, NA.cliLabels.commander.create.description)
132
133 /* Start the server with HTTPs Protocol. */
134 .option(NA.cliLabels.commander.httpSecure.command, NA.cliLabels.commander.httpSecure.description)
135
136 /* Change language used by NodeAtlas. */
137 .option(NA.cliLabels.commander.lang.command, NA.cliLabels.commander.lang.description)
138 .parse(process.argv);
139
140 /* `NA#configuration.xxx` setted from `--xxx`. */
141 for (i = 0; i < commands.length; i++) {
142 if (commander[commands[i]]) {
143 NA.configuration[commands[i]] = commander[commands[i]];
144 }
145 }
146};
147
148/**
149 * Set required variables for the webconfig depending of NPM modules.
150 * @private
151 * @function initRequiredNpmModulesVars
152 * @memberOf NA#
153 * @this NA
154 */
155exports.initRequiredNpmModulesVars = function () {
156 var NA = this,
157 path = NA.modules.path;
158
159 if (typeof NA.configuration.path !== "string") {
160
161 /**
162 * System files path to the NodeAtlas project directory (webconfig + website folder tree).
163 * @public
164 * @alias serverPath
165 * @type {string}
166 * @memberOf NA#
167 * @default « System files path from NodeAtlas is called ».
168 */
169 NA.serverPath = path.join(process.cwd(), "/");
170 } else {
171
172 /* `NA#serverPath` setted from CLI command parameters or API configuration options. */
173 NA.serverPath = path.join(NA.configuration.path, "/");
174 }
175
176 /**
177 * Name of the webconfig used for run the NodeAtlas project website.
178 * @public
179 * @alias webconfigName
180 * @type {string}
181 * @memberOf NA#
182 * @default "webconfig.json".
183 */
184 NA.webconfigName = "webconfig.json";
185
186 /* `webconfigName` manually setted value with `NA#config`. */
187 if (NA.configuration.webconfig) {
188 NA.webconfigName = NA.configuration.webconfig;
189 }
190};
191
192/**
193 * Decide to run a « Simple Web Server » or a « With Weconfig Server » depending to webconfig opening success.
194 * If webconfig is correctly openned, the `NA#createWebconfig` and `callback` function will be run, else, just `NA#simpleWebServer` will be run.
195 * @private
196 * @function initWebsite
197 * @memberOf NA#
198 * @this NA
199 * @param {NA~callback} next Called if webconfig is correctly openned.
200 */
201exports.initWebsite = function (next) {
202 var NA = this;
203
204 /* Webconfig based website... */
205 NA.ifFileExist(NA.serverPath, NA.webconfigName, function (err) {
206 if (err && err.code === 'ENOENT') {
207
208 /* ... or static website. */
209 return NA.simpleWebServer();
210 }
211
212 /* Construction of webconfig. */
213 NA.createWebconfig();
214
215 next();
216 });
217};
218
219/**
220 * Set modules and NPM modules used by NodeAtlas project.
221 * @private
222 * @function initServerModules
223 * @memberOf NA#
224 * @this NA
225 */
226exports.initServerModules = function () {
227 var NA = this;
228
229 /* Open `common` controller. */
230 NA.openController(
231
232 /**
233 * Name of file for `common` controller.
234 * @public
235 * @alias controller
236 * @type {string}
237 * @memberOf NA#webconfig
238 */
239 NA.webconfig.controller);
240
241 /**
242 * Folder which contain the `node-atlas` node modules.
243 * @public
244 * @alias nodeatlasModulesRelativePath
245 * @type {string}
246 * @memberOf NA#
247 * @default "node_modules/"
248 */
249 NA.nodeatlasModulesRelativePath = "node_modules/";
250
251 /**
252 * Folder which contain the current website node modules.
253 * @public
254 * @alias serverModulesRelativePath
255 * @type {string}
256 * @memberOf NA#
257 * @default "node_modules/"
258 */
259 NA.serverModulesRelativePath = "node_modules/";
260
261 /* Use the `NA.controllers[<controller>].setModules(...)` function if set... */
262 if (typeof NA.controllers[NA.webconfig.controller] !== 'undefined' &&
263 typeof NA.controllers[NA.webconfig.controller].setModules !== 'undefined') {
264
265 /**
266 * Define this function for adding npm module into `NA.modules` of application. Only for `common` controller file.
267 * @function setModules
268 * @memberOf NA#controllers[]
269 */
270 NA.controllers[NA.webconfig.controller].setModules.call(NA);
271 }
272};
273
274/**
275 * Set routes, statics and prefix routes.
276 * @private
277 * @function setWebconfigRoutes
278 * @memberOf NA~
279 */
280function setWebconfigRoutes(NA) {
281 var path = NA.modules.path,
282 url = NA.modules.url;
283
284 /**
285 * Adding subfolder to original url.
286 * @public
287 * @alias urlRelativeSubPath
288 * @type {string}
289 * @memberOf NA#webconfig
290 * @default ""
291 * @example
292 * // If `NA#webconfig.urlRelativeSubPath` is setted to "example"
293 * // Website will run by default to « http://localhost/example »
294 */
295 if (NA.webconfig.urlRelativeSubPath) {
296 NA.webconfig.urlRelativeSubPath = url.format(path.join("/", NA.webconfig.urlRelativeSubPath)).replace(/\/+$/g, "");
297 } else {
298 NA.webconfig.urlRelativeSubPath = "";
299 }
300
301 if (typeof NA.webconfig.routes === "string") {
302
303 /**
304 * Contain all routes into an Object or an Array depending how the intial format setted.
305 * @public
306 * @alias routes
307 * @type {Object|Array}
308 * @memberOf NA#webconfig
309 * @default « The webconfig's object property `routes` »
310 */
311 NA.webconfig.routes = NA.openConfiguration(NA.webconfig.routes);
312 }
313
314 if (typeof NA.webconfig.statics === "string") {
315
316 /**
317 * Contain all statics files into an Object or an Array depending how the intial format setted.
318 * @public
319 * @alias statics
320 * @type {Object|Array}
321 * @memberOf NA#webconfig
322 * @default « The webconfig's object property `statics` »
323 */
324 NA.webconfig.statics = NA.openConfiguration(NA.webconfig.statics);
325 }
326}
327
328/**
329 * Set bundles and optimizations.
330 * @private
331 * @function setWebconfigCompressions
332 * @memberOf NA~
333 */
334function setWebconfigCompressions(NA) {
335
336 if (typeof NA.webconfig.bundles === 'string') {
337
338 /**
339 * Configuration for CSS minification/bundle and JS obfuscation/bundle.
340 * @public
341 * @alias bundles
342 * @type {Object}
343 * @memberOf NA#webconfig
344 * @property {Object} bundles The bundles object.
345 * @property {Object} bundles.javascripts Each object name represent an output javascript file and each property of object represent an array of inputs files.
346 * @property {Object} bundles.stylesheets Each object name represent an output stylesheet file and each property of object represent an array of inputs files.
347 * @example {
348 * "javascripts": {
349 * "javascript/framework.min.js": [
350 * "javascript/modernizr.js",
351 * "javascript/jquery.js",
352 * "javascript/prettify.js",
353 * "javascript/prettify/run_prettify.js"
354 * ],
355 * "javascript/common.min.js": [
356 * "javascript/components/extended-format-date.js",
357 * "javascript/common.js"
358 * ]
359 * },
360 * "stylesheets": {
361 * "stylesheets/common.min.css": [
362 * "stylesheets/common.css",
363 * "stylesheets/common-min780.css",
364 * "stylesheets/common-min1160.css"
365 * ]
366 * }
367 * }
368 */
369 NA.webconfig.bundles = NA.openConfiguration(NA.webconfig.bundles);
370 }
371}
372
373/**
374 * Set pug, less and enbaleStylus.
375 * @private
376 * @function setWebconfigPreprocessors
377 * @memberOf NA~
378 */
379function setWebconfigPreprocessors(NA) {
380
381 /**
382 * Enable Pug preprocessor.
383 * @public
384 * @alias pug
385 * @type {boolean}
386 * @memberOf NA#webconfig
387 * @default false
388 */
389 NA.webconfig.pug = NA.webconfig.pug || false;
390
391 if (NA.webconfig.less === true) {
392
393 /**
394 * Enable Less preprocessor.
395 * @namespace less
396 * @public
397 * @alias less
398 * @type {boolean|Object}
399 * @memberOf NA#webconfig
400 * @default false
401 * @property {boolean} compress Minify the Less file.
402 * @property {boolean} sourceMap Create a sourceMap file for development.
403 * @property {Array.<string>} files The file for compilation in an Array.
404 */
405 NA.webconfig.less = {
406 compress: false,
407 sourceMap: true
408 };
409 } else if (typeof NA.webconfig.less === "object" && typeof NA.webconfig.less.files === "string") {
410
411 /**
412 * Contain Less files required for compilation.
413 * @public
414 * @alias files
415 * @type {Array.<string>}
416 * @memberOf NA#webconfig.less
417 * @example {
418 * "files": [
419 * "stylesheets/common.less",
420 * "stylesheets/component-1.less",
421 * "stylesheets/component-2.less",
422 * "stylesheets/component-3.less"
423 * ]
424 * }
425 */
426 NA.webconfig.less.files = NA.openConfiguration(NA.webconfig.less.files);
427 }
428
429 if (NA.webconfig.stylus === true) {
430
431 /**
432 * Enable Stylus preprocessor.
433 * @namespace stylus
434 * @public
435 * @alias stylus
436 * @type {boolean|Object}
437 * @memberOf NA#webconfig
438 * @default false
439 * @property {boolean} compress Minify the Stylus file.
440 * @property {boolean} sourceMap Create a sourceMap file for development.
441 * @property {Array.<string>} files The file for compilation in an Array.
442 */
443 NA.webconfig.stylus = {
444 compress: false,
445 sourceMap: true
446 };
447 } else if (typeof NA.webconfig.stylus === 'object' && typeof NA.webconfig.stylus.files === 'string') {
448
449 /**
450 * Contain Stylus files required for compilation.
451 * @public
452 * @alias files
453 * @type {Array.<string>}
454 * @memberOf NA#webconfig.stylus
455 * @example {
456 * "files": [
457 * "stylesheets/common.styl",
458 * "stylesheets/component-1.styl",
459 * "stylesheets/component-2.styl",
460 * "stylesheets/component-3.styl"
461 * ]
462 * }
463 */
464 NA.webconfig.stylus.files = NA.openConfiguration(NA.webconfig.stylus.files);
465 }
466}
467
468/**
469 * Set NodeAtlas directory names.
470 * @private
471 * @function setWebconfigDirectories
472 * @memberOf NA~
473 */
474function setWebconfigDirectories(NA) {
475
476 if (typeof NA.webconfig.variationsRelativePath === "undefined") {
477
478 /**
479 * Language and variable variation files folder depending of languages.
480 * @public
481 * @alias variationsRelativePath
482 * @type {string}
483 * @memberOf NA#webconfig
484 * @default "variations"
485 */
486 NA.webconfig.variationsRelativePath = "variations";
487 }
488
489 if (typeof NA.webconfig.controllersRelativePath === "undefined") {
490
491 /**
492 * Controller folder for Back-end part.
493 * @public
494 * @alias controllersRelativePath
495 * @type {string}
496 * @memberOf NA#webconfig
497 * @default "controllers"
498 */
499 NA.webconfig.controllersRelativePath = "controllers";
500 }
501
502 if (typeof NA.webconfig.middlewaresRelativePath === "undefined") {
503
504 /**
505 * Controller folder for Middlewares part.
506 * @public
507 * @alias middlewaresRelativePath
508 * @type {string}
509 * @memberOf NA#webconfig
510 * @default "middlewares"
511 */
512 NA.webconfig.middlewaresRelativePath = "middlewares";
513 }
514
515 /* Path to view. */
516 if (typeof NA.webconfig.viewsRelativePath === "undefined") {
517
518 /**
519 * View folder for Template Engine files.
520 * @public
521 * @alias viewsRelativePath
522 * @type {string}
523 * @memberOf NA#webconfig
524 * @default "views"
525 */
526 NA.webconfig.viewsRelativePath = "views";
527 }
528
529 if (typeof NA.webconfig.assetsRelativePath === "undefined") {
530
531 /**
532 * Folder for public file like images, CSS, JS...
533 * @public
534 * @alias assetsRelativePath
535 * @type {string}
536 * @memberOf NA#webconfig
537 * @default "assets"
538 */
539 NA.webconfig.assetsRelativePath = "assets";
540 }
541
542 if (typeof NA.webconfig.serverlessRelativePath === "undefined") {
543
544 /**
545 * HTML assets generation Folder.
546 * @public
547 * @alias serverlessRelativePath
548 * @type {string}
549 * @memberOf NA#webconfig
550 * @default "serverless"
551 */
552 NA.webconfig.serverlessRelativePath = "serverless";
553 }
554}
555
556/**
557 * Set HTTP Port and HTTP hostname.
558 * @private
559 * @function setWebconfigHost
560 * @memberOf NA~
561 */
562function setWebconfigHost(NA) {
563 var defaultPort = NA.webconfig.httpSecure ? 443 : 80;
564
565 /**
566 * Server listening port.
567 * @public
568 * @alias httpPort
569 * @type {string}
570 * @memberOf NA#webconfig
571 * @default « The webconfig's property `httpPort` » || « The `process.env.PORT` if setted » || « 443/80 default port »
572 */
573 NA.webconfig.httpPort = NA.configuration.httpPort || NA.webconfig.httpPort || process.env.PORT || defaultPort;
574
575 /**
576 * Url access port (for reverse proxy).
577 * @public
578 * @alias urlPort
579 * @type {string}
580 * @memberOf NA#webconfig
581 * @default undefined
582 */
583 NA.webconfig.urlPort = NA.webconfig.urlPort || NA.webconfig.httpPort;
584
585 /**
586 * Server listening hostname by HTTP.
587 * @public
588 * @alias httpHostname
589 * @type {string}
590 * @memberOf NA#webconfig
591 * @default « The webconfig's property `httpHostname` » || « The `process.env.IP_ADDRESS` if setted » || "localhost";
592 */
593 NA.webconfig.httpHostname = NA.configuration.httpHostname || NA.webconfig.httpHostname || process.env.IP_ADDRESS || "localhost";
594
595 /**
596 * Url access hostname (for reverse proxy).
597 * @public
598 * @alias urlHostname
599 * @type {string}
600 * @memberOf NA#webconfig
601 * @default undefined
602 */
603 NA.webconfig.urlHostname = NA.webconfig.urlHostname || NA.webconfig.httpHostname;
604}
605
606/**
607 * Set default Cache and Secure.
608 * @private
609 * @function setWebconfigSecure
610 * @memberOf NA~
611 */
612function setWebconfigSecure(NA) {
613
614 /**
615 * Avoil all caching at all level (server, template engine...). Do not use in production.
616 * @public
617 * @alias cache
618 * @type {boolean}
619 * @memberOf NA#webconfig
620 * @default false
621 */
622 NA.webconfig.cache = NA.configuration.cache || NA.webconfig.cache || (process.env.NODE_ENV === 'production');
623
624 /**
625 * Define is site is running with HTTP(S) protocol.
626 * @public
627 * @alias httpSecure
628 * @type {boolean|string|Object}
629 * @memberOf NA#webconfig
630 * @default false
631 */
632 NA.webconfig.httpSecure = NA.configuration.httpSecure || NA.webconfig.httpSecure || false;
633
634 /**
635 * Define the path to the Private Key for HTTPs.
636 * @public
637 * @alias httpSecureKeyRelativePath
638 * @type {string}
639 * @memberOf NA#webconfig
640 */
641 NA.webconfig.httpSecureKeyRelativePath = NA.webconfig.httpSecureKeyRelativePath || ((typeof NA.webconfig.httpSecure === "string") ? NA.webconfig.httpSecure + ".key" : null);
642
643 /**
644 * Define the path to the Certificate for HTTPs.
645 * @public
646 * @alias httpSecureCertificateRelativePath
647 * @type {string}
648 * @memberOf NA#webconfig
649 */
650 NA.webconfig.httpSecureCertificateRelativePath = NA.webconfig.httpSecureCertificateRelativePath || ((typeof NA.webconfig.httpSecure === "string") ? NA.webconfig.httpSecure + ".crt" : null);
651}
652
653/**
654 * Set the mimeType, charset and headers.
655 * @private
656 * @function setWebconfigHeaders
657 * @memberOf NA~
658 */
659function setWebconfigHeaders(NA) {
660
661 /**
662 * Default Content-Type used for all pages.
663 * @public
664 * @alias mimeType
665 * @type {string}
666 * @memberOf NA#webconfig
667 * @default "text/html"
668 */
669 NA.webconfig.mimeType = NA.webconfig.mimeType || "text/html";
670
671 /**
672 * Default Charset used for all pages.
673 * @public
674 * @alias charset
675 * @type {string}
676 * @memberOf NA#webconfig
677 * @default "utf-8"
678 */
679 NA.webconfig.charset = NA.webconfig.charset || "utf-8";
680
681 /**
682 * Default Headers used for all pages.
683 * @public
684 * @alias headers
685 * @type {Object}
686 * @memberOf NA#webconfig
687 * @default {}
688 */
689 NA.webconfig.headers = NA.webconfig.headers || {};
690}
691
692/**
693 * Set all default webconfig's value into `NA#webconfig`.
694 * @private
695 * @function createWebconfig
696 * @memberOf NA#
697 */
698exports.createWebconfig = function () {
699 var NA = this,
700 fs = NA.modules.fs,
701 path = NA.modules.path,
702 ejs = NA.modules.ejs;
703
704 /**
705 * Content of Webconfig file enhenced by `NA#configuration` and CLI commands.
706 * @namespace webconfig
707 * @public
708 * @alias webconfig
709 * @type {Object}
710 * @memberOf NA#
711 */
712 NA.webconfig = NA.openConfiguration(NA.webconfigName);
713
714 /**
715 * Find the version of current NodeAtlas website.
716 * @public
717 * @alias version
718 * @type {string}
719 * @memberOf NA#webconfig
720 * @default "0.0.0"
721 */
722 NA.webconfig.version = NA.webconfig.version || (fs.existsSync(path.join(NA.serverPath, "package.json")) ? (require(path.join(NA.serverPath, "package.json")).version || "0.0.0") : "0.0.0");
723
724 /* Set all webconfig parameters. */
725 setWebconfigRoutes(NA);
726 setWebconfigCompressions(NA);
727 setWebconfigPreprocessors(NA);
728 setWebconfigDirectories(NA);
729 setWebconfigHost(NA);
730 setWebconfigSecure(NA);
731 setWebconfigHeaders(NA);
732
733 /**
734 * Set open and close bracket used by Teplate Engine.
735 * @public
736 * @alias templateEngineDelimiter
737 * @type {string}
738 * @memberOf NA#webconfig
739 * @default "?"
740 */
741 ejs.delimiter = NA.webconfig.templateEngineDelimiter || (
742
743 /**
744 * Set a custom template engine.
745 * @public
746 * @alias engine
747 * @type {string}
748 * @memberOf NA#webconfig
749 * @default undefined
750 */
751 NA.webconfig.engine ? "%" : "?");
752
753 /**
754 * Deliver the client-side window.NA.socket and window.NA.io object.
755 * @public
756 * @alias socketClientFile
757 * @type {string}
758 * @memberOf NA#webconfig
759 * @default "/node-atlas/socket.io.js"
760 */
761 NA.webconfig.socketClientFile = (NA.webconfig.socketClientFile === false) ? false : (NA.webconfig.socketClientFile || "/node-atlas/socket.io.js");
762
763 /**
764 * Website http(s) absolute url based from `NA#webconfig.httpSecure`, `NA#webconfig.urlHostname` and `NA#webconfig.urlPort`.
765 * This value does not contain `NA#webconfig.urlRelativeSubPath`.
766 * @public
767 * @alias urlRoot
768 * @type {string}
769 * @memberOf NA#webconfig
770 */
771 NA.webconfig.urlRoot = 'http' + ((NA.webconfig.httpSecure) ? 's' : '') + '://' + NA.webconfig.urlHostname + ((NA.webconfig.urlPort !== ((NA.webconfig.httpSecure) ? 443 : 80)) ? ':' + NA.webconfig.urlPort : '');
772};
\No newline at end of file