-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·231 lines (201 loc) · 6.99 KB
/
index.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var fs = require('fs');
var path = require('path');
var log = require('./lib/log');
var arrh = require('./lib/arr');
var fh = require('./lib/file-helper');
var Parser = require('./lib/parse');
var min = require('./lib/min');
var html2js = require('./lib/ngHtml2js');
var hash = require('./lib/hash');
function Mgr(configPath) {
this.init();
this.setConfig(configPath);
}
Mgr.prototype.init = function () {
this._config = {};
this._pages = {};
// 已经加载过的lib和group
this.loadedGroups = {};
this.loadedLibs = {};
};
Mgr.prototype.setConfig = function (configPath) {
try {
this._config = JSON.parse(fs.readFileSync(configPath));
} catch (e) {
log.error('Can\'t read config! ' + configPath);
}
};
Mgr.prototype.config = function (name) {
if (typeof this._config[name] !== 'undefined') {
return this._config[name];
} else {
log.warn(name + ' is not set in config!');
return null;
}
};
Mgr.prototype.needMin = function () {
return this.config('min') ? true : false;
};
Mgr.prototype.getConfig = function (libName) {
return this._config.libs[libName];
};
Mgr.prototype.parsePage = function (pageName) {
// we don't need to cache the page right?...
log.debug('Parse page: ' + pageName);
var pageConfig = this._config.pages[pageName];
if (typeof pageConfig !== 'object') {
}
var pageFiles = [];
var parse = new Parser({
dstFolder: 'site',
dstLibFolder: this.config('libpath'),
libConfigs: this._config.libs,
groupConfigs: this._config.groups
});
var me = this;
function pageNeedMin() {
if (typeof pageConfig.min !== 'undefined' && pageConfig.min) {
return true;
} else {
return me.needMin();
}
}
var groups = pageConfig.groups;
var groupFiles = [];
var minOpt = {};
if (typeof groups === 'object') {
log.debug('Start loading groups for page ' + pageName);
groups.forEach(function (groupName) {
log.debug('Parse group ' + groupName + ' for page ' + pageName);
groupFiles = parse.parseGroup(groupName);
if (pageNeedMin()) {
// 压缩和不压缩的要分开处理额,在缓存的时候
if (typeof me.loadedGroups[groupName + '.min'] === 'undefined') {
minOpt = {
name: groupName,
files: groupFiles,
dstFolder: path.join(me.config('grouppath'), groupName)
};
groupFiles = min.lib(minOpt);
me.loadedGroups[groupName + '.min'] = groupFiles;
} else {
groupFiles = me.loadedGroups[groupName + '.min'];
}
}
pageFiles = arrh.merge(pageFiles, groupFiles);
});
}
log.debug('Start loading libs and files for page ' + pageName);
var libs = pageConfig.libs;
var libFiles = [];
if (typeof libs === 'object') {
libs.forEach(function (libName) {
log.debug('Parse lib ' + libName + ' for page ' + pageName);
libFiles = parse.parseLib(libName);
if (pageNeedMin()) {
if (typeof me.loadedLibs[libName + '.min'] === 'undefined') {
var dstFolder = '';
if (me.getConfig(libName).folder) {
dstFolder = me.getConfig(libName).folder;
} else {
dstFolder = path.join(me.config('libpath'), libName);
}
minOpt = {
name: libName,
files: libFiles,
dstFolder: dstFolder
};
libFiles = min.lib(minOpt);
me.loadedLibs[libName + '.min'] = libFiles;
} else {
libFiles = me.loadedLibs[libName + '.min'];
}
}
pageFiles = arrh.merge(pageFiles, libFiles);
});
}
var templateGlobs = pageConfig.templates;
var templateFiles = [];
var templateContent = '';
if (typeof templateGlobs === 'object') {
// min the templates
templateFiles = fh.glob(templateGlobs);
templateFiles.forEach(function (filePath) {
templateContent += html2js.toJs({
src: filePath,
url: path.basename(filePath)
});
});
// 写入到哪里呢?还有压缩的问题,其实应该跟app一起压缩的....还是分开处理吧.
fh.write(pageConfig.templatePath, templateContent);
}
// TODO:do the min for files and do the clean as well?
// No need, just add a task to clean all the js and css that are not min.js min.css
var fileGlobs = pageConfig.files;
var filesOnly = [];
if (typeof fileGlobs === 'object') {
// do the min for files
filesOnly = fh.glob(fileGlobs);
if (pageNeedMin()) {
minOpt = {
name: pageName,
files: filesOnly,
dstFolder: path.join(me.config('pagepath'), pageName)
};
filesOnly = min.lib(minOpt);
}
pageFiles = arrh.merge(pageFiles, filesOnly);
}
log.debug('Resolve file path ');
pageFiles = fh.resolve(pageFiles, this.config('webroot'));
// split the files
var scripts = {};
scripts.js = fh.split(pageFiles, 'js');
scripts.css = fh.split(pageFiles, 'css');
this._pages[pageName] = scripts;
log.debug('Finish loading page ' + pageName);
return this._pages[pageName];
};
Mgr.prototype.toJSON = function (dst) {
var str_pages = JSON.stringify(this._pages, null, 4);
// 把\\替换成/以避免windows下路径分割符导致的错误
str_pages = str_pages.replace(/\\\\/g, '/');
try {
fs.writeFileSync(dst, str_pages);
} catch (e) {
log.error('can\'t save in json format!', e);
}
};
Mgr.prototype.parseAllPage = function () {
if (typeof this._config.pages !== 'object') {
log.error('config is not set! can\'t find any page!');
return;
}
var pages = this._config.pages;
var pageName;
for (pageName in pages) {
log.debug(pageName);
this.parsePage(pageName);
}
if (this.config('hash')) {
this.hash();
}
this.toJSON(this._config.dst);
};
Mgr.prototype.hash = function () {
// first get all the files
log.debug('hashing file now!');
//console.log(this._pages);
var webroot = this.config('webroot');
for (var page_name in this._pages) {
var page = this._pages[page_name];
// hash其实只取开头就够了
page.js = page.js.map(function (file) {
return file + '?hash=' + hash(webroot + '/' + file).substr(0, 6);
});
page.css = page.css.map(function (file) {
return file + '?hash=' + hash(webroot + '/' + file).substr(0, 6);
});
}
};
module.exports = Mgr;