node创建文件

294 阅读1分钟
let fs = require('fs')

//同步创建文件夹
// function mkdirp(paths){
//     let arr = paths.split('/')
//     for (let i = 0; i < arr.length; i++) {
//         let currentPath = arr.slice(0,i+1).join('/')
//         if(!fs.existsSync(currentPath)){
//             fs.mkdirSync(currentPath)
//         }
//     }
// }


//异步创建目录
// function mkdirp(paths,cb){
//     let arr = paths.split('/')
//     let index = 0
//     function next(){
//         if(index === arr.length) return cb()
//         let currentPath = arr.slice(0,++index).join('/')
            //fs.access() 判断目录是否存在
//         fs.access(currentPath,(err) => {
//             if(err){
//                 fs.mkdir(currentPath,next)
//             }else{
//                 next()
//             }
//         })
//     }
//     next()
// }