forked from ksky521/nodeppt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
executable file
·222 lines (203 loc) · 5.9 KB
/
helper.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
var fs = require('fs');
var path = require('path');
var ejs = require('ejs');
var win32 = process.platform === 'win32';
var pathSeparatorRe = /[\/\\]/g;
var $ = {
getDirPath: function(dir) {
dir += path.sep;
try {
dir = fs.realpathSync(dir) + path.sep;
} catch (e) {}
return dir;
}
};
var unixifyPath = function(filepath) {
if (win32) {
return filepath.replace(/\\/g, '/');
} else {
return filepath;
}
};
/**
* mix
* @param {[type]} obj [description]
* @return {[type]} [description]
*/
$.mix = function mix(obj) {
var i = 1,
target, key;
var hasOwnProperty = Object.prototype.hasOwnProperty;
for (; i < arguments.length; i++) {
target = arguments[i];
for (key in target) {
if (hasOwnProperty.call(target, key)) {
obj[key] = target[key];
}
}
}
return obj;
}
/**
* 遍历
* @param {[type]} rootdir [description]
* @param {Function} callback [description]
* @param {[type]} subdir [description]
* @param {[type]} destDir [description]
* @param {[type]} judgeFunction [description]
* @return {[type]} [description]
*/
$.recurse = function(rootdir, callback, subdir, destDir, judgeFunction) {
var abspath = subdir ? path.join(rootdir, subdir) : rootdir;
destDir = destDir || '';
subdir = subdir || '';
if (typeof destDir === 'function') {
judgeFunction = destDir;
}
judgeFunction = typeof judgeFunction === 'function' ? judgeFunction : function() {
return true;
};
fs.readdirSync(abspath).forEach(function(filename) {
var filepath = path.join(abspath, filename);
if (fs.statSync(filepath).isDirectory() && judgeFunction(filename)) {
$.recurse(rootdir, callback, unixifyPath(path.join(subdir, filename)), unixifyPath(path.join(destDir, filename)), judgeFunction);
} else {
judgeFunction(filename) && callback(unixifyPath(filepath), rootdir, subdir, filename, destDir, judgeFunction);
}
});
}
/**
* 是否是目录
* @param {[type]} dir [description]
* @param {[type]} notwarning [description]
* @return {Boolean} [description]
*/
$.isDir = function(dir, notwarning) {
if (!fs.existsSync(dir)) {
dir = path.join(process.cwd(), dir);
if (!fs.existsSync(dir)) {
if (!notwarning) {
console.log('ERROR: '.bold.red + dir + ' 不是一个正确路径');
}
return false;
}
}
var stat = fs.statSync(dir);
if (!stat.isDirectory()) {
if (!notwarning) {
console.log('ERROR: '.bold.red + dir + ' 不是一个正确路径');
}
return false;
}
return true;
}
$.isFile = function(file){
var stat = fs.statSync(file);
if(stat.isFile()){
return true;
}
return false;
}
/**
* 写文件
* @param {[type]} path [description]
* @param {[type]} content [description]
* @return {[type]} [description]
*/
$.writeFile = function(filepath, content) {
$.mkdir(path.dirname(filepath));
return fs.writeFileSync(filepath, content);
}
/**
* 读取文件
* @param {[type]} filepath [description]
* @return {[type]} [description]
*/
$.readFile = function(filepath) {
// console.log(filepath);
return fs.readFileSync(filepath, 'utf8').toString();
}
/**
* 路径复制
* @param {[type]} distDir [description]
* @param {[type]} destDir [description]
* @param {[type]} judgeFunction [description]
* @return {[type]} [description]
*/
$.copy = function(distDir, destDir, judgeFunction) {
if (!(typeof judgeFunction === 'function')) {
judgeFunction = function() {
return true
};
}
if ($.isDir(distDir, true)) {
$.recurse(distDir, function(abspath, rootdir, subdir, filename) {
var dest = path.join(destDir, (subdir ? subdir : ''));
if (judgeFunction(filename, dest, subdir)) {
if (!$.isDir(dest, true)) {
$.mkdir(dest);
}
if (filename) {
$.copy(abspath, path.join(dest, filename));
} else {}
}
});
} else if ($.exists(distDir)) {
var content = fs.readFileSync(String(distDir));
$.writeFile(destDir, content);
} else {
console.log(('路径 "' + distDir + '" 不存在!').bold.red);
}
};
/**
* ejs渲染file
* @param {[type]} filepath [description]
* @param {[type]} data [description]
* @return {[type]} [description]
*/
$.renderFile = function(filepath, data) {
var str = $.readFile(filepath);
// console.log(filepath, str);
return ejs.render(str, data || {});
};
/**
* ejs渲染string
* @param {[type]} str [description]
* @param {[type]} data [description]
* @return {[type]} [description]
*/
$.renderStr = function(str, data) {
return ejs.render(str, data);
}
/**
* 创建目录
* @param {[type]} dirpath [description]
* @param {[type]} mode [description]
* @return {[type]} [description]
*/
$.mkdir = function(dirpath, mode) {
if (mode == null) {
mode = parseInt('0777', 8) & (~process.umask());
}
dirpath.split(pathSeparatorRe).reduce(function(parts, part) {
parts += part + '/';
var subpath = path.resolve(parts);
if (!$.exists(subpath)) {
try {
fs.mkdirSync(subpath, mode);
} catch (e) {
console.log('Unable to create directory "' + subpath + '" (Error code: ' + e.code + ').', e);
}
}
return parts;
}, '');
}
/**
* 判断是否存在
* @return {[type]} [description]
*/
$.exists = function() {
var filepath = path.join.apply(path, arguments);
return fs.existsSync(filepath);
};
module.exports = $;