-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyDraco.js
61 lines (56 loc) · 1.75 KB
/
copyDraco.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
import path from "path";
import fs from "fs";
import { throws } from "assert";
import { error, info } from "console";
const pathArr = [
{
sourceDir: "./src/utils/draco",
targetDir: "./dist/utils/draco",
},
{
sourceDir: "./src/textures/water",
targetDir: "./dist/textures/water",
},
];
const copy = (sd, td) => {
// 读取目录下的文件,返回文件名及文件类型{name: 'xxx.txt, [Symbol(type)]: 1 }
const sourceFile = fs.readdirSync(sd, { withFileTypes: true });
for (const file of sourceFile) {
// 源文件 地址+文件名
const srcFile = path.resolve(sd, file.name);
// 目标文件
const tagFile = path.resolve(td, file.name);
// 文件是目录且未创建
if (file.isDirectory() && !fs.existsSync(tagFile)) {
fs.mkdirSync(tagFile, { recursive: true }, (err) => console.log(err));
copy(srcFile, tagFile);
} else if (file.isDirectory() && fs.existsSync(tagFile)) {
// 文件时目录且已存在
copy(srcFile, tagFile);
}
!file.isDirectory() && fs.copyFileSync(srcFile, tagFile, fs.constants.COPYFILE_FICLONE);
}
};
const run = (sourceDir, targetDir) => {
const startTime = new Date().getTime();
console.log(!fs.existsSync(sourceDir));
if (!fs.existsSync(sourceDir)) {
throw error("no such file or directory");
} else if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true }, (err) => console.log(err));
copy(sourceDir, targetDir);
} else {
copy(sourceDir, targetDir);
}
const endTime = new Date().getTime();
console.log("耗时:", ((endTime - startTime) / 1000).toFixed(2) + "s");
};
pathArr.forEach((pathItem) => {
const { sourceDir, targetDir } = pathItem;
fs.mkdirSync(targetDir, { recursive: true }, (err) => {
if (err) {
throw err;
}
});
run(sourceDir, targetDir);
});