-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev-tool.js
More file actions
163 lines (126 loc) · 5.43 KB
/
dev-tool.js
File metadata and controls
163 lines (126 loc) · 5.43 KB
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
var DOCUMENT_ROOT, STATIC_ROOT, REWRITE_FILE;
var express = require('express');
var router = express.Router();
var path = require('path'), fs = require('fs');
var exists = fs.existsSync || path.existsSync;
function isFile(path){
return exists(path) && fs.statSync(path).isFile();
}
function getRefs(id, map){
var refs = [];
(map[id].refs || []).forEach(function(ref){
refs.push(ref);
refs.unshift.apply(refs, getRefs(ref, map));
});
return refs;
}
function html5addMeta(content, isHtml5){
if(isHtml5 && content.indexOf('<meta name="viewport"') == -1){
content = '<meta name="viewport" content="initial-scale=1, maximum-scale= 1, minimum-scale=1, user-scalable=no">' + content;
}
return content;
}
module.exports = function(root, static_root){
DOCUMENT_ROOT = root;
STATIC_ROOT = static_root;
REWRITE_FILE = path.join(DOCUMENT_ROOT, 'conf/rewrite.js');
ENGINE_FILE = path.join(DOCUMENT_ROOT, 'conf/engine.json');
router.use(function(req, res, next){
var ENGINE_CONFIG = JSON.parse(fs.readFileSync(ENGINE_FILE));
var mustacheExpress = require('mustache-express');
// Register '.mustache' extension with The Mustache Express
app.engine(ENGINE_CONFIG.suffix, mustacheExpress());
app.set('view engine', ENGINE_CONFIG.suffix);
app.set('views', DOCUMENT_ROOT);
if(ENGINE_CONFIG.combo && req.originalUrl.indexOf(ENGINE_CONFIG.combo.syntax[0]) == 1){
var combos = req.originalUrl.split(ENGINE_CONFIG.combo.syntax[0]);
if(combos.length > 1){
//handle combo
combos = combos[1].split(ENGINE_CONFIG.combo.syntax[1]);
var content = '';
for(var i = 0, j = combos.length; i < j; i++){
var file = path.join(STATIC_ROOT, combos[i]);
if(!isFile(file)){
res.status(404).end();
return;
}else{
content += fs.readFileSync(file).toString() + '\n';
}
}
res.writeHead(200, {'Content-Type': /(?:css|less|sass)(?:\?|$)/.test(combos[0]) ? 'text/css' : 'text/javascript'});
res.end(content);
return;
}
}
var rewrites, url = req.path || '/', file;
try{
//强制清除加载的文件,重新加载
delete require.cache[REWRITE_FILE];
rewrites = require(REWRITE_FILE);
}catch(e){}
try{
if(rewrites){
for(var key in rewrites){
if((new RegExp(key, 'i')).test(req.originalUrl)){
url = rewrites[key];
if(typeof url == 'function'){
url(req, res, next);
return;
}
break;
}
}
}
file = path.join(DOCUMENT_ROOT, url);
var suffix = '.' + ENGINE_CONFIG.suffix;
if(isFile(file) && file.slice(-suffix.length) != suffix){
res.sendFile(file, function(){
next();
});
return;
}
file = file.replace(suffix, '') + suffix;
if(!isFile(file) && url == '/'){
file = path.join(DOCUMENT_ROOT, 'index' + suffix);
}
if(isFile(file)){
var bContent = '', mapJson = JSON.parse(fs.readFileSync(path.join(DOCUMENT_ROOT, 'map.json')));
if(/pagelet[\/\\]/.test(file) && 'debug' in req.query){
bContent = '<script src="' + mapJson['static/feather.js'].url + '"></script>';
}
if(ENGINE_CONFIG.mustache){
var id = path.relative(DOCUMENT_ROOT, file).replace(/\\+/g, '/');
var refs = ['_global_' + suffix].concat(getRefs(id, mapJson)), datas = {};
refs.push(id.replace(suffix, '.json'));
for(var i = 0; i < refs.length; i++){
var dataFile = path.join(DOCUMENT_ROOT, 'data', refs[i].replace(suffix, '.json'));
if(isFile(dataFile)){
try{
var c = (fs.readFileSync(dataFile) || '').toString().trim();
if(c){
var data = JSON.parse(c) || {};
for(var key in data){
datas[key] = data[key];
}
}
}catch(e){
return res.status(500).send(dataFile + ' is not a valid json file!')
}
}
}
res.render(file, datas, function(err, html){
res.send(bContent + html5addMeta(html, 'h5debug' in req.query));
});
}else{
var content = fs.readFileSync(file).toString();
res.send(bContent + html5addMeta(content, 'h5debug' in req.query));
}
return;
}
}catch(e){
res.status(500).send(e.message);
}
next();
});
return router;
};