-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
74 lines (66 loc) · 1.77 KB
/
build.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
const fs = require('fs');
const path = require('path');
const srcPath = ['./src/upload', './src/views', './src/public'];
const tarPath = ['./build/src/upload', './build/src/views', './build/src/public'];
function copyFile(srcPath, tarPath, cb) {
var rs = fs.createReadStream(srcPath);
rs.on('error', function (err) {
if (err) {
console.log('read error', srcPath);
}
cb && cb(err);
});
var ws = fs.createWriteStream(tarPath);
ws.on('error', function (err) {
if (err) {
console.log('write error', tarPath);
}
cb && cb(err);
});
ws.on('close', function (ex) {
cb && cb(ex);
});
rs.pipe(ws);
}
function copyFolder(srcDir, tarDir, cb) {
fs.readdir(srcDir, function (err, files) {
var count = 0;
var checkEnd = function () {
++count == files.length && cb && cb();
};
if (err) {
checkEnd();
return;
}
files.forEach(function (file) {
var srcPath = path.join(srcDir, file);
var tarPath = path.join(tarDir, file);
fs.stat(srcPath, function (err, stats) {
if (stats.isDirectory()) {
// console.log('mkdir', tarPath);
fs.mkdir(tarPath, function (err) {
if (err) {
console.log(1, err);
return;
}
copyFolder(srcPath, tarPath, checkEnd);
});
} else {
copyFile(srcPath, tarPath, checkEnd);
}
});
});
//为空时直接回调
files.length === 0 && cb && cb();
});
}
function work() {
const start = Date.now();
console.log('开始复制非ts文件');
for (let i = 0; i < tarPath.length; i++) {
fs.mkdirSync(tarPath[i]);
copyFolder(srcPath[i], tarPath[i]);
}
console.log('复制非ts文件完成,耗时', Date.now() - start, 'ms');
}
work();