1.0.0 • Published 4 years ago

fuzhi-demo v1.0.0

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

拷贝文件夹

const fs=require('fs');

const copyDir=(sourcePath,targetPath)=>{
    //检测文件是否存在
    const flag1=fs.existsSync(sourcePath);//源文件
    const flag2=fs.existsSync(targetPath);//目标文件
    if(!flag1){  //源文件应该存在
        throw new Error('源文件不存在'+sourcePath);
        return;
    }
    if(flag2){ //目标文件应该不存在
        throw new Error('目标文件存在'+targetPath);
        return;
    }

    //建目标文件夹
    fs.mkdirSync(targetPath);
    //读取源文件的子目录
    const arr=fs.readdirSync(sourcePath);
    //遍历子目录
    arr.forEach(item=>{
        const midsourcePath=sourcePath+'/'+item;
        const midtargetPath=targetPath+'/'+item;
        //获取原文件信息
        const info=fs.statSync(midsourcePath);
        if(info.isFile()){
            //是文件 赋值文件
            fs.copyFileSync(midsourcePath,midtargetPath);
        }else{
            //文件夹
            copyDir(midsourcePath,midtargetPath);
        }
    })

}

copyDir('./node_modules','aa');