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

后端 未结 22 1376
清歌不尽
清歌不尽 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:43

    Here is the coffee script prototype function I created for fluentnode that deletes a folder recursively

    String::folder_Delete_Recursive = ->
      path = @.toString()
      if path.exists()
        for file in path.files()
          curPath = path.path_Combine(file)
          if curPath.is_Folder()
            curPath.folder_Delete_Recursive()
          else
            curPath.file_Delete()
        fs.rmdirSync(path);
    
      return path.not_Exists()
    

    here is the test:

    it 'folder_Create and folder_Delete' , ->
      tmpDir = "./".temp_Name_In_Folder()
      expect(tmpDir.folder_Exists()).to.be.false
      expect(tmpDir.folder_Create()).to.equal(tmpDir.realPath())
      expect(tmpDir.folder_Exists()).to.be.true
      expect(tmpDir.folder_Delete()).to.be.true
      expect(tmpDir.folder_Exists()).to.be.false
    
    it 'folder_Delete_Recursive' , ->
      tmpDir = "./"   .temp_Name_In_Folder().folder_Create()
      tmpFile = tmpDir.temp_Name_In_Folder().file_Create()
      expect(tmpDir.folder_Delete_Recursive()).to.be.true
    

提交回复
热议问题