UNPKG

8.69 kBJavaScriptView Raw
1/*------------------------------------*\
2 $%INIT
3\*------------------------------------*/
4/* jslint node: true */
5
6/**
7 * Represent options passed through API.
8 * @private
9 * @alias configuration
10 * @type {Object}
11 * @memberOf NA#
12 * @example {
13 * path: <string>,
14 * webconfig: <string>,
15 * browse: <boolean|string>,
16 * httpHostname: <string>,
17 * httpPort: <number>,
18 * generate: <boolean>,
19 * cache: <boolean>,
20 * lang: <string>,
21 * create: <string>,
22 * httpSecure: <boolean|string>
23 * }
24 */
25exports.configuration = {};
26
27/**
28 * Represent the function called after all assets generation.
29 * @private
30 * @alias afterGeneration
31 * @type {Object}
32 * @memberOf NA#
33 */
34exports.afterGeneration = null;
35
36/**
37 * Represent the function called after create a project.
38 * @private
39 * @alias afterNewProject
40 * @type {Object}
41 * @memberOf NA#
42 */
43exports.afterNewProject = null;
44
45/**
46 * Represent the function called after the server was started.
47 * @private
48 * @alias afterRunning
49 * @type {Object}
50 * @memberOf NA#
51 */
52exports.afterRunning = null;
53
54/**
55 * Represent the function called after the server was stoped.
56 * @private
57 * @alias afterClosing
58 * @type {Object}
59 * @memberOf NA#
60 */
61exports.afterClosing = null;
62
63/**
64 * Set private `NA#configuration`.
65 * @public
66 * @function init
67 * @memberOf NA#
68 * @this NA
69 * @param {Object} options CLI Parameters as JSON object.
70 * @example require('node-atlas')().init({
71 * webconfig: "webconfig.alternatif.json",
72 * httpPort: 7778,
73 * generate: true,
74 * browse: true
75 * }).start();
76 * @return {Object} The NA instance for chained functions.
77 */
78exports.init = function (options) {
79 var NA = this,
80 configuration = options || {};
81
82 NA.configuration = configuration;
83
84 return NA;
85};
86
87/**
88 * Set private `NA#afterGeneration`.
89 * @public
90 * @function generated
91 * @memberOf NA#
92 * @this NA
93 * @param {function} callback Instruction to execute after generation of generates.
94 * @example require('node-atlas')().generated(function () {
95 * // Update all files on a server...
96 * // or
97 * // Generate a documentation...
98 * // or
99 * // ...
100 * }).run({
101 * generate: true
102 * });
103 * @return {Object} The NA instance for chained functions.
104 */
105exports.generated = function (callback) {
106 var NA = this;
107
108 NA.afterGeneration = callback;
109
110 return NA;
111};
112
113/**
114 * Set private `NA#afterNewProject`.
115 * @public
116 * @function created
117 * @memberOf NA#
118 * @this NA
119 * @param {function} callback Instruction to execute after server was started.
120 * @example require('node-atlas')().created(function () {
121 * // Run a server...
122 * // or
123 * // ...
124 * }).run({
125 * generate: true
126 * });
127 * @return {Object} The NA instance for chained functions.
128 */
129exports.created = function (callback) {
130 var NA = this;
131
132 NA.afterNewProject = callback;
133
134 return NA;
135};
136
137/**
138 * Set private `NA#afterClosing`.
139 * @public
140 * @function stopped
141 * @memberOf NA#
142 * @this NA
143 * @param {function} callback Instruction to execute after server was stopped.
144 * @example require('node-atlas')().stopped(function () {
145 * // Re-run server...
146 * // or
147 * // ...
148 * }).start();
149 * @return {Object} The NA instance for chained functions.
150 */
151exports.stopped = function (callback) {
152 var NA = this;
153
154 NA.afterClosing = callback;
155
156 return NA;
157};
158
159/**
160 * Set private `NA#afterRunning`.
161 * @public
162 * @function started
163 * @memberOf NA#
164 * @this NA
165 * @param {function} callback Instruction to execute after started a webserver.
166 * @example require('node-atlas')().started(function () {
167 * // Run another server...
168 * // or
169 * // ...
170 * }).run({
171 * generate: true
172 * });
173 * @return {Object} The NA instance for chained functions.
174 */
175exports.started = function (callback) {
176 var NA = this;
177
178 NA.afterRunning = callback;
179
180 return NA;
181};
182
183/**
184 * Change the default language used by NodeAtlas CLI and keep it.
185 * @private
186 * @function changeLanguage
187 * @memberOf NA#
188 * @this NA
189 * @param {NA~callback} next Called in all cases.
190 */
191exports.changeLanguage = function (next) {
192 var NA = this,
193 fs = NA.modules.fs,
194 path = NA.modules.path,
195 source = path.join(__dirname, "..", "languages", NA.configuration.lang + ".json"),
196 dest = path.join(__dirname, "..", "languages", NA.cliLanguage + ".json");
197
198 if (NA.configuration.lang) {
199 fs.readFile(source, "utf-8", function (error, file) {
200 var errorMessages = {
201 language: NA.configuration.lang
202 };
203
204 if (error) {
205 NA.log(NA.cliLabels.languageNotFound.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return errorMessages[matches]; }));
206 return next();
207 }
208
209 fs.writeFile(dest, file, function (error) {
210 if (error) {
211 errorMessages.language = "default";
212 NA.log(NA.cliLabels.languageNotFound.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return errorMessages[matches]; }));
213 return next();
214 }
215
216 NA.cliLabels = JSON.parse(file);
217
218 NA.log(NA.cliLabels.languageChanged);
219 next();
220 });
221 });
222 } else {
223
224 /**
225 * Continue after synchronous/asynchronous operations done.
226 * @callback NA~callback
227 */
228 next();
229 }
230};
231
232/**
233 * Catch exit / SIGINT / uncaughtException at the end of program.
234 * @private
235 * @function exit
236 * @memberOf NA#
237 * @this NA
238 */
239exports.exit = function () {
240 var NA = this,
241 limit,
242 separator = "",
243 i,
244 data = {
245 httpPort: NA.webconfig.httpPort
246 },
247 message = NA.cliLabels.closing.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return data[matches]; });
248
249 process.stdin.resume();
250
251 process.on('cleanup', function () {
252 if (NA.afterClosing) {
253 NA.afterClosing.call(NA);
254 }
255 });
256
257 process.on('exit', function () {
258 process.emit('cleanup');
259 });
260
261 process.on('SIGINT', function () {
262 limit = message.length + 2;
263 for (i = 0; i < limit; i++) {
264 separator += "=";
265 }
266 NA.log("");
267 NA.log("\u001B[32m", separator);
268 NA.log(" " + message);
269 NA.log("\u001B[32m", " Ctrl + C");
270 NA.log("\u001B[32m", separator);
271 NA.log("");
272 process.exit(2);
273 });
274
275 process.on('uncaughtException', function (error) {
276 limit = NA.cliLabels.uncaughtException.length + 2;
277 for (i = 0; i < limit; i++) {
278 separator += "=";
279 }
280 NA.log("");
281 NA.log("\u001B[32m", separator);
282 NA.log(" " + NA.cliLabels.uncaughtException);
283 NA.log("\u001B[31m", " " + error.stack);
284 NA.log("\u001B[32m", separator);
285 NA.log("");
286
287 process.exit(99);
288 });
289};
290
291/**
292 * Initialize a NA instance.
293 * @public
294 * @function start
295 * @memberOf NA#
296 * @this NA
297 * @example require('node-atlas')().start();
298 * @return {Object} The NA instance for chained functions.
299 */
300exports.start = function () {
301 var NA = this;
302
303 NA.initNodeModules();
304 NA.initRequiredVars();
305 NA.initNpmModules();
306 NA.initCliConfiguration();
307 NA.initRequiredNpmModulesVars();
308 NA.changeLanguage(function () {
309 NA.createTemplateProject(function () {
310 NA.initWebsite(function () {
311 NA.exit();
312 NA.initServerModules();
313 NA.nodeAtlasWebServer(function () {
314 NA.initStatics();
315 NA.initRoutes();
316 }, function () {
317 NA.initOutputs();
318 });
319 });
320 });
321 });
322
323 return NA;
324};
325
326/**
327 * Close the NA this NA instance.
328 * @public
329 * @function close
330 * @memberOf NA#
331 * @this NA
332 * @example require('node-atlas')().close();
333 * @return {Object} The NA instance for chained functions.
334 */
335exports.close = function () {
336 var NA = this,
337 limit,
338 separator = "",
339 i,
340 data = {
341 httpPort: NA.webconfig.httpPort
342 },
343 message = NA.cliLabels.closing.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return data[matches]; });
344
345 limit = message.length + 2;
346 for (i = 0; i < limit; i++) {
347 separator += "=";
348 }
349
350 NA.log("");
351 NA.log("\u001B[32m", separator);
352 NA.log(" " + message);
353 NA.log("\u001B[32m", " API");
354 NA.log("\u001B[32m", separator);
355 NA.log("");
356
357 NA.server.close();
358
359 return NA;
360};
361
362/**
363 * Execute both `NA#init()` and `NA#start()` functions.
364 * @public
365 * @function run
366 * @memberOf NA#
367 * @this NA
368 * @param {Object} options CLI Parameters as JSON object.
369 * @example require('node-atlas').run({
370 * webconfig: "webconfig.alternatif.json",
371 * httpPort: 7778,
372 * generate: true
373 * });
374 * @return {Object} The NA instance for chained functions.
375 */
376exports.run = function (options) {
377 var NA = this;
378
379 NA.init(options);
380 NA.start();
381
382 return NA;
383};
\No newline at end of file