Is node.js rmdir recursive ? Will it work on non empty directories?

后端 未结 22 1652
清歌不尽
清歌不尽 2021-01-31 13:41

The documentation for fs.rmdir is very short and doesn\'t explain the behavior of rmdir when the directory is not empty.

Q: What happens if I try to use

22条回答
  •  误落风尘
    2021-01-31 13:53

    This post was getting the top answer from google but none of the answers gives a solution that:

    • doesn't make use of sync functions

    • doesn't require external libraries

    • doesn't use bash directly

    Here is my async solution which doesn't assume anything else than node installed:

    const fs = require('fs'); const path = require('path');
    
    function rm(path){  
        return stat(path).then((_stat) => {                                   
    
        if(_stat.isDirectory()){                                                                                                                                                                                                                          
          return ls(path)                                                                                                                                                                                                                                   
            .then((files) => Promise.all(files.map(file => rm(Path.join(path, file)))))
           .then(() => removeEmptyFolder(path));                                                                                                                                                                                                 
        }else{                                                                                                                                                                                                                                            
          return removeFileOrLink(path);                                                                                                                                                                                                            
        }   });
                                                                                                                                                                                                                                                  function removeEmptyFolder(path){                                     
    
        return new Promise((done, err) => {                                                                                                                                                                                                               
          fs.rmdir(path, function(error){                                                                                                                                                                                                                   
            if(error){ return err(error); }                                                                                                                                                                                                               
            return done("ok");                                                                                                                                                                                                                        
          });                                                                                                                                                                                                                                       
        });                                                                                                                                                                                                                                          }
                                                                                                                                                                                                                                                    function removeFileOrLink(path){                                      
    
        return new Promise((done, err) => {                                                                                                                                                                                                               
          fs.unlink(path, function(error){                                                                                                                                                                                                                  
            if(error){ return err(error); }                                                                                                                                                                                                               
            return done("ok");                                                                                                                                                                                                                        
          });                                                                                                                                                                                                                                       
        });                                                                                                                                                                                                                                          }
    
                                                                                                                                                                                                                                                    function ls(path){                                                    
    
        return new Promise((done, err) => {                                                                                                                                                                                                               
          fs.readdir(path, function (error, files) {                                                                                                                                                                                                        
            if(error) return err(error)                                                                                                                                                                                                                   
            return done(files)                                                                                                                                                                                                                        
          });                                                                                                                                                                                                                                       
        });                                                                                                                                                                                                                                          }
                                                                                                                                                                                                                                                    function stat(path){                                                  
    
        return new Promise((done, err) => {                                                                                                                                                                                                               
          fs.stat(path, function (error, _stat) {                                                                                                                                                                                                           
            if(error){ return err(error); }                                                                                                                                                                                                               
            return done(_stat);                                                                                                                                                                                                                       
          });                                                                                                                                                                                                                                       
        });                                                                                                                                                                                                                                          } }
    

提交回复
热议问题