diff --git a/lib/toaster.js b/lib/toaster.js index 171a294..897f448 100644 --- a/lib/toaster.js +++ b/lib/toaster.js @@ -522,22 +522,25 @@ Builder.prototype._build_ns_tree = function(tree, folderpath) { - var folder, folders, include, item, _i, _j, _len, _len1, _ref, _results; + var file, include, item, _i, _j, _len, _len1, _ref, _ref1, _results; if (!fs.lstatSync(folderpath).isDirectory()) { return; } - folders = fsu.ls(folderpath); + _ref = fsu.ls(folderpath); _results = []; - for (_i = 0, _len = folders.length; _i < _len; _i++) { - folder = folders[_i]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + file = _ref[_i]; + if (!fs.lstatSync(file).isDirectory()) { + continue; + } include = true; - _ref = this.exclude; - for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { - item = _ref[_j]; - include &= !(new RegExp(item).test(folder)); + _ref1 = this.exclude; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + item = _ref1[_j]; + include &= !(new RegExp(item).test(file)); } if (include) { - _results.push(this._build_ns_tree((tree[folder.match(/[^\/\\]+$/m)] = {}), folder)); + _results.push(this._build_ns_tree((tree[file.match(/[^\/\\]+$/m)] = {}), file)); } else { _results.push(void 0); } diff --git a/src/toaster/core/builder.coffee b/src/toaster/core/builder.coffee index 1bc1373..6ab2a96 100644 --- a/src/toaster/core/builder.coffee +++ b/src/toaster/core/builder.coffee @@ -107,7 +107,7 @@ class Builder tree = {} for folder in @config.src_folders t = (tree[folder.alias] = {}) if folder.alias? - @_build_ns_tree t || tree, folder.path + @_build_ns_tree(t || tree, folder.path) buffer = "" for name, scope of tree @@ -123,13 +123,13 @@ class Builder ### _build_ns_tree: (tree, folderpath) -> return unless fs.lstatSync(folderpath).isDirectory() - folders = fsu.ls(folderpath) - for folder in folders + for file in fsu.ls(folderpath) + continue unless fs.lstatSync(file).isDirectory() include = true for item in @exclude - include &= !(new RegExp( item ).test folder) + include &= !(new RegExp(item).test file) if include - @_build_ns_tree (tree[folder.match /[^\/\\]+$/m] = {}), folder + @_build_ns_tree((tree[file.match /[^\/\\]+$/m] = {}), file) ### @return {String} diff --git a/test/coverage.html b/test/coverage.html index d74119c..dba6661 100644 --- a/test/coverage.html +++ b/test/coverage.html @@ -338,4 +338,4 @@ code .string { color: #5890AD } code .keyword { color: #8A6343 } code .number { color: #2F6FAD } -
Line | Hits | Source |
---|---|---|
1 | 1 | (function() { |
2 | 1 | var ArrayUtil, Builder, Script, Toast, Toaster, error, log, warn, |
3 | 6 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; |
4 | ||
5 | 1 | Toast = (function() { |
6 | 1 | var fs, path; |
7 | ||
8 | 1 | fs = require("fs"); |
9 | ||
10 | 1 | path = require("path"); |
11 | ||
12 | 1 | Toast.prototype.basepath = null; |
13 | ||
14 | 1 | Toast.prototype.builders = null; |
15 | ||
16 | 1 | function Toast(basepath, options) { |
17 | 3 | var item, _i, _len, _ref; |
18 | 3 | this.basepath = basepath; |
19 | 3 | this._toast = __bind(this._toast, this); |
20 | 3 | this.builders = []; |
21 | 3 | _ref = [].concat(options); |
22 | 3 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
23 | 3 | item = _ref[_i]; |
24 | 3 | this._toast(item); |
25 | } | |
26 | } | |
27 | ||
28 | 1 | Toast.prototype._toast = function(srcpath, params) { |
29 | 3 | var alias, builder, config, folder, i, item, v, vpath, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9; |
30 | 3 | if (params == null) { |
31 | 3 | params = {}; |
32 | } | |
33 | 3 | if (srcpath instanceof Object) { |
34 | 3 | params = srcpath; |
35 | 0 | } else if ((path.resolve(srcpath)) !== srcpath) { |
36 | 0 | folder = path.join(this.basepath, srcpath); |
37 | } | |
38 | 3 | if (params.release == null) { |
39 | 0 | error('Release path not informed in config.'); |
40 | 0 | return process.exit(); |
41 | } | |
42 | 3 | if (!fs.existsSync(path.join(this.basepath, path.dirname(params.release)))) { |
43 | 0 | error("Release folder does not exist:\n\t" + dir.yellow); |
44 | 0 | return process.exit(); |
45 | } | |
46 | 3 | config = { |
47 | is_building: false, | |
48 | basepath: this.basepath, | |
49 | src_folders: [], | |
50 | files: [], | |
51 | vendors: (_ref = params.vendors) != null ? _ref : [], | |
52 | exclude: (_ref1 = params.exclude) != null ? _ref1 : [], | |
53 | bare: (_ref2 = params.bare) != null ? _ref2 : false, | |
54 | packaging: (_ref3 = params.packaging) != null ? _ref3 : true, | |
55 | expose: (_ref4 = params.expose) != null ? _ref4 : null, | |
56 | minify: (_ref5 = params.minify) != null ? _ref5 : true, | |
57 | callback: (_ref6 = params.callback) != null ? _ref6 : null, | |
58 | release: path.join(this.basepath, params.release) | |
59 | }; | |
60 | 3 | _ref7 = config.vendors; |
61 | 3 | for (i = _i = 0, _len = _ref7.length; _i < _len; i = ++_i) { |
62 | 3 | v = _ref7[i]; |
63 | 3 | vpath = config.vendors[i] = path.resolve(v); |
64 | 3 | if ((path.resolve(vpath)) !== vpath) { |
65 | 0 | config.vendors[i] = path.join(this.basepath, v); |
66 | } | |
67 | } | |
68 | 3 | if (!(srcpath instanceof Object)) { |
69 | 0 | srcpath = path.resolve(path.join(this.basepath, srcpath)); |
70 | 0 | config.src_folders.push({ |
71 | path: srcpath, | |
72 | alias: params.alias || null | |
73 | }); | |
74 | } | |
75 | 3 | if (params.folders != null) { |
76 | 3 | _ref8 = params.folders; |
77 | 3 | for (folder in _ref8) { |
78 | 3 | alias = _ref8[folder]; |
79 | 3 | if ((path.resolve(folder)) !== folder) { |
80 | 3 | folder = path.join(this.basepath, folder); |
81 | } | |
82 | 3 | config.src_folders.push({ |
83 | path: folder, | |
84 | alias: alias | |
85 | }); | |
86 | } | |
87 | } | |
88 | 3 | _ref9 = config.src_folders; |
89 | 3 | for (_j = 0, _len1 = _ref9.length; _j < _len1; _j++) { |
90 | 3 | item = _ref9[_j]; |
91 | 3 | if (!fs.existsSync(item.path)) { |
92 | 0 | error(("Source folder doens't exist:\n\t" + item.path.red + "\n") + ("Check your " + 'toaster.coffee'.yellow + " and try again.") + "\n\t" + (path.join(this.basepath, "toaster.coffee")).yellow); |
93 | 0 | return process.exit(); |
94 | } | |
95 | } | |
96 | 3 | builder = new Builder(this.basepath, config); |
97 | 3 | return this.builders.push(builder); |
98 | }; | |
99 | ||
100 | 1 | return Toast; |
101 | ||
102 | })(); | |
103 | ||
104 | 1 | ArrayUtil = (function() { |
105 | 1 | function ArrayUtil() {} |
106 | ||
107 | /* | |
108 | @param {Array} source | |
109 | @param {Object} search | |
110 | @param {String} by_property | |
111 | @return {Boolean} | |
112 | */ | |
113 | ||
114 | ||
115 | 1 | ArrayUtil.has = function(source, search, by_property) { |
116 | 3 | return ArrayUtil.find(source, search, by_property) != null; |
117 | }; | |
118 | ||
119 | /* | |
120 | @param {Array} source | |
121 | @param {Object} search | |
122 | @param {String} by_property | |
123 | @return {Object} | |
124 | */ | |
125 | ||
126 | ||
127 | 1 | ArrayUtil.find = function(source, search, by_property) { |
128 | 39 | var k, p, v, _i, _j, _k, _len, _len1, _len2; |
129 | 39 | if (!by_property) { |
130 | 3 | for (k = _i = 0, _len = source.length; _i < _len; k = ++_i) { |
131 | 0 | v = source[k]; |
132 | 0 | if (v === search) { |
133 | 0 | return { |
134 | item: v, | |
135 | index: k | |
136 | }; | |
137 | } | |
138 | } | |
139 | } else { | |
140 | 36 | by_property = [].concat(by_property); |
141 | 36 | for (k = _j = 0, _len1 = source.length; _j < _len1; k = ++_j) { |
142 | 66 | v = source[k]; |
143 | 66 | for (_k = 0, _len2 = by_property.length; _k < _len2; _k++) { |
144 | 66 | p = by_property[_k]; |
145 | 66 | if (search === v[p]) { |
146 | 36 | return { |
147 | item: v, | |
148 | index: k | |
149 | }; | |
150 | } | |
151 | } | |
152 | } | |
153 | } | |
154 | 3 | return null; |
155 | }; | |
156 | ||
157 | /* | |
158 | @param {Array} source | |
159 | @param {Object} search | |
160 | @param {String} by_property | |
161 | @param {Boolean} regexp | |
162 | @param {Boolean} unique | |
163 | @return {Object} | |
164 | */ | |
165 | ||
166 | ||
167 | 1 | ArrayUtil.find_all = function(source, search, by_property, regexp, unique) { |
168 | 3 | var item, k, p, v, _i, _j, _k, _len, _len1, _len2, _output, _unique; |
169 | 3 | _output = []; |
170 | 3 | _unique = {}; |
171 | 3 | if (by_property === null) { |
172 | 0 | for (k = _i = 0, _len = source.length; _i < _len; k = ++_i) { |
173 | 0 | v = source[k]; |
174 | 0 | if (regexp) { |
175 | 0 | if (search.test(v)) { |
176 | 0 | item = { |
177 | item: v, | |
178 | index: k | |
179 | }; | |
180 | } | |
181 | } else { | |
182 | 0 | if (search === v) { |
183 | 0 | item = { |
184 | item: v, | |
185 | index: k | |
186 | }; | |
187 | } | |
188 | } | |
189 | 0 | if (item) { |
190 | 0 | _output.push(item); |
191 | } | |
192 | } | |
193 | } else { | |
194 | 3 | by_property = [].concat(by_property); |
195 | 3 | for (k = _j = 0, _len1 = source.length; _j < _len1; k = ++_j) { |
196 | 9 | v = source[k]; |
197 | 9 | for (_k = 0, _len2 = by_property.length; _k < _len2; _k++) { |
198 | 9 | p = by_property[_k]; |
199 | 9 | item = null; |
200 | 9 | if (regexp) { |
201 | 9 | if (search.test(v[p])) { |
202 | 9 | if (unique && !_unique[k]) { |
203 | 9 | item = { |
204 | item: v, | |
205 | index: k | |
206 | }; | |
207 | 0 | } else if (unique === !true) { |
208 | 0 | item = { |
209 | item: v, | |
210 | index: k | |
211 | }; | |
212 | } | |
213 | } | |
214 | } else { | |
215 | 0 | if (search === v[p]) { |
216 | 0 | item = { |
217 | item: v, | |
218 | index: k | |
219 | }; | |
220 | } | |
221 | } | |
222 | 9 | if (item) { |
223 | 9 | _unique[k] = true; |
224 | 9 | _output.push(item); |
225 | } | |
226 | } | |
227 | } | |
228 | } | |
229 | 3 | return _output; |
230 | }; | |
231 | ||
232 | 1 | return ArrayUtil; |
233 | ||
234 | })(); | |
235 | ||
236 | 1 | Script = (function() { |
237 | 1 | var cs, fs, path; |
238 | ||
239 | 1 | fs = require("fs"); |
240 | ||
241 | 1 | path = require("path"); |
242 | ||
243 | 1 | cs = require("coffee-script"); |
244 | ||
245 | 1 | Script.prototype.builder = null; |
246 | ||
247 | 1 | Script.prototype.folderpath = null; |
248 | ||
249 | 1 | Script.prototype.realpath = null; |
250 | ||
251 | 1 | Script.prototype.alias = null; |
252 | ||
253 | 1 | Script.prototype.opts = null; |
254 | ||
255 | 1 | Script.prototype.dependencies_collapsed = null; |
256 | ||
257 | 1 | Script.prototype.raw = null; |
258 | ||
259 | 1 | Script.prototype.baseclasses = null; |
260 | ||
261 | 1 | Script.prototype.filepath = null; |
262 | ||
263 | 1 | Script.prototype.filename = null; |
264 | ||
265 | 1 | Script.prototype.filefolder = null; |
266 | ||
267 | 1 | Script.prototype.namespace = null; |
268 | ||
269 | 1 | function Script(builder, folderpath, realpath, alias) { |
270 | 9 | this.builder = builder; |
271 | 9 | this.folderpath = folderpath; |
272 | 9 | this.realpath = realpath; |
273 | 9 | this.alias = alias; |
274 | 9 | this._getinfo(); |
275 | } | |
276 | ||
277 | /* | |
278 | @return {Array} | |
279 | */ | |
280 | ||
281 | ||
282 | 1 | Script.prototype.expand_dependencies = function() { |
283 | 9 | var dependency, expanded, files, found, index, reg, _i, _j, _len, _len1, _ref; |
284 | 9 | files = this.builder.files; |
285 | 9 | this.dependencies = []; |
286 | 9 | _ref = this.dependencies_collapsed; |
287 | 9 | for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { |
288 | 6 | dependency = _ref[index]; |
289 | 6 | if ((dependency.substr(0, 1)) === path.sep) { |
290 | 0 | dependency = dependency.substr(1); |
291 | } | |
292 | 6 | if (dependency.substr(-1) !== "*") { |
293 | 3 | this.dependencies.push("" + path.sep + dependency + ".coffee"); |
294 | 3 | continue; |
295 | } | |
296 | 3 | reg = new RegExp(dependency.replace(/(\/|\\)/g, "\\$1")); |
297 | 3 | found = ArrayUtil.find_all(files, reg, "filepath", true, true); |
298 | 3 | if (found.length <= 0) { |
299 | 0 | warn(("Nothing found inside " + ("'" + dependency).white + "'").yellow); |
300 | 0 | continue; |
301 | } | |
302 | 3 | for (_j = 0, _len1 = found.length; _j < _len1; _j++) { |
303 | 9 | expanded = found[_j]; |
304 | 9 | if (expanded.item.filepath !== this.filepath) { |
305 | 6 | this.dependencies.push(expanded.item.filepath); |
306 | } | |
307 | } | |
308 | } | |
309 | 9 | return this.dependencies; |
310 | }; | |
311 | ||
312 | /* | |
313 | @param {Boolean} declare_ns | |
314 | */ | |
315 | ||
316 | ||
317 | 1 | Script.prototype._getinfo = function(declare_ns) { |
318 | 9 | var baseclass, decl, extending, item, klass, name, repl, requirements, rgx, rgx_ext, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _results; |
319 | 9 | if (declare_ns == null) { |
320 | 9 | declare_ns = true; |
321 | } | |
322 | 9 | this.raw = fs.readFileSync(this.realpath, "utf-8"); |
323 | 9 | this.dependencies_collapsed = []; |
324 | 9 | this.baseclasses = []; |
325 | 9 | this.filepath = this.realpath.replace(this.folderpath, ''); |
326 | 9 | if (this.alias != null) { |
327 | 9 | this.filepath = path.join(path.sep, this.alias, this.filepath); |
328 | } | |
329 | 9 | this.filename = path.basename(this.filepath); |
330 | 9 | this.filefolder = path.dirname(this.filepath); |
331 | 9 | this.namespace = ""; |
332 | 9 | if (this.filepath.indexOf(path.sep) === -1) { |
333 | 0 | this.filefolder = ""; |
334 | } | |
335 | 9 | this.namespace = this.filefolder.replace(new RegExp("\\" + path.sep, "g"), "."); |
336 | 9 | this.namespace = this.namespace.replace(/^\.?(.*)\.?$/g, "$1"); |
337 | 9 | rgx = /^(class)+\s+([^\s]+)+(\s(extends)\s+([\w.]+))?/mg; |
338 | 9 | rgx_ext = /(^|=\s*)(class)\s(\w+)\s(extends)\s(\\w+)\s*$/gm; |
339 | 9 | if (this.raw.match(rgx) != null) { |
340 | 9 | if (this.namespace !== "" && this.builder.packaging) { |
341 | 0 | _ref = [].concat(this.raw.match(rgx)); |
342 | 0 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
343 | 0 | decl = _ref[_i]; |
344 | 0 | name = decl.match(/class\s([^\s]+)/); |
345 | 0 | name = (name[1].split('.')).pop(); |
346 | 0 | extending = decl.match(/(\sextends\s[^\s]+$)/m); |
347 | 0 | if (extending) { |
348 | 0 | extending = extending[0]; |
349 | } | |
350 | 0 | extending || (extending = ""); |
351 | 0 | repl = "class " + this.namespace + "." + name + extending; |
352 | 0 | if (decl !== repl) { |
353 | 0 | this.raw = this.raw.replace(decl, repl); |
354 | 0 | fs.writeFileSync(this.realpath, this.raw); |
355 | } | |
356 | } | |
357 | 0 | this.classpath = "" + this.namespace + "." + this.classname; |
358 | } | |
359 | 9 | this.classname = this.raw.match(rgx)[3]; |
360 | 9 | _ref2 = (_ref1 = this.raw.match(rgx_ext)) != null ? _ref1 : []; |
361 | 9 | for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) { |
362 | 0 | klass = _ref2[_j]; |
363 | 0 | baseclass = klass.match(rgx_ext)[5]; |
364 | 0 | this.baseclasses.push(baseclass); |
365 | } | |
366 | } | |
367 | 9 | if (/(#<<\s)(.*)/g.test(this.raw)) { |
368 | 6 | requirements = this.raw.match(/(#<<\s)(.*)/g); |
369 | 6 | _results = []; |
370 | 6 | for (_k = 0, _len2 = requirements.length; _k < _len2; _k++) { |
371 | 6 | item = requirements[_k]; |
372 | 6 | item = /(#<<\s)(.*)/.exec(item)[2]; |
373 | 6 | item = item.replace(/\s/g, ""); |
374 | 6 | if (path.sep === "\\") { |
375 | 0 | item = item.replace(/(\/)/g, "\\"); |
376 | } | |
377 | 6 | _results.push(this.dependencies_collapsed = this.dependencies_collapsed.concat(item)); |
378 | } | |
379 | 6 | return _results; |
380 | } | |
381 | }; | |
382 | ||
383 | 1 | return Script; |
384 | ||
385 | })(); | |
386 | ||
387 | 1 | Builder = (function() { |
388 | 1 | var cs, fs, fsu, path, uglify; |
389 | ||
390 | 1 | fs = require('fs'); |
391 | ||
392 | 1 | path = require('path'); |
393 | ||
394 | 1 | fsu = require('fs-util'); |
395 | ||
396 | 1 | cs = require("coffee-script"); |
397 | ||
398 | 1 | uglify = require("uglify-js"); |
399 | ||
400 | 1 | Builder.prototype.missing = {}; |
401 | ||
402 | 1 | function Builder(basepath, config) { |
403 | 3 | var falias, file, folder, fpath, include, item, result, s, _i, _j, _k, _len, _len1, _len2, _ref, _ref1; |
404 | 3 | this.basepath = basepath; |
405 | 3 | this.config = config; |
406 | 3 | this.exclude = [].concat(this.config.exclude); |
407 | 3 | this.files = this.config.files; |
408 | 3 | _ref = this.config.src_folders; |
409 | 3 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
410 | 3 | folder = _ref[_i]; |
411 | 3 | result = fsu.find(folder.path, /.coffee$/m); |
412 | 3 | fpath = folder.path; |
413 | 3 | falias = folder.alias; |
414 | 3 | for (_j = 0, _len1 = result.length; _j < _len1; _j++) { |
415 | 9 | file = result[_j]; |
416 | 9 | include = true; |
417 | 9 | _ref1 = this.exclude; |
418 | 9 | for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) { |
419 | 0 | item = _ref1[_k]; |
420 | 0 | include &= !(new RegExp(item).test(file)); |
421 | } | |
422 | 9 | if (include) { |
423 | 9 | s = new Script(this, fpath, file, falias); |
424 | 9 | this.files.push(s); |
425 | } | |
426 | } | |
427 | } | |
428 | } | |
429 | ||
430 | 1 | Builder.prototype.build = function(header_code, footer_code) { |
431 | 3 | var contents, namespaces, options, vendors; |
432 | 3 | if (header_code == null) { |
433 | 0 | header_code = ""; |
434 | } | |
435 | 3 | if (footer_code == null) { |
436 | 0 | footer_code = ""; |
437 | } | |
438 | 3 | namespaces = this._build_namespaces(); |
439 | 3 | vendors = this._merge_vendors(); |
440 | 3 | contents = []; |
441 | 3 | if (this.config.packaging) { |
442 | 3 | contents.push(namespaces); |
443 | } | |
444 | 3 | if (header_code !== "") { |
445 | 0 | contents.push(header_code); |
446 | } | |
447 | 3 | contents.push(this._compile()); |
448 | 3 | if (header_code !== "") { |
449 | 0 | contents.push(footer_code); |
450 | } | |
451 | 3 | contents = contents.join('\n'); |
452 | 3 | if (this.config.minify !== false) { |
453 | 2 | if (this.config.minify === true) { |
454 | 0 | options = {}; |
455 | } else { | |
456 | 2 | options = this.config.minify; |
457 | } | |
458 | 2 | options.fromString = true; |
459 | 2 | contents = uglify.minify(contents, options).code; |
460 | } | |
461 | 3 | if (this.config.callback != null) { |
462 | 1 | contents = this.config.callback.apply(this, [contents, this.config.minify !== false]); |
463 | } | |
464 | 3 | if (vendors !== "") { |
465 | 3 | contents = vendors + '\n' + contents; |
466 | } | |
467 | 3 | return fs.writeFileSync(this.config.release, contents); |
468 | }; | |
469 | ||
470 | /* | |
471 | @return {String} | |
472 | */ | |
473 | ||
474 | ||
475 | 1 | Builder.prototype._merge_vendors = function() { |
476 | 3 | var buffer, vendor, _i, _len, _ref; |
477 | 3 | buffer = []; |
478 | 3 | _ref = this.config.vendors; |
479 | 3 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
480 | 3 | vendor = _ref[_i]; |
481 | 3 | if (fs.existsSync(vendor)) { |
482 | 3 | buffer.push(fs.readFileSync(vendor, 'utf-8')); |
483 | } else { | |
484 | 0 | warn("Vendor not found at ".white + vendor.yellow.bold); |
485 | } | |
486 | } | |
487 | 3 | return buffer.join("\n"); |
488 | }; | |
489 | ||
490 | /* | |
491 | Creates a NS holder for all folders | |
492 | */ | |
493 | ||
494 | ||
495 | 1 | Builder.prototype._build_namespaces = function() { |
496 | 3 | var buffer, folder, name, scope, t, tree, _i, _len, _ref; |
497 | 3 | tree = {}; |
498 | 3 | _ref = this.config.src_folders; |
499 | 3 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
500 | 3 | folder = _ref[_i]; |
501 | 3 | if (folder.alias != null) { |
502 | 3 | t = (tree[folder.alias] = {}); |
503 | } | |
504 | 3 | this._build_ns_tree(t || tree, folder.path); |
505 | } | |
506 | 3 | buffer = ""; |
507 | 3 | for (name in tree) { |
508 | 3 | scope = tree[name]; |
509 | 3 | buffer += "var " + name + " = "; |
510 | 3 | if (this.config.expose != null) { |
511 | 0 | buffer += "" + this.config.expose + "." + name + " = "; |
512 | } | |
513 | 3 | buffer += (JSON.stringify(scope, null, '')).replace(/\"/g, "'"); |
514 | 3 | buffer += ";\n"; |
515 | } | |
516 | 3 | return buffer; |
517 | }; | |
518 | ||
519 | /* | |
520 | Walk some folderpath and lists all its subfolders | |
521 | */ | |
522 | ||
523 | ||
524 | 1 | Builder.prototype._build_ns_tree = function(tree, folderpath) { |
525 | 6 | var folder, folders, include, item, _i, _j, _len, _len1, _ref, _results; |
526 | 6 | if (!fs.lstatSync(folderpath).isDirectory()) { |
527 | 0 | return; |
528 | } | |
529 | 6 | folders = fsu.ls(folderpath); |
530 | 6 | _results = []; |
531 | 6 | for (_i = 0, _len = folders.length; _i < _len; _i++) { |
532 | 3 | folder = folders[_i]; |
533 | 3 | include = true; |
534 | 3 | _ref = this.exclude; |
535 | 3 | for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { |
536 | 0 | item = _ref[_j]; |
537 | 0 | include &= !(new RegExp(item).test(folder)); |
538 | } | |
539 | 3 | if (include) { |
540 | 3 | _results.push(this._build_ns_tree((tree[folder.match(/[^\/\\]+$/m)] = {}), folder)); |
541 | } else { | |
542 | 0 | _results.push(void 0); |
543 | } | |
544 | } | |
545 | 6 | return _results; |
546 | }; | |
547 | ||
548 | /* | |
549 | @return {String} | |
550 | */ | |
551 | ||
552 | ||
553 | 1 | Builder.prototype._compile = function() { |
554 | 3 | var err, file, msg, output, _i, _j, _len, _len1, _ref, _ref1; |
555 | 3 | _ref = this.files; |
556 | 3 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
557 | 9 | file = _ref[_i]; |
558 | 9 | try { |
559 | 9 | cs.compile(file.raw); |
560 | } catch (_error) { | |
561 | 0 | err = _error; |
562 | 0 | msg = err.message.replace('"', '\\"'); |
563 | 0 | msg = ("" + msg.white + " at file: ") + ("" + file.filepath).bold.red; |
564 | 0 | error(msg); |
565 | 0 | return null; |
566 | } | |
567 | } | |
568 | 3 | _ref1 = this.files; |
569 | 3 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { |
570 | 9 | file = _ref1[_j]; |
571 | 9 | file.expand_dependencies(); |
572 | } | |
573 | 3 | this._reorder(); |
574 | 3 | output = ((function() { |
575 | 3 | var _k, _len2, _ref2, _results; |
576 | 3 | _ref2 = this.files; |
577 | 3 | _results = []; |
578 | 3 | for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { |
579 | 9 | file = _ref2[_k]; |
580 | 9 | _results.push(file.raw); |
581 | } | |
582 | 3 | return _results; |
583 | }).call(this)).join("\n"); | |
584 | 3 | return output = cs.compile(output, { |
585 | bare: this.config.bare | |
586 | }); | |
587 | }; | |
588 | ||
589 | /* | |
590 | @param {Boolean} cycling | |
591 | */ | |
592 | ||
593 | ||
594 | 1 | Builder.prototype._reorder = function(cycling) { |
595 | 6 | var bc, dependency, dependency_index, file, file_index, filepath, found, i, index, not_found, _i, _j, _len, _len1, _ref, _ref1, _results; |
596 | 6 | if (cycling == null) { |
597 | 3 | cycling = false; |
598 | } | |
599 | 6 | if (cycling === false) { |
600 | 3 | this.missing = {}; |
601 | } | |
602 | 6 | _ref = this.files; |
603 | 6 | _results = []; |
604 | 6 | for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { |
605 | 18 | file = _ref[i]; |
606 | 18 | if (!file.dependencies.length && !file.baseclasses.length) { |
607 | 3 | continue; |
608 | } | |
609 | 15 | _ref1 = file.dependencies; |
610 | 15 | for (index = _j = 0, _len1 = _ref1.length; _j < _len1; index = ++_j) { |
611 | 21 | filepath = _ref1[index]; |
612 | 21 | dependency = ArrayUtil.find(this.files, filepath, "filepath"); |
613 | 21 | if (dependency != null) { |
614 | 21 | dependency_index = dependency.index; |
615 | } | |
616 | 21 | if (dependency_index < i && (dependency != null)) { |
617 | 18 | continue; |
618 | } | |
619 | 3 | if (dependency != null) { |
620 | 3 | if (ArrayUtil.has(dependency.item.dependencies, file.filepath)) { |
621 | 0 | file.dependencies.splice(index, 1); |
622 | 0 | warn("Circular dependency found between ".yellow + filepath.grey.bold + " and ".yellow + file.filepath.grey.bold); |
623 | 0 | continue; |
624 | } else { | |
625 | 3 | this.files.splice(index, 0, dependency.item); |
626 | 3 | this.files.splice(dependency.index + 1, 1); |
627 | 3 | this._reorder(true); |
628 | 3 | break; |
629 | } | |
630 | 0 | } else if (this.missing[filepath] !== true) { |
631 | 0 | this.missing[filepath] = true; |
632 | 0 | file.dependencies.push(filepath); |
633 | 0 | file.dependencies.splice(index, 1); |
634 | 0 | warn("" + 'Dependency'.yellow + " " + filepath.bold.grey + " " + 'not found for file'.yellow + " " + file.filepath.grey.bold); |
635 | } | |
636 | } | |
637 | 15 | file_index = ArrayUtil.find(this.files, file.filepath, "filepath"); |
638 | 15 | file_index = file_index.index; |
639 | 15 | _results.push((function() { |
640 | 15 | var _k, _len2, _ref2, _results1; |
641 | 15 | _ref2 = file.baseclasses; |
642 | 15 | _results1 = []; |
643 | 15 | for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { |
644 | 0 | bc = _ref2[_k]; |
645 | 0 | found = ArrayUtil.find(this.files, bc, "classname"); |
646 | 0 | not_found = (found === null) || (found.index > file_index); |
647 | 0 | if (not_found && !this.missing[bc]) { |
648 | 0 | this.missing[bc] = true; |
649 | 0 | _results1.push(warn("Base class ".yellow + ("" + bc + " ").bold.grey + "not found for class ".yellow + ("" + file.classname + " ").bold.grey + "in file ".yellow + file.filepath.bold.grey)); |
650 | } else { | |
651 | 0 | _results1.push(void 0); |
652 | } | |
653 | } | |
654 | 15 | return _results1; |
655 | }).call(this)); | |
656 | } | |
657 | 6 | return _results; |
658 | }; | |
659 | ||
660 | 1 | return Builder; |
661 | ||
662 | })(); | |
663 | ||
664 | 1 | module.exports = Toaster = (function() { |
665 | 1 | var path; |
666 | ||
667 | 1 | path = require("path"); |
668 | ||
669 | /* | |
670 | @var {Cli} | |
671 | */ | |
672 | ||
673 | ||
674 | 1 | Toaster.prototype.options = null; |
675 | ||
676 | /* | |
677 | @var {Object} before filter container | |
678 | */ | |
679 | ||
680 | ||
681 | 1 | Toaster.prototype.before_build = null; |
682 | ||
683 | 1 | function Toaster(basedir, options) { |
684 | 3 | this.toast = new Toast(path.resolve(basedir), options); |
685 | } | |
686 | ||
687 | 1 | Toaster.prototype.build = function(header_code, footer_code) { |
688 | 3 | var builder, _i, _len, _ref, _results; |
689 | 3 | if (header_code == null) { |
690 | 3 | header_code = ""; |
691 | } | |
692 | 3 | if (footer_code == null) { |
693 | 3 | footer_code = ""; |
694 | } | |
695 | 3 | _ref = this.toast.builders; |
696 | 3 | _results = []; |
697 | 3 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
698 | 3 | builder = _ref[_i]; |
699 | 3 | _results.push(builder.build(header_code, footer_code)); |
700 | } | |
701 | 3 | return _results; |
702 | }; | |
703 | ||
704 | 1 | return Toaster; |
705 | ||
706 | })(); | |
707 | ||
708 | 1 | log = function(msg) { |
709 | 0 | console.log(msg); |
710 | 0 | return msg; |
711 | }; | |
712 | ||
713 | 1 | error = function(msg) { |
714 | 0 | msg = log("ERROR ".bold.red + msg); |
715 | 0 | return msg; |
716 | }; | |
717 | ||
718 | 1 | warn = function(msg) { |
719 | 0 | msg = log("WARNING ".bold.yellow + msg); |
720 | 0 | return msg; |
721 | }; | |
722 | ||
723 | }).call(this); | |
724 |