1.0.0 • Published 3 years ago

szl-demo1 v1.0.0

Weekly downloads
1
License
ISC
Repository
-
Last release
3 years ago
const copyDir = (sourcePath, targetPath) => {
    const flag1 = fs.existsSync(sourcePath)
    const flag2 = fs.existsSync(targetPath)
    // 1.容错处理 
    if (!flag1) {
        throw new Error("源目录不存在!!!" + sourcePath);
        return;
    }
    if (flag2) {
        throw new Error("目标目录存在!!!" + targetPath);
        return;
    }
    // 2.创建目标文件夹
    fs.mkdirSync(targetPath);
    // 3.读取源目录的子目录
    fs.readdirSync(sourcePath).forEach(item => {
        // 拼接
        let midSourcePath = sourcePath + "/" + item;
        let midTargetPath = targetPath + "/" + item;
        // 读取信息
        if (fs.statSync(midSourcePath).isFile()) {
            // 是文件
            fs.copyFileSync(midSourcePath, midTargetPath)
        } else {
            // 是文件夹 (递归)
            copyDir(midSourcePath, midTargetPath)
        }
    })
}