-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.js
55 lines (47 loc) · 1.2 KB
/
common.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
#!/usr/bin/env node
var fs = require("fs");
var path = require("path");
function createParentFolder(filePath) {
var parentDir = path.dirname(filePath);
if (!fs.existsSync(parentDir)) {
try {
fs.mkdirSync(parentDir);
} catch (e) {
console.log("create folder fail:" + parentDir);
}
}
}
function getFiles(path, data) {
var dir = fs.readdirSync(path);
dir.map(function(item) {
if (item === "." || item === "..") {
return;
}
var itempath = path + "/" + item;
var stat = fs.statSync(itempath);
if (stat.isDirectory()) {
getFiles(itempath, data);
} else {
data.push(itempath);
}
});
return data;
}
function escapeRegExp(string) {
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
function replaceAll(string, find, replace) {
if (string) {
return string.replace(new RegExp(escapeRegExp(find), "g"), replace);
}
return string;
}
function isObject(item) {
return (typeof item === "object" && !Array.isArray(item) && item !== null);
}
module.exports = {
getFiles,
replaceAll,
isObject,
createParentFolder
};