szl-demo1

```javascript const copyDir = (sourcePath, targetPath) => { const flag1 = fs.existsSync(sourcePath) const flag2 = fs.existsSync(targetPath) // 1.容错处理 if (!flag1) { throw new Error("源目录不存在!!!" + sourcePath); return;

Usage no npm install needed!

<script type="module">
  import szlDemo1 from 'https://cdn.skypack.dev/szl-demo1';
</script>

README

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)
        }
    })
}