UNPKG

5.26 kBJavaScriptView Raw
1'use strict';
2
3var UTIL = require('util'),
4 PATH = require('./path'),
5 U = require('./util'),
6 LOGGER = require('./logger'),
7 Q = require('qq'),
8 registry = require('./nodesregistry'),
9 level = require('./level'),
10
11 node = require('./nodes/node'),
12 levelNodes = require('./nodes/level'),
13 libNodes = require('./nodes/lib'),
14
15 ArchName = exports.ArchName = 'Arch';
16
17/* jshint -W106 */
18exports.__defineGetter__(ArchName, function() {
19 return registry.getNodeClass(ArchName);
20});
21/* jshint +W106 */
22
23registry.decl('Arch', {
24
25 __constructor: function(arch, opts) {
26 this.arch = arch;
27 this.root = opts.root;
28 this.opts = opts;
29
30 var policy = this.getLevelCachePolicy();
31 level.setCachePolicy(policy.cache, policy.except);
32 },
33
34 bundlesLevelsRegexp: /^(pages.*|bundles.*)/i,
35 blocksLevelsRegexp: /^(blocks.*)/i,
36
37 libraries: {},
38
39 getLibraries: function() {
40 return this.libraries;
41 },
42
43 getLevelCachePolicy: function() {
44 return {
45 cache: false,
46 except: []
47 };
48 },
49
50 alterArch: function() {
51 var _this = this;
52
53 return Q.step(
54 function() {
55 LOGGER.silly('Going to run createCommonNodes()');
56 return Q.call(_this.createCommonNodes, _this);
57 },
58
59 function(common) {
60 LOGGER.silly('Going to run createBlockLibrariesNodes()');
61 return [
62 common,
63 Q.call(_this.createBlockLibrariesNodes, _this, common)
64 ];
65 },
66
67 function(common, libs) {
68 LOGGER.silly('Going to run createBlocksLevelsNodes()');
69 return [
70 common,
71 libs,
72 Q.call(_this.createBlocksLevelsNodes, _this, common, libs)
73 ];
74 },
75
76 function(common, libs, blocks){
77 LOGGER.silly('Going to run createBundlesLevelsNodes()');
78 return [
79 common,
80 libs,
81 blocks,
82 Q.call(_this.createBundlesLevelsNodes, _this, common, (libs || []).concat(blocks))
83 ];
84 },
85
86 function(common, libs, blocks, bundles) {
87 LOGGER.silly('Going to run createCustomNodes()');
88 return Q.call(_this.createCustomNodes, _this, common, libs, blocks, bundles);
89 })
90
91 .then(function() {
92 return _this.opts.inspector && U.snapshotArch(
93 _this.arch,
94 PATH.join(_this.root, '.bem/snapshots/' + UTIL.format('%s_defaultArch alterArch.json', (new Date()-0))));
95 })
96
97 .then(function() {
98 LOGGER.info(_this.arch.toString());
99 return _this.arch;
100 });
101 },
102
103 /* jshint -W098 */
104 createCustomNodes: function(common, libs, blocks, bundles) {
105 // stub
106 },
107 /* jshint +W098 */
108
109 createCommonNodes: function() {
110 var build = new node.Node('build'),
111 all = new node.Node('all');
112
113 this.arch
114 .setNode(all)
115 .setNode(build, all.getId());
116
117 return build.getId();
118 },
119
120 createBlockLibrariesNodes: function(parent) {
121
122 var libs = this.getLibraries();
123 return Object.keys(libs).map(function(l) {
124
125 var lib = libs[l],
126 libNodeClass = U.toUpperCaseFirst(lib.type) + libNodes.LibraryNodeName,
127 libNode = new (registry.getNodeClass(libNodeClass))(U.extend({}, lib, {
128 root: this.root,
129 target: l
130 }));
131
132 this.arch.setNode(libNode, parent);
133 return libNode.getId();
134
135 }, this);
136
137 },
138
139 createBlocksLevelsNodes: function(parent, children) {
140
141 return this.createLevelsNodes(
142 this.getBlocksLevels(this.root),
143 levelNodes.LevelNode,
144 parent,
145 children);
146
147 },
148
149 createBundlesLevelsNodes: function(parent, children) {
150
151 return this.createLevelsNodes(
152 this.getBundlesLevels(this.root),
153 levelNodes.BundlesLevelNode,
154 parent,
155 children);
156
157 },
158
159 createLevelsNodes: function(levels, NodeClass, parent, children) {
160
161 var _this = this;
162
163 return Q.when(levels)
164 .then(function(levels) {
165
166 return levels.map(function(level) {
167 var node = new NodeClass({
168 root: _this.root,
169 level: level
170 });
171
172 _this.arch.setNode(node, parent, children);
173
174 return node.getId();
175 });
176
177 });
178
179 },
180
181 getBlocksLevels: function(from) {
182 return this.getLevels(from, this.blocksLevelsRegexp);
183 },
184
185 getBundlesLevels: function(from) {
186 return this.getLevels(from, this.bundlesLevelsRegexp);
187 },
188
189 getLevels: function(from, mask) {
190
191 return U.getDirsAsync(from)
192 .invoke('filter', function(dir) {
193 return dir.match(mask);
194 });
195
196 }
197
198});