dome-copydir
v1.0.1
Published
```js const copyDir = (usedDir, targetDir) => { // 创建目标文件 fs.mkdirSync(targetDir); // 判断是否有要拷贝的文件 if (!fs.existsSync(usedDir)) { // 如果没有 抛出错误 throw new Error('不存在要拷贝的文件' + usedDir);
Downloads
5
Readme
文件拷贝
const copyDir = (usedDir, targetDir) => {
// 创建目标文件
fs.mkdirSync(targetDir);
// 判断是否有要拷贝的文件
if (!fs.existsSync(usedDir)) {
// 如果没有 抛出错误
throw new Error('不存在要拷贝的文件' + usedDir);
return;
}
// 判断是否有目标文件
if (!fs.existsSync(targetDir)) {
// 如果没有 抛出错误
throw new Error('没有找到目标文件' + targetDir);
return;
}
// 读取要拷贝的文件夹下的所有的子目录
const arr = fs.readdirSync(usedDir);
// 遍历所有的子目录
arr.forEach(item => {
// 定义被拷贝的文件路径
const path1 = usedDir + "/" + item;
// 定义目标文件路径
const path2 = targetDir + "/" + item;
// 读取文件的信息
const info = fs.statSync(path1);
// 判断这个文件是不是一个文件
if (info.isFile()) {
// 如果是一个文件就拷贝
fs.copyFileSync(path1, path2);
} else {
// 重新调用函数(递归)
copyDir(path1, path2);
}
})
}