-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.js
165 lines (131 loc) · 4.75 KB
/
builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var fs = require('fs'),
stylus = require('stylus'),
nib = require('nib'),
Builder;
Builder = function () {
var _this = this;
this.options = {};
this.configClient = require('./configs/config.client');
this.clientFolder = '/client';
this.clientPath = process.cwd() + this.clientFolder;
this.folders = [
'/vendors',
'/layouts',
'/pages',
'/blocks'
]
this.render = {
styl: function (filePath) {
var dirPath = filePath.slice(0, filePath.lastIndexOf('/')),
str = fs.readFileSync(_this.clientPath + filePath, 'utf8');
return stylus(str)
.use(nib())
.import('nib')
.include(_this.clientPath + '/_addons/stylus/')
.import('variables')
.import('mixins')
.define('$dir-path', _this.options.env == 'production' ? '' : dirPath)
.render();
},
js: function (filePath) {
return fs.readFileSync(_this.clientPath + filePath, 'utf8');
}
};
}
Builder.prototype.setOptions = function (options) {
this.options = options;
}
Builder.prototype.collect = function (arr, collection) {
var _this = this,
collection = collection || {};
arr.forEach(function (name) {
if (_this.configClient[name] && !collection[name]) {
var item = _this.configClient[name];
collection[name] = item;
if (item.deps) {
collection = _this.collect(item.deps, collection);
}
}
});
return collection;
};
Builder.prototype.getDeps = function (include, exclude) {
var _this = this,
include = (include instanceof Array) ? include : [include],
includeCollection = this.collect(include),
excludeCollection = {},
files = {
styles: {},
scripts: {},
views: {},
img: {}
},
reUrl = /^((https?:)?\/\/)/;
if (exclude) {
exclude = (exclude instanceof Array) ? exclude : [exclude];
excludeCollection = this.collect(exclude);
}
for (var name in includeCollection) {
if (excludeCollection[name]) continue;
includeCollection[name].styles && includeCollection[name].styles.forEach(function (fileName) {
if (reUrl.test(fileName)) {
files.styles[name] = files.styles[name] || [];
files.styles[name].push(fileName);
}
else {
_this.isClientFileExists(name + '/' + fileName, function (filePath) {
files.styles[name] = files.styles[name] || [];
files.styles[name].push(filePath);
});
}
});
includeCollection[name].scripts && includeCollection[name].scripts.forEach(function (fileName) {
if (reUrl.test(fileName)) {
files.scripts[name] = files.scripts[name] || [];
files.scripts[name].push(fileName);
}
else {
_this.isClientFileExists(name + '/' + fileName, function (filePath) {
files.scripts[name] = files.scripts[name] || [];
files.scripts[name].push(filePath);
});
}
});
includeCollection[name].view && [includeCollection[name].view].forEach(function (fileName) {
_this.isClientFileExists(name + '/' + fileName, function (filePath) {
files.views[name] = filePath.replace(/^\//, '');
});
});
_this.isClientFileExists(name + '/img/', function (folderPath) {
var fullPath = _this.clientPath + folderPath;
fs.readdirSync(fullPath).forEach(function(file) {
files.img[name] = files.img[name] || [];
files.img[name].push(fullPath + file);
});
});
}
return files;
};
Builder.prototype.renderFile = function (filePath, cb) {
var ext = filePath.match(/\w+$/)[0],
res;
if (typeof this.render[ext] == 'function') {
res = this.render[ext](filePath);
}
return typeof cb == 'function' ? cb(res) : res;
};
Builder.prototype.isClientFileExists = function (filePath, cb) {
var _this = this,
exists = false;
this.folders.forEach(function (path) {
if (fs.existsSync(_this.clientPath + path + '/' + filePath)) {
if (typeof cb == 'function') {
cb(path + '/' + filePath);
}
exists = true;
return false;
}
});
return exists;
};
module.exports = new Builder();