-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
58 lines (50 loc) · 1.72 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
const fs = require('fs');
const path = require('path');
const excludeDirs = ['build', 'node_modules', 'deploy', '.idea'];
/**
* @des 参数解释同上
*/
function copyFolder(copiedPath, resultPath, direct) {
if (!direct) {
copiedPath = path.join(__dirname, copiedPath);
resultPath = path.join(__dirname, resultPath);
}
const p = path.relative(__dirname, copiedPath);
if (excludeDirs.some(ef => p.startsWith(ef))) return;
function createDir(dirPath) {
fs.mkdirSync(dirPath);
}
if (fs.existsSync(copiedPath)) {
createDir(resultPath);
/**
* @des 方式二:
*/
const files = fs.readdirSync(copiedPath, {withFileTypes: true});
for (let i = 0; i < files.length; i++) {
const cf = files[i];
const ccp = path.join(copiedPath, cf.name);
const crp = path.join(resultPath, cf.name);
if (cf.isFile()) {
/**
* @des 创建文件,使用流的形式可以读写大文件
*/
const readStream = fs.createReadStream(ccp);
const writeStream = fs.createWriteStream(crp);
readStream.pipe(writeStream);
} else {
try {
/**
* @des 判断读(R_OK | W_OK)写权限
*/
fs.accessSync(path.join(crp, '..'), fs.constants.W_OK);
copyFolder(ccp, crp, true);
} catch (error) {
console.log('folder write error:', error);
}
}
}
} else {
console.log('do not exist path: ', copiedPath);
}
}
copyFolder('.', './build');