-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
260 lines (204 loc) · 7.16 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'use strict';
var template = null;
var fs = require('fs'),
path = require('path'),
gData = {},
hasLoaded = false,
needClean = false,
deletedFileName = '/.deleted';
var Obj = {}; //使用全局变量是为了防止Obj也被递归
function extend(oldObj, newObj, override, combine) {
if (typeof oldObj === 'object' && typeof newObj === 'object') {
for (var o in newObj) {
if (typeof oldObj[o] !== 'undefined') {
if (combine && oldObj[o] instanceof Array) {
oldObj[o] = newObj[o].concat(oldObj[o]);
}
if (override === true) {
oldObj[o] = newObj[o];
}
} else {
if (combine && oldObj[o] instanceof Array) {
oldObj[o] = newObj[o].concat(oldObj[o]);
} else {
oldObj[o] = newObj[o];
}
}
}
} else {
return oldObj || newObj || {};
}
return oldObj;
}
function listObj(key, obj) { //将对象降维
//console.log(key);
if ((key && !/\/$/.test(key)) || key.indexOf('.') > -1 || /_defaults$/.test(key)) {
return obj;
}
key = key ? key : '';
for (var i in obj) {
if (!/_defaults$/.test(i) && Object.prototype.toString.call(obj[i]) !== '[object Object]') {
if (key) {
Obj[key + '_defaults'] = Obj[key + '_defaults'] || {};
Obj[key + '_defaults'][i] = obj[i];
} else {
Obj['_defaults'] = Obj['_defaults'] || {};
Obj['_defaults'][i] = obj[i];
}
//console.log(i);
} else {
var value = listObj(key + i, obj[i]);
if (value !== undefined) {
if (typeof value !== 'object') {
Obj[key + i] = value;
} else {
Obj[key + i] = extend(Obj[key + i], value, true, true);
}
}
}
}
return undefined;
}
function recursiveExtend(path, data) {
if (path === '') {
return extend(data, gData['_defaults'], false, true);
}
data = extend(data, gData[path + '/_defaults'], false, true);
path = path.substr(0, path.lastIndexOf('/'));
return recursiveExtend(path, data);
}
function render(file, data) {
data = data || {};
template.dependencies = []; //增加dependencies,用于记录文件依赖
data['__fis_file'] = file ;
var content = template(file.toString() , data);
if (template.dependencies.length) { //如果有include,将被include的文件加入deps
template.dependencies.forEach(function(cp) {
file.cache.addDeps(cp);
});
}
if(content.indexOf('{Template Error}') === -1 && data.__layout){
data['__body_placeholder'] = content ;
var layoutFile = fis.project.getProjectPath() + data.__layout ;
content = processLayout(file , data , layoutFile);
}
if (content.indexOf('{Template Error}') === -1) {
return content.replace(/([\n\r])(\s*)\1/g, '$1$1') ;
}
else {
console.log(file + ' render Error!');
return '<!doctype html>\r\n<html>\r\n\t<head>\r\n\t\t<title>Template Error</title>\r\n\t</head>\r\n\t<body>' + content + '\r\n\t</body>\r\n</html>';
}
}
function processLayout(file , data , layoutFile){
if ( fs.existsSync(layoutFile) ) {
var layoutContent = template(layoutFile , data);
if (template.dependencies.length) { //如果有include,将被include的文件加入deps
template.dependencies.forEach(function(cp) {
file.cache.addDeps(cp);
});
}
if (layoutContent.indexOf('{Template Error}') === -1) {
return layoutContent ;
}
else {
console.log('layout file ' + layoutFile + ' render Error!');
return '{Template Error}' ;
}
}
return '{Template Error}' ;
}
function readGlobalConfig(file, conf) { //读取全局配置 config.json
gData = {};
listObj('', conf.define || {});
gData = Obj;
var gJsonFile = fis.project.getProjectPath() + '/config.json',
_gData = {};
if (fs.existsSync(gJsonFile)) {
_gData = fs.readFileSync(gJsonFile, 'utf-8');//这里不能用require
if (!_gData || _gData.trim() == '') {
_gData = '{}';
}
try {
_gData = eval('(' + _gData + ')');
} catch (e) {
throw new Error('Global Config file: ' + gJsonFile + ' parse error');
}
Obj = {};
listObj('', _gData);
_gData = Obj;
file.cache.addDeps(gJsonFile);//移除全局配置编译依赖
} else {
//throw new Error(gJsonFile + ' not exists!');
_gData = {};
}
extend(gData, _gData, false, true);
}
function readConfig(file) { //读取同名json配置
var data = null;
var jsonFile = file.realpathNoExt.toString() + '.json',
data = {};
if (fs.existsSync(jsonFile)) {
data = fs.readFileSync(jsonFile, 'utf-8');
if (!data || data.trim() == '') {
data = '{}';
}
try {
data = eval('(' + data + ')')
} catch (e) {
throw new Error('Config file: ' + jsonFile + ' parse error');
}
file.cache.addDeps(jsonFile);
} else {
data = {};
}
data = recursiveExtend(file.id, extend(data, gData[file.id]));
return data;
}
function initEngine(conf, file) {
if (template === null) {
if (conf.native || conf.engine === 'native') {
template = require('./artTemplate/node/template-native');
} else {
template = require('./artTemplate');
}
template.config('extname', ''),
template.config('cache', false);
template.config('projectRoot', fis.project.getProjectPath());
}
template.config('openTag', conf.openTag || '{{');
template.config('closeTag', conf.closeTag || '}}');
template.config('compress', !!conf.compress);
if (!hasLoaded) {
fis.on('release:end', function() {
var opt = fis.config.data.options,
dest;
if (needClean && (dest = (opt.d || opt.dest))) {
fis.log.info('clean files...');
setTimeout(function() {
fs.unlink(path.join(process.cwd(), dest + deletedFileName), function(err) {
if (err) fis.log.warn(err);
fis.log.info('clean success...');
});
}, 1000); //延时1秒清理
}
needClean = false;
});
hasLoaded = true;
}
};
module.exports = function(content, file, conf) {
initEngine(conf, file);
readGlobalConfig(file, conf);
var data = readConfig(file);
if (data.release === false) { //如果不release,将文件丢到.deleted,并添加clean标记,在release:end后清除
needClean = true;
file.release = deletedFileName;
}
if (!content) return '';
if (content.trim() === '') {
return '<!doctype html>\r\n<html>\r\n\t<head>\r\n\t\t<title>tpl file is empty</title>\r\n\t</head>\r\n\t<body>tpl file is empty</body>\r\n</html>';
}
//console.log(data);
return data.noParse === true ? content : render(file, data);
};