-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
159 lines (149 loc) · 4.89 KB
/
util.js
File metadata and controls
159 lines (149 loc) · 4.89 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
/**
* Created by moujintao on 2017/6/8.
*/
let path = require('path');
let fs = require('fs');
module.exports={
rmdirSync:(function(){
function iterator(url,dirs){
var stat = fs.statSync(url);
if(stat.isDirectory()){
dirs.unshift(url);//收集目录
inner(url,dirs);
}else if(stat.isFile()){
fs.unlinkSync(url);//直接删除文件
}
}
function inner(path,dirs){
var arr = fs.readdirSync(path);
for(var i = 0, el ; el = arr[i++];){
iterator(path+"/"+el,dirs);
}
}
return function(dir,cb){
cb = cb || function(){};
var dirs = [];
try{
iterator(dir,dirs);
for(var i = 0, el ; el = dirs[i++];){
fs.rmdirSync(el);//一次性删除所有收集到的目录
}
cb()
}catch(e){//如果文件或目录本来就不存在,fs.statSync会报错,不过我们还是当成没有异常发生
e.code === "ENOENT" ? cb() : cb(e);
}
}
})(),
readAllIconSvg:function (entryList,runPath) {
let files = [],jsPath,dirs,matchs,entery;
for(let i=0,len=entryList.length;i<len;i++){
entery=entryList[i];
jsPath = path.join(runPath, entery);
dirs = fs.readdirSync(jsPath);
matchs = [];
dirs.forEach(function (item) {
matchs = item.match(/(.+)\.svg$/);
if (matchs) {
files.push(path.resolve(jsPath, item));
}
});
}
return files;
},
mkdir:function (dirpath,dirname){
//判断是否是第一次调用
if(typeof dirname === "undefined"){
if(fs.existsSync(dirpath)){
return;
}else{
this.mkdir(dirpath,path.dirname(dirpath));
}
}else{
//判断第二个参数是否正常,避免调用时传入错误参数
if(dirname !== path.dirname(dirpath)){
this.mkdir(dirpath);
return;
}
if(fs.existsSync(dirname)){
fs.mkdirSync(dirpath)
}else{
this.mkdir(dirname,path.dirname(dirname));
fs.mkdirSync(dirpath);
}
}
},
delRepeat:function (arry) {
arry.sort();//排序
var n = [arry[0]];
for (var i = 1; i < arry.length; i++) {
if (arry[i] !== n[n.length - 1]) {
n.push(arry[i]);
}
}
return n;
},
getRepeatClassNameSvg:function (arry) {
let n = [],nameList=[],svgList=[],result={};
arry.forEach((value)=>{
result=this.delRepeatName(value,result)
});
return result
},
delRepeatName:function (value,result) {
let filePath=value.split("/"),fileName=filePath[filePath.length-1],names=fileName.split(".");
let name=names.slice(0,names.length-1).join("");
if(result[name]===undefined){
result[name]=[];
result[name].push(value);
}else{
result[name].push(value);
}
return result;
},
createFileCache:function (classNames,cachePath) {
var list=[],hasCache=false;
this.mkdir(cachePath);
for(let name of Object.keys(classNames)){
if(classNames[name].length==1){
list.push(classNames[name][0]);
}else{
classNames[name].forEach((val,index)=>{
let suffix="",newPath;
if(index!==0){
suffix=index;
newPath=cachePath+"/"+name+"-"+suffix+".svg";
}else{
newPath=cachePath+"/"+name+".svg";
}
this.copy(val,newPath);
hasCache=true;
list.push(newPath);
});
}
}
return {
svgList:list,
hasCache:hasCache
};
},
getOutputPath(output,rootPath){
let fontDirName,htmlDirName,cssDirName;
if(output instanceof Object){
fontDirName = output.font;
htmlDirName = output.html||output.font;
cssDirName = output.css||output.font;
}else if(typeof output==="string"){
fontDirName = output;
htmlDirName = output;
cssDirName = output;
}
return {
fontDirName:path.join(rootPath,fontDirName),
htmlDirName:path.join(rootPath,htmlDirName),
cssDirName:path.join(rootPath,cssDirName)
}
},
copy:function(src, dst) {
fs.writeFileSync(dst, fs.readFileSync(src));
}
}