-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (73 loc) · 3.38 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
var path = require('path');
var file = require('file-system');
var util = require('utils-extend');
var fs = require('fs');
var css = require('css');
const outputFile = 'detail2.css';
const inputFile = 'detail1.css';
const unused_cssFile = 'unused_detail.txt';
const replacement = '';
var contents = file.readFileSync(path.join(__dirname, inputFile),{ encoding: 'utf8' });
var astObj = css.parse(contents);
console.log(css.stringify(astObj));
return;
// console.log(contents);
contents = removeComments(contents);
// file.fs.writeFileSync(path.join(__dirname, 'isd2.css'), contents, { encoding: 'utf8' });
function removeComments(contents){
return contents.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\//g,'');
}
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(path.join(__dirname, unused_cssFile))
});
lineReader.on('line', function (line) {
var originLine = line;
// if( /\.isdbookingnov \.noticeIcon:before/.test(originLine)){
// console.log();
// }
var reg1 = new RegExp('^[.\\w\\- \\[\\=\'\\]\\:>]*,','g');
if(reg1.test(line)){//end ,
// console.log('RegExp found! RegExp:'+reg1+' line:'+line);
line = line.replace(/\./g,'\\.').replace(/\[/g,'\\[').replace(/\]/g,'\\]').replace(/^ /,'').replace(/[ ]*>[ ]*/g,'[ ]*>[ ]*').replace(/\(/g,'\\(').replace(/\)/g,'\\)');
var regTemp = new RegExp('[\\r\\n][\\s\\b]*'+line,'g');
if(!regTemp.test(contents)){
console.log('error! regTemp:'+regTemp);
}
contents = contents.replace(regTemp, replacement);
file.fs.writeFileSync(path.join(__dirname, outputFile), contents, { encoding: 'utf8' });
return;
}
//if end with ',' match , then delete it with ','
line = line.replace(/\./g,'\\.').replace(/\[/g,'\\[').replace(/\]/g,'\\]').replace(/^ /,'').replace(/[ ]*>[ ]*/g,'[ ]*>[ ]*').replace(/\(/g,'\\(').replace(/\)/g,'\\)');
var regTemp = new RegExp('[\\r\\n][\\s\\b]*'+line+'[\\s\\b]*,','g');
if(regTemp.test(contents)){
console.log('end with \',\' originLine:'+originLine);
contents = contents.replace(regTemp, replacement);
file.fs.writeFileSync(path.join(__dirname, outputFile), contents, { encoding: 'utf8' });
// return;
}
// if(originLine == '.car-index .clu li:before'){
// console.log();
// file.fs.writeFileSync(path.join(__dirname, outputFile), contents, { encoding: 'utf8' });
// }
// line = line.replace(/\./g,'\\.').replace(/\[/g,'\\[').replace(/\]/g,'\\]').replace(/^ /,'').replace(/[ ]*>[ ]*/g,'[ ]*>[ ]*').replace(/\(/g,'\\(').replace(/\)/g,'\\)');
//',' lies before
var reg = new RegExp(',[\\r\\n]*' + line + '[\\s]*[,{]','g');
if(reg.test(contents)){
var regTemp = new RegExp(',[\\r\\n]*' + line + '[\\s]*[,]*','g');
contents = contents.replace(regTemp, replacement);
file.fs.writeFileSync(path.join(__dirname, outputFile), contents, { encoding: 'utf8' });
// return;
}
var reg2 = new RegExp('[\\r\\n]' + line + '[\\s]*{[\\r\\n\\w\\s\\b:\\-%"\\\\;#\\.\'\\(\\),/*+=\!]*}','g');
if(!reg2.test(contents)){
console.log('RegExp not found! :'+reg2 +' originLine:'+originLine);
return;
}else{
contents = contents.replace(reg2, replacement);
}
});
lineReader.on('close',function(){
console.log('closed');
file.fs.writeFileSync(path.join(__dirname, outputFile), contents, { encoding: 'utf8' });
});