1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | exports.createTemplateProject = function (next) {
|
15 | var NA = this,
|
16 | fs = NA.modules.fs,
|
17 | path = NA.modules.path,
|
18 | copyDir = NA.modules.copyDir,
|
19 | source = (typeof NA.configuration.create === "string") ? NA.configuration.create : "hello-world",
|
20 | sourcePath = path.join(NA.nodeatlasPath, "templates", source),
|
21 | destinationPath = path.join(NA.serverPath),
|
22 | data = {};
|
23 |
|
24 | if (NA.configuration.create) {
|
25 |
|
26 |
|
27 | if (sourcePath !== destinationPath && fs.existsSync(sourcePath)) {
|
28 |
|
29 | copyDir(sourcePath, destinationPath, function () {
|
30 | data.pathName = sourcePath;
|
31 | NA.log(NA.cliLabels.initCopy.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return data[matches]; }));
|
32 | if (NA.afterNewProject) {
|
33 | NA.afterNewProject.call(NA);
|
34 | } else {
|
35 | process.exit(0);
|
36 | }
|
37 | });
|
38 | } else {
|
39 | data.initPath = source;
|
40 | NA.log(NA.cliLabels.initNotFound.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return data[matches]; }));
|
41 | process.exit(0);
|
42 | }
|
43 | } else {
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 | next();
|
50 | }
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function generateHTML(NA) {
|
61 | var output = false;
|
62 |
|
63 |
|
64 | if (typeof NA.webconfig.output === "boolean") {
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | output = NA.webconfig.output;
|
75 | }
|
76 |
|
77 |
|
78 | if (NA.configuration.generate) {
|
79 |
|
80 |
|
81 | if (output) {
|
82 | NA.forEach(NA.webconfig.routes, function (currentUrl) {
|
83 | NA.prepareResponse(currentUrl, NA.webconfig.routes, undefined, undefined, function (locals, request, response) {
|
84 | NA.changeVariationsCommon(locals, request, response);
|
85 | });
|
86 | });
|
87 | }
|
88 |
|
89 | NA.generateAssets();
|
90 | }
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | exports.initOutputs = function () {
|
101 | var NA = this;
|
102 |
|
103 | if (typeof NA.controllers[NA.webconfig.controller] !== 'undefined' &&
|
104 | typeof NA.controllers[NA.webconfig.controller].setRoutes !== 'undefined') {
|
105 | NA.controllers[NA.webconfig.controller].setRoutes.call(NA, function () {
|
106 | generateHTML(NA);
|
107 | });
|
108 | } else {
|
109 | generateHTML(NA);
|
110 | }
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | exports.generateAssets = function () {
|
121 | var NA = this,
|
122 | async = NA.modules.async;
|
123 |
|
124 |
|
125 | async.parallel([
|
126 | NA.cssCompilation.bind(NA),
|
127 | NA.jsObfuscation.bind(NA)
|
128 | ], function () {
|
129 |
|
130 |
|
131 | async.parallel([
|
132 | NA.publicsGeneration.bind(NA),
|
133 | NA.staticsGeneration.bind(NA),
|
134 | ], function () {
|
135 |
|
136 | if (NA.afterGeneration) {
|
137 | NA.afterGeneration.call(NA);
|
138 | } else {
|
139 | process.exit(0);
|
140 | }
|
141 | });
|
142 | });
|
143 | };
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | exports.publicsGeneration = function (next) {
|
154 | var NA = this,
|
155 | path = NA.modules.path,
|
156 | fs = NA.modules.fs,
|
157 | copyDir = NA.modules.copyDir,
|
158 | sourcePath = path.join(NA.serverPath, NA.webconfig.assetsRelativePath),
|
159 | destinationPath = path.join(NA.serverPath, NA.webconfig.serverlessRelativePath),
|
160 | assetsCopy = false;
|
161 |
|
162 |
|
163 | if (typeof NA.webconfig.assetsCopy === "boolean") {
|
164 |
|
165 | |
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | assetsCopy = NA.webconfig.assetsCopy;
|
174 | }
|
175 |
|
176 | if (assetsCopy && sourcePath !== destinationPath && fs.existsSync(sourcePath)) {
|
177 | copyDir(sourcePath, destinationPath, function () {
|
178 | NA.log(NA.cliLabels.assetsCopy);
|
179 | next();
|
180 | });
|
181 | } else {
|
182 | next();
|
183 | }
|
184 | };
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function traverse(paths, next) {
|
199 | var NA = this,
|
200 | copyDir = NA.modules.copyDir,
|
201 | path = NA.modules.path,
|
202 | sourcePath = path.join(NA.serverPath, paths.real),
|
203 | destinationPath = path.join(NA.serverPath, NA.webconfig.serverlessRelativePath, paths.virtual),
|
204 | data = {
|
205 | source: sourcePath,
|
206 | dest: destinationPath
|
207 | };
|
208 |
|
209 | if (paths.output) {
|
210 | copyDir(sourcePath, destinationPath, function () {
|
211 | NA.log(NA.cliLabels.staticsCopy.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return data[matches]; }));
|
212 | next();
|
213 | });
|
214 | } else {
|
215 | next();
|
216 | }
|
217 | }
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | exports.staticsGeneration = function (next) {
|
228 | var NA = this,
|
229 | async = NA.modules.async,
|
230 | statics = [];
|
231 |
|
232 | if (NA.webconfig.statics) {
|
233 |
|
234 | NA.forEach(NA.webconfig.statics, function (directory, directories) {
|
235 | var virtual = directory,
|
236 | real = directories[directory],
|
237 | output = false;
|
238 |
|
239 | if (NA.webconfig.statics instanceof Array) {
|
240 | virtual = directory.virtual;
|
241 | output = directory.output;
|
242 | real = directory;
|
243 | }
|
244 |
|
245 | if (typeof real === "object") {
|
246 | output = (typeof real.output === "boolean" && !real.output) ? false : true;
|
247 | real = real.path;
|
248 | }
|
249 |
|
250 | statics.push({
|
251 | real: real,
|
252 | virtual: virtual,
|
253 | output: output
|
254 | });
|
255 | });
|
256 |
|
257 | async.map(statics, traverse.bind(NA), function() {
|
258 | next();
|
259 | });
|
260 | } else {
|
261 | next();
|
262 | }
|
263 | };
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | function save(NA, html, data, templateName) {
|
276 | var mkpath = NA.modules.mkpath,
|
277 | path = NA.modules.path,
|
278 | pathToSaveFileComplete = path.join(NA.serverPath, NA.webconfig.serverlessRelativePath, templateName),
|
279 | pathToSaveFile = path.dirname(pathToSaveFileComplete),
|
280 | fs = NA.modules.fs;
|
281 |
|
282 | if (templateName) {
|
283 |
|
284 |
|
285 | mkpath(pathToSaveFile, function () {
|
286 | var dataError = {};
|
287 |
|
288 |
|
289 | if (data.trim().match(/<\/html>$/g) === null) {
|
290 | html = data;
|
291 | }
|
292 |
|
293 | dataError.pathName = path.join(NA.serverPath, NA.webconfig.serverlessRelativePath, templateName);
|
294 |
|
295 |
|
296 | fs.writeFile(pathToSaveFileComplete, html, function (error) {
|
297 | if (error) {
|
298 | if (error.code === 'EISDIR' || error.code === 'ENOENT') {
|
299 | NA.log(NA.cliLabels.templateNotGenerate.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return dataError[matches]; }));
|
300 | } else {
|
301 | throw error;
|
302 | }
|
303 | } else {
|
304 | NA.log(NA.cliLabels.templateGenerate.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return dataError[matches]; }));
|
305 | }
|
306 | });
|
307 |
|
308 | });
|
309 | }
|
310 | }
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | exports.saveRender = function (data, templateName) {
|
322 | var NA = this,
|
323 | jsdom = NA.modules.jsdom,
|
324 | dom = new jsdom.JSDOM(data),
|
325 | deeper,
|
326 | newBase = "";
|
327 |
|
328 | |
329 |
|
330 |
|
331 | if (templateName !== false) {
|
332 |
|
333 | |
334 |
|
335 |
|
336 | templateName = templateName.replace(/\/$/g, "");
|
337 |
|
338 | |
339 |
|
340 |
|
341 |
|
342 | deeper = templateName.split("/").length - 1;
|
343 | if (templateName[0] === "/") {
|
344 | deeper = templateName.split("/").length - 2;
|
345 | }
|
346 |
|
347 |
|
348 | for (var i = 0; i < deeper; i++) {
|
349 | newBase += "../";
|
350 | }
|
351 |
|
352 |
|
353 | Array.prototype.forEach.call(dom.window.document.getElementsByTagName("base"), function (base) {
|
354 | base.setAttribute("href", newBase.replace(/\/$/, ""));
|
355 | });
|
356 |
|
357 |
|
358 | save(NA, dom.serialize(), data, (templateName) ? templateName : "");
|
359 | }
|
360 | }; |
\ | No newline at end of file |