-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparsefile.js
112 lines (91 loc) · 2.47 KB
/
parsefile.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
/**
* @author [email protected]
* parse source file
* the code for each product maybe different
*/
var fs = require('fs')
, path = require('path')
, compress = require("./compile/compress")
, step = require('./compile/lib/step')
, config = require("./config")
;
exports.getJSListObj = function(iPath){
var fileName = path.basename(iPath)
, reg = config.configJS.jsReg
, oDir = config.config.tmpDir
, jsBaseDir = config.configJS.jsBaseDir
, oPath = path.join(oDir, fileName)
, iCode = ''
, list = []
, remoteList = []
, localList = []
, i = 0
, leng = 0
, index = 0
, tmpStr = ''
, tmpList = []
, tmpObj = {}
, listObj = {}
, charset = config.configJS.charset
, iPathObj = {'url':iPath, 'charset': charset}
;
step.Step(
function mkTmpDirAndCompressFile() {
if (!path.existsSync(oDir)) {
fs.mkdirSync(oDir, 0755);
}
var sourceName = path.basename(iPathObj.url);
compress.compress(iPathObj, oPath, charset, false, false, this);
},
function parseFile(oPath){
iCode = fs.readFileSync(oPath, charset);
list = iCode.match(reg);
leng = (list ? list.length:0);
if(leng > 0) {
index = list[0].indexOf('(')+1;
}
for(i = 0; i < leng; i++) {
tmpStr = list[i].substring(index, list[i].length-2).replace(/['"]/g, '');
tmpList = tmpStr.split(',');
tmpObj = {
'url': tmpList[0],
'charset': (tmpList[1]?tmpList[1]:'utf8')
};
if(tmpList[0].indexOf('http') === 0) {
remoteList.push(tmpObj);
} else {
tmpObj.url = path.join(jsBaseDir, tmpObj.url);
localList.push(tmpObj);
}
}
//源文件的处理
iCode = iCode.replace(reg, '');
var fd = fs.openSync(oPath, 'w', '0666');
fs.writeSync(fd, iCode, 0);
fs.closeSync(fd);
tmpObj = {
'url': oPath,
'charset': charset
};
listObj = {'remoteList': remoteList, 'localList': localList, 'sourceFile':tmpObj};
}
);
return listObj;
};
exports.getCSSListObj = function(){
var iPath = config.configCSS.sPath
, reg = config.configCSS.cssReg
, charset = config.configCSS.charset
, cssBaseDir = path.dirname(iPath)
, iCode = ''
, list = []
, tmpStr = ''
, localList = []
;
iCode = fs.readFileSync(iPath, charset);
while((list = reg.exec(iCode)) != null){
tmpStr = list[1].replace(/['"]/g, '');
localList.push(path.join(cssBaseDir, tmpStr));
}
return localList;
};