UNPKG

2.66 kBJavaScriptView Raw
1var recursive = require("./util/recursive");
2var path = require("path");
3var Module = require("module");
4var BlockFile = require("./blockfile");
5var CssFile = require("./cssfile");
6var tags = require("./util/tags");
7
8Array.prototype.unique = function() {
9 var a = this.concat();
10 for(var i=0; i<a.length; ++i) {
11 for(var j=i+1; j<a.length; ++j) {
12 if(a[i] === a[j])
13 a.splice(j--, 1);
14 }
15 }
16 return a;
17};
18
19var Pack = function(cfg) {
20
21 var dependencies = [];
22 checkDeps(cfg, dependencies);
23
24 var fileList = [];
25 var d = path.join(cfg.path ,cfg.tahoe.blocksDir||"blocks");
26 recursive(d).forEach(function(f) {
27 fileList.push(f);
28 });
29
30 this.publicDirs = function() {
31 var dirs = [];
32 dependencies.forEach(function(d) {
33 dirs = dirs.concat(d.publicDirs());
34 });
35 dirs.push(path.join(cfg.path, cfg.tahoe.publicDir||"public"));
36 return dirs.unique();
37 };
38
39 this.schema = function(ctx) {
40 dependencies.forEach(function(d) {
41 d.schema(ctx);
42 });
43 fileList.forEach(function(f) {
44 if (f.indexOf(".xml", f.length - 4) == -1) return;
45 var bf = new BlockFile(d, f, ctx);
46 bf.compile(tags.schema);
47 });
48 };
49
50 this.css = function() {
51 var css = "";
52 dependencies.forEach(function(d) {
53 css += d.css();
54 });
55 fileList.forEach(function(f) {
56 if (f.indexOf(".css", f.length - 4) == -1) return;
57 var bf = new CssFile(f);
58 css += bf.compile(null);
59 });
60 return css;
61 };
62
63 this.js = function() {
64 var js = "";
65 dependencies.forEach(function(d) {
66 js += d.js();
67 });
68 fileList.forEach(function(f) {
69 if (f.indexOf(".js", f.length - 3) == -1) return;
70 var jf = new CssFile(f);
71 js += jf.compile();
72 });
73 return js;
74 }
75};
76
77var checkDeps = function(cfg, dependencies) {
78 if (!cfg.tahoe || !cfg.tahoe.dependencies) {
79 return;
80 }
81 var j = module.children.length;
82 for (var i = 0; i < cfg.tahoe.dependencies.length; i++) {
83 var dep = cfg.tahoe.dependencies[i];
84 if (dep.startsWith(".") || dep.startsWith("/")) dep = path.join(cfg.path, dep);
85 dep = path.join(dep, "tahoe.json");
86 var t = require(dep);
87 var p = Module._resolveFilename(dep, module);
88 t.path = path.dirname(p);
89 dependencies.push(new Pack(t));
90 }
91};
92
93module.exports = Pack;
\No newline at end of file