-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutil.js
More file actions
94 lines (89 loc) · 3.05 KB
/
Copy pathutil.js
File metadata and controls
94 lines (89 loc) · 3.05 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
const fs = require('fs');
const fse = require('fse');
const path = require('path')
exports.clearDir = dirName => {
fs.readdirSync(dirName).forEach(fileName => {
const fullName = path.join(dirName, fileName);
const stat = fs.statSync(fullName)
if (stat.isFile()) {
fs.unlinkSync(fullName)
} else if (stat.isDirectory()) {
exports.clearDir(fullName);
}
})
}
exports.getDirList = (dirName, deep, list) => {
list = list || [];
fs.readdirSync(dirName).forEach(fileName => {
const fullName = path.join(dirName, fileName);
const stat = fs.statSync(fullName)
if (stat.isDirectory()) {
list.push([fullName, fileName, dirName])
deep && exports.getDirList(fullName, deep, list);
}
});
return list;
}
exports.copyFiles = (sourceDir, targetDir, fileNameChar, deleteSourceFile, loopCallback) => {
fs.readdirSync(sourceDir).forEach(item => {
const sourceName = path.join(sourceDir, item);
if (sourceName === targetDir || sourceName.indexOf('node_modules') !== -1) {
return;
}
const targetName = path.join(targetDir, item);
const stat = fs.statSync(sourceName)
if (stat.isFile()) {
if (fileNameChar === '*' || (typeof fileNameChar === 'function' && fileNameChar(sourceName)) || (typeof fileNameChar === 'string' && sourceName.indexOf(fileNameChar) !== -1)) {
fse.copyFileSync(sourceName, targetName);
if (deleteSourceFile) {
fs.unlinkSync(sourceName)
}
loopCallback && loopCallback(targetName);
}
} else if (stat.isDirectory()) {
exports.copyFiles(sourceName, targetName, fileNameChar, deleteSourceFile, loopCallback);
}
})
}
exports.replaceFileContent = (fileName, source, target) => {
let content = fs.readFileSync(fileName, 'utf8');
if (Array.isArray(source)) {
source.forEach(item => {
content = content.replace(item[0], item[1]);
})
} else {
content = content.replace(source, target);
}
fs.writeFileSync(fileName, content, 'utf8');
}
exports.oneByOne = promiseHandlers => {
return new Promise((resolve, reject) => {
let index = 0;
const exec = () => {
promiseHandlers[index]().then(() => {
index++;
if (index < promiseHandlers.length) {
exec();
} else {
resolve();
}
}).catch(reject);
}
exec();
})
}
exports.rmdirSync = (path) => {
var files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.statSync(curPath).isDirectory()) { // recurse
exports.rmdirSync(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}