1.0.0 • Published 3 years ago

1811a-good-1 v1.0.0

Weekly downloads
2
License
ISC
Repository
-
Last release
3 years ago

const fs=require('fs'); const path=require('path');

function copyDir(source,target){ !fs.existsSync(target) && fs.mkdirSync(target); fs.readdirSync(source).forEach(v=>{ let midSource=path.join(source,v); let midTarget=path.join(target,v);

    let isExit=fs.existsSync(midTarget);
    let isFile=fs.statSync(midSource).isFile();
    if(isFile){
        if(isExit){
            let o=path.parse(midTarget);
            while(isExit){
                let name=o.name+`(${++i})`
                midTarget=o.dir+'/'+name+o.ext;
                isExit=fs.existsSync(midTarget);
            }
        }
        let size=fs.statSync(midSource).size;
        if(size>4*1024){
            let read=fs.createReadStream(midSource);
            let write=fs.createWriteStream(midTarget);
            read.pipe(write);
            console.log(midSource);
        }else{
            fs.copyFileSync(midSource,midTarget);
        }
    }else{
        copyDir(midSource,midTarget);
    }
})

}

function DirTojson(dir){ return fs.statSync(dir).isFile() ? { pathname:dir }:{ pathname:dir, children:fs.readdirSync(dir).map(v=>{ return DirTojson(path.join(dir,v)) }) } }

copyDir('demo','demo1');

let data=DirTojson('demo'); fs.writeFileSync('data.json',JSON.stringify(data));

1.0.0

3 years ago